Skip to content

Commit

Permalink
added github action (tentative)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudoku-lord committed Dec 21, 2021
1 parent 07736d4 commit b91baa0
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 40 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/run_tests_job.yml
@@ -0,0 +1,46 @@
name: Run full Elixir workflow

on:
pull_request:
types: [ opened, reopened, edited ]
branches:
- master
push:
branches:
- add_github_action
workflow_dispatch:
jobs:
elixir_workflow:
runs-on: ubuntu-latest
strategy:
matrix:
elixir-version: ['1.12']
otp: ['22.0']
steps:
- uses: actions/checkout@v2
- name: Setup Elixir
uses: erlef/setup-beam@v1
with:
otp-version: '22.0'
elixir-version: '1.12'
- name: Install Dependencies
run: mix deps.get
- name: Compile Repository
run: mix compile --warnings-as-errors
- name: Check Code Coverage
run: MIX_ENV=test mix coveralls.github --max-cases 1
env:
API_KEY: ${{ secrets.API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Run Static Code Analysis for Elixir
run: mix credo
- name: Run Static Code Analysis for Erlang
run: mix dialyzer --halt-exit-status
- name: Run Elixir Tests
env:
API_KEY: ${{ secrets.API_KEY }}
run: mix deps.get && mix test
- name: Send coverage to Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -28,7 +28,7 @@ This library implements the Lob API. Please read through the official [API Docum
The library requires a valid Lob API key to work properly. To acquire an API key, first create an account at [Lob.com](https://dashboard.lob.com/#/register). Once you have created an account, you can access your API Keys from the [Settings Panel](https://dashboard.lob.com/#/settings).

### API Key Configuration
The library will by default refer to the `:api_key` config when making authenticated requests. If that is not present, it will look for the `LOB_API_KEY` environment variable.
The library will by default refer to the `:api_key` config when making authenticated requests. If that is not present, it will look for the `API_KEY` environment variable.

```elixir
# Configuring an API key with configs
Expand Down Expand Up @@ -78,11 +78,11 @@ Tests are written using [ExUnit](https://hexdocs.pm/ex_unit/ExUnit.html), Elixir

Here's how you can run the tests:

LOB_API_KEY=YOUR_TEST_API_KEY mix test
API_KEY=YOUR_TEST_API_KEY mix test

To run tests with a coverage report:

LOB_API_KEY=YOUR_TEST_API_KEY mix coveralls.html
API_KEY=YOUR_TEST_API_KEY mix coveralls.html

Then view the report at `cover/excoveralls.html`.

Expand Down
2 changes: 1 addition & 1 deletion config/config.exs
@@ -1,3 +1,3 @@
use Mix.Config
import Config

import_config "#{Mix.env}.exs"
4 changes: 2 additions & 2 deletions config/test.exs
@@ -1,4 +1,4 @@
use Mix.Config
import Config

config :lob_elixir,
api_key: System.get_env("LOB_API_KEY")
api_key: System.get_env("API_KEY")
2 changes: 1 addition & 1 deletion lib/lob/bulk_intl_verification.ex
Expand Up @@ -8,7 +8,7 @@ defmodule Lob.BulkIntlVerification do
def verify(data, headers \\ %{"Content-type": "application/json"}) do
with {:ok, encoded_data} <- Poison.encode(data)
do
Client.post_request(base_url(), encoded_data, Util.build_headers(headers))
Client.post_request_binary(base_url(), encoded_data, Util.build_headers(headers))
end
end

Expand Down
13 changes: 10 additions & 3 deletions lib/lob/bulk_us_verification.ex
Expand Up @@ -2,12 +2,19 @@ defmodule Lob.BulkUSVerification do
@moduledoc """
Module implementing the Lob bulk US verifications API.
"""

use Lob.ResourceBase, endpoint: "bulk/us_verifications", methods: []

@spec verify(map, map) :: Client.client_response | no_return
# The @spec for the function does not match the success typing of the function.

# Function:
# Lob.BulkUSVerification.verify/2

# Success typing:
# @spec verify(%{:addresses => [any()]}, map()) :: {:error, _} | {:ok, map(), [any()]}

@spec verify(any, map()) :: {:error, any} | {:ok, map(), [any()]}
def verify(data, headers \\ %{"Content-type": "application/json"}) do
Client.post_request(base_url(), Poison.encode!(data), Util.build_headers(headers))
Client.post_request_binary(base_url(), Poison.encode!(data), Util.build_headers(headers))
end

end
16 changes: 11 additions & 5 deletions lib/lob/client.ex
Expand Up @@ -3,9 +3,8 @@ defmodule Lob.Client do
Client responsible for making requests to Lob and handling the responses.
"""

alias Poison.Parser
alias HTTPoison.Error
alias HTTPoison.Response
alias Poison.Parser

use HTTPoison.Base

Expand All @@ -20,7 +19,7 @@ defmodule Lob.Client do

defexception message: """
The api_key setting is required to make requests to Lob.
Please configure :api_key in config.exs or set the LOB_API_KEY
Please configure :api_key in config.exs or set the API_KEY
environment variable.
config :lob_elixir, api_key: API_KEY
Expand All @@ -32,7 +31,7 @@ defmodule Lob.Client do

@spec api_key(atom) :: String.t
def api_key(env_key \\ :api_key) do
case Confex.get_env(:lob_elixir, env_key, System.get_env("LOB_API_KEY")) || :not_found do
case Confex.get_env(:lob_elixir, env_key, System.get_env("API_KEY")) || :not_found do
:not_found -> raise MissingAPIKeyError
value -> value
end
Expand Down Expand Up @@ -68,13 +67,20 @@ defmodule Lob.Client do
|> handle_response
end

@spec post_request(String.t, {:multipart, list}, HTTPoison.Base.headers) :: client_response
@spec post_request(<<_::64, _::_*8>>, {:multipart, [any()]}, [{binary(), binary()}]) :: client_response
def post_request(url, body, headers \\ []) do
url
|> post(body, headers, build_options())
|> handle_response
end

@spec post_request_binary(<<_::64, _::_*8>>, binary(), [{binary(), binary()}]) :: client_response
def post_request_binary(url, body, headers \\ []) do
url
|> post(body, headers, build_options())
|> handle_response
end

@spec delete_request(String.t, HTTPoison.Base.headers) :: client_response
def delete_request(url, headers \\ []) do
url
Expand Down
20 changes: 13 additions & 7 deletions lib/lob/resource_base.ex
Expand Up @@ -10,8 +10,8 @@ defmodule Lob.ResourceBase do
quote do
@default_api_host "https://api.lob.com/v1"

alias Lob.Util
alias Lob.Client
alias Lob.Util

if :list in unquote(methods) do
@spec list(map, map) :: Client.client_response
Expand Down Expand Up @@ -73,14 +73,20 @@ defmodule Lob.ResourceBase do
end
end

@spec base_url :: String.t
defp base_url, do: "#{api_host()}/#{unquote(endpoint)}"
@spec base_url :: <<_::64, _::_*8>>
defp base_url do
"#{api_host()}/#{unquote(endpoint)}"
end

@spec api_host :: String.t
defp api_host, do: Application.get_env(:lob_elixir, :api_host, @default_api_host)
@spec api_host :: <<_::64, _::_*8>>
defp api_host do
"#{Application.get_env(:lob_elixir, :api_host, @default_api_host)}"
end

@spec resource_url(String.t) :: String.t
defp resource_url(resource_id), do: "#{base_url()}/#{resource_id}"
@spec resource_url(String.t) :: <<_::64, _::_*8>>
defp resource_url(resource_id) do
"#{base_url()}/#{resource_id}"
end
end

end
Expand Down
3 changes: 2 additions & 1 deletion lib/lob/util.ex
Expand Up @@ -27,7 +27,8 @@ defmodule Lob.Util do
"""
@spec build_body(map) :: {:multipart, list}
def build_body(body) when is_map(body) do
{:multipart, Enum.reduce(body, [], &(&2 ++ transform_argument(&1)))}
result = {:multipart, Enum.reduce(body, [], &(&2 ++ transform_argument(&1)))}
result
end

@doc """
Expand Down
6 changes: 6 additions & 0 deletions mix.exs
Expand Up @@ -21,6 +21,11 @@ defmodule Lob.Mixfile do
:unknown
],
ignore_warnings: ".dialyzer_ignore"
],
xref: [
exclude: [
:crypto
]
]
]
end
Expand All @@ -39,6 +44,7 @@ defmodule Lob.Mixfile do
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
{:excoveralls, "~> 0.14", only: :test},
{:httpoison, "~> 1.8"},
{:json, "~> 1.4"},
{:poison, "~> 5.0"},
{:plug_cowboy, "~> 2.5"},
{:uuid, "~> 1.1", only: :test}
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Expand Up @@ -16,6 +16,7 @@
"httpoison": {:hex, :httpoison, "1.8.0", "6b85dea15820b7804ef607ff78406ab449dd78bed923a49c7160e1886e987a3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "28089eaa98cf90c66265b6b5ad87c59a3729bea2e74e9d08f9b51eb9729b3c3a"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"json": {:hex, :json, "1.4.1", "8648f04a9439765ad449bc56a3ff7d8b11dd44ff08ffcdefc4329f7c93843dfa", [:mix], [], "hexpm", "9abf218dbe4ea4fcb875e087d5f904ef263d012ee5ed21d46e9dbca63f053d16"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm", "fc3499fed7a726995aa659143a248534adc754ebd16ccd437cd93b649a95091f"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
Expand Down
15 changes: 12 additions & 3 deletions test/lob/address_test.exs
Expand Up @@ -13,7 +13,10 @@ defmodule Lob.AddressTest do
address_city: "San Francisco",
address_state: "CA",
address_country: "US",
address_zip: "94107"
address_zip: "94107",
metadata: %{
foo: "bar"
}
}
}
end
Expand All @@ -35,9 +38,15 @@ defmodule Lob.AddressTest do
assert addresses.count == 2
end

test "filters by metadata" do
test "filters by metadata", %{sample_address: sample_address} do
{:ok, created_address, _headers} =
sample_address
|> Map.merge(%{metadata: %{foo: "bar"}})
|> Address.create

{:ok, addresses, _headers} = Address.list(%{metadata: %{foo: "bar"}})
assert addresses.count == 1
assert addresses.count > 0
Address.delete(created_address.id)
end

end
Expand Down
10 changes: 7 additions & 3 deletions test/lob/bank_account_test.exs
Expand Up @@ -9,7 +9,8 @@ defmodule Lob.BankAccountTest do
routing_number: "122100024",
account_number: "123456789",
account_type: "company",
signatory: "John Doe"
signatory: "John Doe",
metadata: %{foo: "bar"}
}
}
end
Expand All @@ -31,9 +32,12 @@ defmodule Lob.BankAccountTest do
assert bank_accounts.count == 2
end

test "filters by metadata" do
test "filters by metadata", %{sample_bank_account: sample_bank_account} do
{:ok, created_bank_account, _headers} = BankAccount.create(sample_bank_account)

{:ok, bank_accounts, _headers} = BankAccount.list(%{metadata: %{foo: "bar"}})
assert bank_accounts.count == 1
assert bank_accounts.count > 0
BankAccount.delete(created_bank_account.id)
end

end
Expand Down
19 changes: 16 additions & 3 deletions test/lob/check_test.exs
Expand Up @@ -26,7 +26,7 @@ defmodule Lob.CheckTest do

sample_check = %{
description: "Library Test Check #{DateTime.utc_now |> DateTime.to_string}",
amount: 100
amount: 100,
}

%{
Expand All @@ -53,9 +53,22 @@ defmodule Lob.CheckTest do
assert checks.count == 2
end

test "filters by metadata" do
test "filters by metadata", %{sample_address: sample_address, sample_bank_account: sample_bank_account, sample_check: sample_check} do
{:ok, created_address, _headers} = Address.create(sample_address)
{:ok, verified_bank_account, _headers} = create_and_verify_bank_account(sample_bank_account)

{:ok, created_check, _headers} =
Check.create(%{
description: sample_check.description,
to: created_address.id,
from: created_address.id,
bank_account: verified_bank_account.id,
amount: 1312,
metadata: %{foo: "bar"}
})
{:ok, checks, _headers} = Check.list(%{metadata: %{foo: "bar"}})
assert checks.count == 1
assert checks.count > 0
Check.delete(created_check.id)
end

end
Expand Down
6 changes: 3 additions & 3 deletions test/lob/client_test.exs
Expand Up @@ -15,10 +15,10 @@ defmodule Lob.ClientTest do
test "raises MissingAPIKeyError if no API key is found" do

assert_raise(MissingAPIKeyError, fn ->
api_key = System.get_env("LOB_API_KEY")
System.delete_env("LOB_API_KEY")
api_key = System.get_env("API_KEY")
System.delete_env("API_KEY")
Client.api_key(nil)
System.put_env("LOB_API_KEY", api_key)
System.put_env("API_KEY", api_key)
end)

end
Expand Down
23 changes: 20 additions & 3 deletions test/lob/letter_test.exs
Expand Up @@ -17,7 +17,7 @@ defmodule Lob.LetterTest do
}

sample_letter = %{
description: "Library Test Letter #{DateTime.utc_now |> DateTime.to_string}"
description: "Library Test Letter #{DateTime.utc_now |> DateTime.to_string}",
}

%{
Expand All @@ -43,9 +43,26 @@ defmodule Lob.LetterTest do
assert letters.count == 2
end

test "filters by metadata" do
test "filters by metadata", %{sample_address: sample_address} do
{:ok, created_address, _headers} = Address.create(sample_address)

{:ok, created_letter, _headers} =
Letter.create(%{
description: "Letter with metadata",
to: created_address.id,
from: created_address.id,
color: true,
file: "<html>{{data.name}}</html>",
metadata: %{foo: "bar"},
merge_variables: %{
data: %{
name: "Donald"
}
}
})
{:ok, letters, _headers} = Letter.list(%{metadata: %{foo: "bar"}})
assert letters.count == 1
assert letters.count > 0
Letter.delete(created_letter.id)
end

end
Expand Down

0 comments on commit b91baa0

Please sign in to comment.