Skip to content

Commit

Permalink
chore(template-syntax): allow nested merge variables (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloysius Pawlicki committed Apr 13, 2020
1 parent e876385 commit a28fc52
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/lob/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ defmodule Lob.Util do
end

@spec transform_argument({any, any}) :: list
defp transform_argument({:merge_variables, v}), do: transform_argument({"merge_variables", v})

defp transform_argument({"merge_variables", v}) do
[{"merge_variables", Poison.encode!(v), [{"Content-Type", "application/json"}]}]
end

defp transform_argument({k, v}) when is_list(v) do
Enum.map(v, fn e ->
{"#{to_string(k)}[]", to_string(e)}
Expand Down
10 changes: 5 additions & 5 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"bypass": {:hex, :bypass, "0.8.1", "16d409e05530ece4a72fabcf021a3e5c7e15dcc77f911423196a0c551f2a15ca", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"confex": {:hex, :confex, "3.4.0", "8b1c3cc7a93320291abb31223a178df19d7f722ee816c05a8070c8c9a054560d", [:mix], [], "hexpm"},
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], [], "hexpm"},
"credo": {:hex, :credo, "0.8.10", "261862bb7363247762e1063713bb85df2bbd84af8d8610d1272cd9c1943bba63", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.7.5", "339e433e5d3bce09400dc8de7b9040741a409c93917849916c136a0f51fdc183", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.15.0", "287a5d2304d516f63e56c469511c42b016423bcb167e61b611f6bad47e3ca60e", [:rebar3], [{:certifi, "2.4.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "1.5.0", "71ae9f304bdf7f00e9cd1823f275c955bdfc68282bc5eb5c85c3a9ade865d68e", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "1.6.2", "ace7c8d3a361cebccbed19c283c349b3d26991eff73a1eaaa8abae2e3c8089b6", [:mix], [{:hackney, "~> 1.15 and >= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [:mix], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"plug": {:hex, :plug, "1.4.3", "236d77ce7bf3e3a2668dc0d32a9b6f1f9b1f05361019946aae49874904be4aed", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
"uuid": {:hex, :uuid, "1.1.8", "e22fc04499de0de3ed1116b770c7737779f226ceefa0badb3592e64d5cfb4eb9", [:mix], [], "hexpm"},
}
21 changes: 21 additions & 0 deletions test/lob/check_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,27 @@ defmodule Lob.CheckTest do
assert created_check.description == duplicated_postcard.description
end

test "creates a check with a merge variable conditional", %{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: 42,
attachment: "<html>{{#is_awesome}}You are awesome!{{/is_awesome}}</html>",
merge_variables: %{
is_awesome: true
}
})

assert created_check.description == sample_check.description
assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"})
end

end

describe "delete/2" do
Expand Down
22 changes: 22 additions & 0 deletions test/lob/letter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ defmodule Lob.LetterTest do
assert created_letter.description == duplicated_letter.description
end

test "creates a letter with a merge variable object", %{sample_address: sample_address, sample_letter: sample_letter} do
{:ok, created_address, _headers} = Address.create(sample_address)

{:ok, created_letter, headers} =
Letter.create(%{
description: sample_letter.description,
to: created_address.id,
from: created_address.id,
color: true,
file: "<html>{{data.name}}</html>",
merge_variables: %{
data: %{
name: "Donald"
}
}
})

assert created_letter.description == sample_letter.description
assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"})
end


end

describe "delete/2" do
Expand Down
25 changes: 25 additions & 0 deletions test/lob/postcard_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,31 @@ defmodule Lob.PostcardTest do
assert created_postcard.description == duplicated_postcard.description
end

test "creates a postcard with a merge variable list", %{sample_address: sample_address, sample_postcard: sample_postcard} do
{:ok, created_address, _headers} = Address.create(sample_address)

{:ok, created_postcard, headers} =
Postcard.create(%{
description: sample_postcard.description,
to: created_address.id,
front: "<html>{{#list}} {{name}} {{/list}}</html>",
back: sample_postcard.back,
merge_variables: %{
list: [
%{
name: "Larissa"
},
%{
name: "Larry"
}
]
}
})

assert created_postcard.description == sample_postcard.description
assert Enum.member?(headers, {"X-Rate-Limit-Limit", "150"})
end

end

describe "delete/2" do
Expand Down

0 comments on commit a28fc52

Please sign in to comment.