From bb041b8e50e501972f25310630e1557d64838c4b Mon Sep 17 00:00:00 2001 From: Milton Mazzarri Date: Wed, 19 Apr 2017 18:10:57 -0500 Subject: [PATCH] Fix some specs --- lib/hunter.ex | 8 ++++---- lib/hunter/account.ex | 8 ++++---- lib/hunter/api.ex | 8 ++++---- lib/hunter/api/http_client.ex | 22 +++++++++++++++------- lib/hunter/application.ex | 2 +- lib/hunter/attachment.ex | 8 ++++---- lib/hunter/card.ex | 2 +- lib/hunter/client.ex | 4 ++-- lib/hunter/error.ex | 7 +++++++ lib/hunter/instance.ex | 2 +- lib/hunter/mention.ex | 2 +- lib/hunter/notification.ex | 2 +- lib/hunter/status.ex | 6 +++--- lib/hunter/tag.ex | 2 +- 14 files changed, 49 insertions(+), 34 deletions(-) create mode 100644 lib/hunter/error.ex diff --git a/lib/hunter.ex b/lib/hunter.ex index 9e0d4ae..3f88d72 100644 --- a/lib/hunter.ex +++ b/lib/hunter.ex @@ -81,7 +81,7 @@ defmodule Hunter do * `uri` - URI of the remote user, in the format of `username@domain` """ - @spec follow_by_uri(Hunter.Client.t, URI.t) :: Hunter.Account.t + @spec follow_by_uri(Hunter.Client.t, String.t) :: Hunter.Account.t defdelegate follow_by_uri(conn, uri), to: Hunter.Account @doc """ @@ -188,7 +188,7 @@ defmodule Hunter do different instance. default: `https://mastodon.social` """ - @spec create_app(String.t, URI.t, [String.t], String.t, Keyword.t) :: Hunter.Application.t + @spec create_app(String.t, String.t, [String.t], String.t, Keyword.t) :: Hunter.Application.t | no_return defdelegate create_app(name, redirect_uri \\ "urn:ietf:wg:oauth:2.0:oob", scopes \\ ["read"], website \\ nil, options \\ []), to: Hunter.Application @doc """ @@ -342,7 +342,7 @@ defmodule Hunter do * `media_ids` - [Array] """ - @spec create_status(Hunter.Client.t, String.t, non_neg_integer, [non_neg_integer]) :: Hunter.Status.t + @spec create_status(Hunter.Client.t, String.t, non_neg_integer, [non_neg_integer]) :: Hunter.Status.t | no_return defdelegate create_status(conn, text, in_reply_to_id \\ nil, media_ids \\ []), to: Hunter.Status @doc """ @@ -568,7 +568,7 @@ defmodule Hunter do * `conn` - connection credentials """ - @spec clear_notifications(Hunter.Client.t) :: map + @spec clear_notifications(Hunter.Client.t) :: boolean defdelegate clear_notifications(conn), to: Hunter.Notification @doc """ diff --git a/lib/hunter/account.ex b/lib/hunter/account.ex index f90ebab..7fefa79 100644 --- a/lib/hunter/account.ex +++ b/lib/hunter/account.ex @@ -30,9 +30,9 @@ defmodule Hunter.Account do acct: String.t, display_name: String.t, note: String.t, - url: URI.t, - avatar: URI.t, - header: URI.t, + url: String.t, + avatar: String.t, + header: String.t, locked: String.t, created_at: String.t, followers_count: non_neg_integer, @@ -140,7 +140,7 @@ defmodule Hunter.Account do * `uri` - URI of the remote user, in the format of `username@domain` """ - @spec follow_by_uri(Hunter.Client.t, URI.t) :: Hunter.Account.t + @spec follow_by_uri(Hunter.Client.t, String.t) :: Hunter.Account.t def follow_by_uri(conn, uri) do @hunter_api.follow_by_uri(conn, uri) end diff --git a/lib/hunter/api.ex b/lib/hunter/api.ex index 2918b59..57e2dc8 100644 --- a/lib/hunter/api.ex +++ b/lib/hunter/api.ex @@ -163,7 +163,7 @@ defmodule Hunter.Api do Multiple scopes can be requested during the authorization phase with the `scope` query param """ - @callback create_app(name :: String.t, redirect_uri :: URI.t, scopes :: [String.t], website :: String.t, base_url :: String.t) :: Hunter.Application.t + @callback create_app(name :: String.t, redirect_uri :: String.t, scopes :: [String.t], website :: String.t, base_url :: String.t) :: Hunter.Application.t | no_return @doc """ Upload a media file @@ -284,7 +284,7 @@ defmodule Hunter.Api do * `media_ids` - [Array] """ - @callback create_status(conn :: Hunter.Client.t, text :: String.t, in_reply_to_id :: non_neg_integer, media_ids :: [non_neg_integer]) :: Hunter.Status.t + @callback create_status(conn :: Hunter.Client.t, text :: String.t, in_reply_to_id :: non_neg_integer, media_ids :: [non_neg_integer]) :: Hunter.Status.t | no_return @doc """ Retrieve status @@ -492,7 +492,7 @@ defmodule Hunter.Api do * `conn` - connection credentials """ - @callback clear_notifications(conn :: Hunter.Client.t) :: map + @callback clear_notifications(conn :: Hunter.Client.t) :: boolean @doc """ Retrieve a user's reports @@ -550,5 +550,5 @@ defmodule Hunter.Api do * `base_url` - API base url, default: `https://mastodon.social` """ - @callback log_in(app :: Hunter.Application.t, username :: String.t, password :: String.t, base_url :: URI.t) :: Hunter.Client.t + @callback log_in(app :: Hunter.Application.t, username :: String.t, password :: String.t, base_url :: String.t) :: Hunter.Client.t end diff --git a/lib/hunter/api/http_client.ex b/lib/hunter/api/http_client.ex index d108adc..333f808 100644 --- a/lib/hunter/api/http_client.ex +++ b/lib/hunter/api/http_client.ex @@ -69,8 +69,12 @@ defmodule Hunter.Api.HTTPClient do def create_app(name, redirect_uri, scopes, website, base_url) do payload = Poison.encode!(%{client_name: name, redirect_uris: redirect_uri, scopes: Enum.join(scopes, " "), website: website}) - %HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/apps", payload, [{"Content-Type", "application/json"}]) - Poison.decode!(body, as: %Hunter.Application{}) + case HTTPoison.post(base_url <> "/api/v1/apps", payload, [{"Content-Type", "application/json"}]) do + {:ok, %HTTPoison.Response{body: body, status_code: 200}} -> + Poison.decode!(body, as: %Hunter.Application{}) + {:error, %HTTPoison.Error{reason: reason}} -> + raise Hunter.Error, reason: reason + end end def upload_media(%Hunter.Client{base_url: base_url} = conn, file) do @@ -136,13 +140,17 @@ defmodule Hunter.Api.HTTPClient do def create_status(%Hunter.Client{base_url: base_url} = conn, text, in_reply_to_id, _media_ids) do payload = Poison.encode!(%{status: text, in_reply_to_id: in_reply_to_id}) - %HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/statuses", payload, [{"Content-Type", "application/json"} | get_headers(conn)]) - to_status(body) + case HTTPoison.post(base_url <> "/api/v1/statuses", payload, [{"Content-Type", "application/json"} | get_headers(conn)]) do + {:ok, %HTTPoison.Response{body: body, status_code: 200}} -> + to_status(body) + {:error, %HTTPoison.Error{reason: reason}} -> + raise Hunter.Error, reason: reason + end end def status(%Hunter.Client{base_url: base_url} = conn, id) do %HTTPoison.Response{body: body, status_code: 200} = HTTPoison.get!(base_url <> "/api/v1/statuses/#{id}", get_headers(conn)) - Poison.decode(body, as: %Hunter.Status{}) + to_status(body) end def destroy_status(%Hunter.Client{base_url: base_url} = conn, id) do @@ -245,8 +253,8 @@ defmodule Hunter.Api.HTTPClient do def clear_notifications(%Hunter.Client{base_url: base_url} = conn) do payload = Poison.encode!(%{}) - %HTTPoison.Response{body: body, status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/notifications/clear", payload, [{"Content-Type", "application/json"} | get_headers(conn)]) - body + %HTTPoison.Response{status_code: 200} = HTTPoison.post!(base_url <> "/api/v1/notifications/clear", payload, [{"Content-Type", "application/json"} | get_headers(conn)]) + true end def reports(%Hunter.Client{base_url: base_url} = conn) do diff --git a/lib/hunter/application.ex b/lib/hunter/application.ex index d5cc2b9..586a1bb 100644 --- a/lib/hunter/application.ex +++ b/lib/hunter/application.ex @@ -53,7 +53,7 @@ defmodule Hunter.Application do different instance. default: `https://mastodon.social` """ - @spec create_app(String.t, URI.t, [String.t], String.t, Keyword.t) :: Hunter.Application.t + @spec create_app(String.t, String.t, [String.t], String.t, Keyword.t) :: Hunter.Application.t | no_return def create_app(name, redirect_uri \\ "urn:ietf:wg:oauth:2.0:oob", scopes \\ ["read"], website \\ nil, options \\ []) do save? = Keyword.get(options, :save?, false) base_url = Keyword.get(options, :api_base_url, "https://mastodon.social") diff --git a/lib/hunter/attachment.ex b/lib/hunter/attachment.ex index 9582cc8..dff32a7 100644 --- a/lib/hunter/attachment.ex +++ b/lib/hunter/attachment.ex @@ -20,10 +20,10 @@ defmodule Hunter.Attachment do @type t :: %__MODULE__{ id: non_neg_integer, type: String.t, - url: URI.t, - remote_url: URI.t, - preview_url: URI.t, - text_url: URI.t + url: String.t, + remote_url: String.t, + preview_url: String.t, + text_url: String.t } @derive [Poison.Encoder] diff --git a/lib/hunter/card.ex b/lib/hunter/card.ex index d50a9f3..3b39c71 100644 --- a/lib/hunter/card.ex +++ b/lib/hunter/card.ex @@ -16,7 +16,7 @@ defmodule Hunter.Card do @hunter_api Hunter.Config.hunter_api() @type t :: %__MODULE__{ - url: URI.t, + url: String.t, title: String.t, description: String.t, image: String.t diff --git a/lib/hunter/client.ex b/lib/hunter/client.ex index 9a40989..b293e13 100644 --- a/lib/hunter/client.ex +++ b/lib/hunter/client.ex @@ -6,7 +6,7 @@ defmodule Hunter.Client do @hunter_api Hunter.Config.hunter_api() @type t :: %__MODULE__{ - base_url: URI.t, + base_url: String.t, bearer_token: String.t } @@ -46,7 +46,7 @@ defmodule Hunter.Client do * `base_url` - API base url, default: `https://mastodon.social` """ - @spec log_in(Hunter.Application.t, String.t, String.t, URI.t) :: Hunter.Client.t + @spec log_in(Hunter.Application.t, String.t, String.t, String.t) :: Hunter.Client.t def log_in(app, username, password, base_url \\ "https://mastodon.social") do @hunter_api.log_in(app, username, password, base_url) end diff --git a/lib/hunter/error.ex b/lib/hunter/error.ex new file mode 100644 index 0000000..be54781 --- /dev/null +++ b/lib/hunter/error.ex @@ -0,0 +1,7 @@ +defmodule Hunter.Error do + @type t :: %__MODULE__{reason: any} + + defexception reason: nil + + def message(%__MODULE__{reason: reason}), do: inspect(reason) +end diff --git a/lib/hunter/instance.ex b/lib/hunter/instance.ex index 60afe28..70b2363 100644 --- a/lib/hunter/instance.ex +++ b/lib/hunter/instance.ex @@ -16,7 +16,7 @@ defmodule Hunter.Instance do @hunter_api Hunter.Config.hunter_api() @type t :: %__MODULE__{ - uri: URI.t, + uri: String.t, title: String.t, description: String.t, email: String.t diff --git a/lib/hunter/mention.ex b/lib/hunter/mention.ex index 256e400..55c0ef7 100644 --- a/lib/hunter/mention.ex +++ b/lib/hunter/mention.ex @@ -11,7 +11,7 @@ defmodule Hunter.Mention do """ @type t :: %__MODULE__{ - url: URI.t, + url: String.t, username: String.t, acct: String.t, id: non_neg_integer diff --git a/lib/hunter/notification.ex b/lib/hunter/notification.ex index 9f67c60..199f1a4 100644 --- a/lib/hunter/notification.ex +++ b/lib/hunter/notification.ex @@ -62,7 +62,7 @@ defmodule Hunter.Notification do * `conn` - connection credentials """ - @spec clear_notifications(Hunter.Client.t) :: map + @spec clear_notifications(Hunter.Client.t) :: boolean def clear_notifications(conn) do @hunter_api.clear_notifications(conn) end diff --git a/lib/hunter/status.ex b/lib/hunter/status.ex index 8b318f6..4bb25eb 100644 --- a/lib/hunter/status.ex +++ b/lib/hunter/status.ex @@ -30,8 +30,8 @@ defmodule Hunter.Status do @type t :: %__MODULE__{ id: non_neg_integer, - uri: URI.t, - url: URI.t, + uri: String.t, + url: String.t, account: Hunter.Account.t, in_reply_to_id: non_neg_integer, reblog: Hunter.Status.t | nil, @@ -84,7 +84,7 @@ defmodule Hunter.Status do * `media_ids` - [Array] """ - @spec create_status(Hunter.Client.t, String.t, status_id, [non_neg_integer]) :: Hunter.Status.t + @spec create_status(Hunter.Client.t, String.t, status_id, [non_neg_integer]) :: Hunter.Status.t | no_return def create_status(conn, text, in_reply_to_id \\ nil, media_ids \\ []) do @hunter_api.create_status(conn, text, in_reply_to_id, media_ids) end diff --git a/lib/hunter/tag.ex b/lib/hunter/tag.ex index a41846d..f96f2b1 100644 --- a/lib/hunter/tag.ex +++ b/lib/hunter/tag.ex @@ -11,7 +11,7 @@ defmodule Hunter.Tag do @type t :: %__MODULE__{ name: String.t, - url: URI.t + url: String.t } @derive [Poison.Encoder]