From 9d5f63df9cb36708c20a409534ef40b2674ae21c Mon Sep 17 00:00:00 2001 From: whodidthis Date: Wed, 8 Mar 2017 01:39:15 +0200 Subject: [PATCH] Support passing custom params to Postmark Postmark's API accepts several parameters that we don't want to directly support to keep the library's API small and clean. This adds support for passing parameters we don't directly support to the request's body. --- lib/bamboo/postmark_adapter.ex | 8 ++++++++ lib/bamboo/postmark_helper.ex | 16 ++++++++++++++++ test/lib/bamboo/postmark_adapter_test.exs | 17 ++++++++++++++--- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/bamboo/postmark_adapter.ex b/lib/bamboo/postmark_adapter.ex index 3fe95c4..f24b7e9 100644 --- a/lib/bamboo/postmark_adapter.ex +++ b/lib/bamboo/postmark_adapter.ex @@ -116,8 +116,16 @@ defmodule Bamboo.PostmarkAdapter do "Headers": email_headers(email), "TrackOpens": true } + |> add_message_params(email) end + defp add_message_params(params, %{private: %{message_params: message_params}}) do + Enum.reduce(message_params, params, fn({key, value}, params) -> + Map.put(params, key, value) + end) + end + defp add_message_params(params, _), do: params + defp email_from(email) do name = email.from |> elem(0) email = email.from |> elem(1) diff --git a/lib/bamboo/postmark_helper.ex b/lib/bamboo/postmark_helper.ex index bbbdbca..28ada5f 100644 --- a/lib/bamboo/postmark_helper.ex +++ b/lib/bamboo/postmark_helper.ex @@ -35,4 +35,20 @@ defmodule Bamboo.PostmarkHelper do |> Email.put_private(:template_id, template_id) |> Email.put_private(:template_model, template_model) end + + @doc """ + Put extra message parameters that are used by Postmark. You can set things like TrackOpens or TrackLinks. + + ## Example + email + |> put_params(email, "TrackLinks", "HtmlAndText") + |> put_params(email, "TrackOpens", true) + """ + def put_param(%Email{private: %{message_params: _}} = email, key, value) do + put_in(email.private[:message_params][key], value) + end + def put_param(email, key, value) do + email |> Email.put_private(:message_params, %{}) |> put_param(key, value) + end + end diff --git a/test/lib/bamboo/postmark_adapter_test.exs b/test/lib/bamboo/postmark_adapter_test.exs index ac31bcd..762cdd7 100644 --- a/test/lib/bamboo/postmark_adapter_test.exs +++ b/test/lib/bamboo/postmark_adapter_test.exs @@ -158,11 +158,22 @@ defmodule Bamboo.PostmarkAdapterTest do end test "deliver/2 puts tag param" do - email = new_email() |> PostmarkHelper.tag("some_tag") + email = new_email() |> PostmarkHelper.tag("some_tag") + email |> PostmarkAdapter.deliver(@config) + assert_receive {:fake_postmark, %{params: %{"Tag" => "some_tag"}}} + end - email |> PostmarkAdapter.deliver(@config) + test "deliver/2 puts tracking params" do + email = new_email() + |> PostmarkHelper.template("hello") + |> PostmarkHelper.put_param("TrackOpens", true) + |> PostmarkHelper.put_param("TrackLinks", "HtmlOnly") + + email |> PostmarkAdapter.deliver(@config) - assert_receive {:fake_postmark, %{params: %{"Tag" => "some_tag"}}} + assert_receive {:fake_postmark, %{params: %{ + "TrackLinks" => "HtmlOnly", "TrackOpens" => true, "TemplateId" => "hello"} + }} end test "raises if the response is not a success" do