Skip to content

Commit

Permalink
Merge pull request #45 from danielberkompas/feature/credo-linter
Browse files Browse the repository at this point in the history
Add Credo Linter
  • Loading branch information
danielberkompas authored Jul 20, 2016
2 parents acf6e4d + 93d0e51 commit 735725a
Show file tree
Hide file tree
Showing 23 changed files with 241 additions and 38 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ before_script:
- mix local.hex --force
- mix deps.get --only test
script:
- mix credo --strict
- mix test
- dialyzer --no_check_plt --plt $PLT_LOCATION -Wno_match -Wno_return --no_native _build/test/lib/ex_twilio/ebin
after_script:
Expand Down
109 changes: 109 additions & 0 deletions config/.credo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# This file contains the configuration for Credo and you are probably reading
# this after creating it with `mix credo.gen.config`.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
#
%{
#
# You can have as many configs as you like in the `configs:` field.
configs: [
%{
#
# Run any config using `mix credo -C <name>`. If no config name is given
# "default" is used.
name: "default",
#
# these are the files included in the analysis
files: %{
#
# you can give explicit globs or simply directories
# in the latter case `**/*.{ex,exs}` will be used
included: ["lib/"],
excluded: [~r"/_build/", ~r"/deps/"]
},
#
# If you create your own checks, you must specify the source files for
# them here, so they can be loaded by Credo before running the analysis.
requires: [],
#
# Credo automatically checks for updates, like e.g. Hex does.
# You can disable this behaviour below:
check_for_updates: true,
#
# You can customize the parameters of any check by adding a second element
# to the tuple.
#
# To disable a check put `false` as second element:
#
# {Credo.Check.Design.DuplicatedCode, false}
#
checks: [
{Credo.Check.Consistency.ExceptionNames},
{Credo.Check.Consistency.LineEndings},
{Credo.Check.Consistency.SpaceAroundOperators},
{Credo.Check.Consistency.SpaceInParentheses},
{Credo.Check.Consistency.TabsOrSpaces},

# For some checks, like AliasUsage, you can only customize the priority
# Priority values are: `low, normal, high, higher`
{Credo.Check.Design.AliasUsage, priority: :low},

# For others you can set parameters

# If you don't want the `setup` and `test` macro calls in ExUnit tests
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},

# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},

{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 100},
{Credo.Check.Readability.ModuleAttributeNames},
{Credo.Check.Readability.ModuleDoc},
{Credo.Check.Readability.ModuleNames},
{Credo.Check.Readability.ParenthesesInCondition},
{Credo.Check.Readability.PredicateFunctionNames},
{Credo.Check.Readability.TrailingBlankLine},
{Credo.Check.Readability.TrailingWhiteSpace},
{Credo.Check.Readability.VariableNames},

{Credo.Check.Refactor.ABCSize},
# {Credo.Check.Refactor.CaseTrivialMatches}, # deprecated in 0.4.0
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.UnlessWithElse},

{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
{Credo.Check.Warning.NameRedeclarationByAssignment},
{Credo.Check.Warning.NameRedeclarationByCase},
{Credo.Check.Warning.NameRedeclarationByDef},
{Credo.Check.Warning.NameRedeclarationByFn},
{Credo.Check.Warning.OperationOnSameValues},
{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.UnusedEnumOperation},
{Credo.Check.Warning.UnusedKeywordOperation},
{Credo.Check.Warning.UnusedListOperation},
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},
{Credo.Check.Warning.OperationWithConstantResult},

# Custom checks can be created using `mix credo.gen.check`.
#
]
}
]
}
10 changes: 6 additions & 4 deletions lib/ex_twilio/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ defmodule ExTwilio.Api do
"""
@spec find(atom, String.t | nil, list) :: Parser.success | Parser.error
def find(module, sid, options \\ []) do
Url.build_url(module, sid, options)
module
|> Url.build_url(sid, options)
|> Api.get!
|> Parser.parse(module)
end
Expand Down Expand Up @@ -84,7 +85,7 @@ defmodule ExTwilio.Api do
@spec update(atom, String.t, data, list) :: Parser.success | Parser.error
def update(module, sid, data, options \\ [])
def update(module, sid, data, options) when is_binary(sid), do: do_update(module, sid, data, options)
def update(module, %{sid: sid}, data, options), do: do_update(module, sid, data, options)
def update(module, %{sid: sid}, data, options), do: do_update(module, sid, data, options)
defp do_update(module, sid, data, options) do
data = format_data(data)
module
Expand All @@ -107,7 +108,7 @@ defmodule ExTwilio.Api do
@spec destroy(atom, String.t) :: Parser.success_delete | Parser.error
def destroy(module, sid, options \\ [])
def destroy(module, sid, options) when is_binary(sid), do: do_destroy(module, sid, options)
def destroy(module, %{sid: sid}, options), do: do_destroy(module, sid, options)
def destroy(module, %{sid: sid}, options), do: do_destroy(module, sid, options)
defp do_destroy(module, sid, options) do
module
|> Url.build_url(sid, options)
Expand All @@ -124,9 +125,10 @@ defmodule ExTwilio.Api do
"""
@spec process_request_headers(list) :: list
def process_request_headers(headers) do
auth = Base.encode64("#{Config.account_sid}:#{Config.auth_token}")
headers
|> Keyword.put(:"Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|> Keyword.put(:"Authorization", "Basic " <> Base.encode64("#{Config.account_sid}:#{Config.auth_token}"))
|> Keyword.put(:"Authorization", "Basic #{auth}")
end

@spec format_data(data) :: binary
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_twilio/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule ExTwilio.Config do

@doc """
Returns the Twilio Account SID. Set it in `mix.exs`:
config :ex_twilio, account_sid: "YOUR_ACCOUNT_SID"
"""
def account_sid, do: Application.get_env(:ex_twilio, :account_sid)
Expand Down Expand Up @@ -34,7 +34,7 @@ defmodule ExTwilio.Config do
def api_version, do: Application.get_env(:ex_twilio, :api_version) || "2010-04-01"

@doc """
Return the combined base URL of the Twilio API, using the configuration
Return the combined base URL of the Twilio API, using the configuration
settings given.
"""
def base_url do
Expand Down
4 changes: 2 additions & 2 deletions lib/ex_twilio/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ defmodule ExTwilio.Parser do
@spec handle_errors(response, ((String.t) -> any)) :: success | success_delete | error
defp handle_errors(response, fun) do
case response do
%{body: body, status_code: status} when status in [200, 201] ->
%{body: body, status_code: status} when status in [200, 201] ->
{:ok, fun.(body)}

%{body: _, status_code: 204} ->
:ok

%{body: body, status_code: status} ->
%{body: body, status_code: status} ->
{:ok, json} = Poison.decode(body)
{:error, json["message"], status}
end
Expand Down
6 changes: 5 additions & 1 deletion lib/ex_twilio/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ defmodule ExTwilio.Resource do

if :all in import_functions do
@spec all(list) :: [map]
def all(options \\ []), do: stream(options) |> Enum.into([])
def all(options \\ []) do
options
|> stream
|> Enum.into([])
end
end

if :find in import_functions do
Expand Down
10 changes: 8 additions & 2 deletions lib/ex_twilio/resources/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ defmodule ExTwilio.Application do
message_status_callback: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def parents, do: [:account]
end

13 changes: 10 additions & 3 deletions lib/ex_twilio/resources/call.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ defmodule ExTwilio.Call do
caller_name: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def cancel(%{sid: sid}), do: cancel(sid)
def cancel(sid), do: update(sid, status: "canceled")
def cancel(sid), do: update(sid, status: "canceled")

def complete(%{sid: sid}), do: complete(sid)
def complete(sid), do: update(sid, status: "completed")
def complete(sid), do: update(sid, status: "completed")

def parents, do: [:account]
end
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/incoming_phone_number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ defmodule ExTwilio.IncomingPhoneNumber do
address_requirements: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def parents, do: [:account]
end
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ defmodule ExTwilio.Message do
uri: nil,
subresource_uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def parents, do: [:account]
end
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/outgoing_caller_id.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ defmodule ExTwilio.OutgoingCallerId do
call_sid: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def parents, do: [:account]
end
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/queue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ defmodule ExTwilio.Queue do
max_size: nil,
average_wait_time: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def parents, do: [:account]
end
2 changes: 1 addition & 1 deletion lib/ex_twilio/resources/short_code.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExTwilio.ShortCode do
@moduledoc """
Represents a ShortCode resource in the Twilio API.
- [Twilio docs](https://www.twilio.com/docs/api/rest/short-codes)
"""

Expand Down
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/sip_credential.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ defmodule ExTwilio.SipCredential do
date_updated: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def resource_name, do: "Credentials"
def resource_collection_name, do: "credentials"
Expand Down
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/sip_credential_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ defmodule ExTwilio.SipCredentialList do
date_updated: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def resource_name, do: "SIP/CredentialLists"
def resource_collection_name, do: "credential_lists"
Expand Down
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/sip_domain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ defmodule ExTwilio.SipDomain do
date_updated: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def resource_name, do: "SIP/Domains"
def resource_collection_name, do: "domains"
Expand Down
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/sip_ip_access_control_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ defmodule ExTwilio.SipIpAccessControlList do
date_updated: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def resource_name, do: "SIP/IpAccessControlLists"
def resource_collection_name, do: "ip_access_control_lists"
Expand Down
9 changes: 8 additions & 1 deletion lib/ex_twilio/resources/sip_ip_address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ defmodule ExTwilio.SipIpAddress do
date_updated: nil,
uri: nil

use ExTwilio.Resource, import: [:stream, :all, :find, :create, :update, :destroy]
use ExTwilio.Resource, import: [
:stream,
:all,
:find,
:create,
:update,
:destroy
]

def resource_name, do: "IpAddresses"
def resource_collection_name, do: "ip_addresses"
Expand Down
Loading

0 comments on commit 735725a

Please sign in to comment.