From 75d4783d05d5871cca62054454833de7a72a2b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20C=C3=A1rdenas?= Date: Mon, 16 Oct 2017 17:47:19 -0500 Subject: [PATCH] Fix code style issues There are some old practices that are now considered bad code style. This change also fixes a typo in the documentation of `put_param` and adds an attachment example to the README. --- README.md | 14 +++-- config/config.exs | 29 ---------- lib/bamboo/postmark_adapter.ex | 18 ++++++- lib/bamboo/postmark_helper.ex | 21 ++++++-- test/lib/bamboo/postmark_adapter_test.exs | 65 +++++++++++++---------- 5 files changed, 79 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 5e64dc7..84dbca4 100644 --- a/README.md +++ b/README.md @@ -93,15 +93,21 @@ end ## Sending extra parameters -You can send other extra parameters to Postmark such as TrackOpens or -TrackLinks by using `put_params`. +You can send other extra parameters to Postmark with the `put_param` helper. > See Postmark's API for a complete list of parameters supported. ```elixir email -|> put_params("TrackLinks", "HtmlAndText") -|> put_params("TrackOpens", true) +|> put_param("TrackLinks", "HtmlAndText") +|> put_param("TrackOpens", true) +|> put_param("Attachments", [ + %{ + Name: "file.txt", + Content: "/some/file.txt" |> File.read!() |> Base.encode64(), + ContentType: "txt" + } +]) ``` ## Changing the underlying request configuration diff --git a/config/config.exs b/config/config.exs index 984f426..d2d855e 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,30 +1 @@ -# This file is responsible for configuring your application -# and its dependencies with the aid of the Mix.Config module. use Mix.Config - -# This configuration is loaded before any dependency and is restricted -# to this project. If another project depends on this project, this -# file won't be loaded nor affect the parent project. For this reason, -# if you want to provide default values for your application for -# 3rd-party users, it should be done in your "mix.exs" file. - -# You can configure for your application as: -# -# config :bamboo_postmark, key: :value -# -# And access this configuration in your application as: -# -# Application.get_env(:bamboo_postmark, :key) -# -# Or configure a 3rd-party app: -# -# config :logger, level: :info -# - -# It is also possible to import configuration files, relative to this -# directory. For example, you can emulate configuration per environment -# by uncommenting the line below and defining dev.exs, test.exs and such. -# Configuration from the imported file will override the ones defined -# here (which is why it is important to import them last). -# -# import_config "#{Mix.env}.exs" diff --git a/lib/bamboo/postmark_adapter.ex b/lib/bamboo/postmark_adapter.ex index 29e1bd1..1b55978 100644 --- a/lib/bamboo/postmark_adapter.ex +++ b/lib/bamboo/postmark_adapter.ex @@ -1,4 +1,18 @@ defmodule Bamboo.PostmarkAdapter do + @moduledoc """ + Sends email using Postmarks's API. + + Use this adapter to send emails through Postmark's API. Requires that an API + key is set in the config. + + ## Example config + + # In config/config.exs, or config.prod.exs, etc. + config :my_app, MyApp.Mailer, + adapter: Bamboo.PostmarkAdapter, + api_key: "my_api_key" + """ + @behaviour Bamboo.Adapter @default_base_uri "https://api.postmarkapp.com" @@ -29,8 +43,8 @@ defmodule Bamboo.PostmarkAdapter do Add the following configuration to your elixir_buildpack.config: config_vars_to_export=( - DATABASE_URL - POSTMARK_API_KEY + DATABASE_URL + POSTMARK_API_KEY ) """ %ApiError{message: message} diff --git a/lib/bamboo/postmark_helper.ex b/lib/bamboo/postmark_helper.ex index 01ba0d5..0f22790 100644 --- a/lib/bamboo/postmark_helper.ex +++ b/lib/bamboo/postmark_helper.ex @@ -11,6 +11,7 @@ defmodule Bamboo.PostmarkHelper do A convenience function for `put_private(email, :tag, "my-tag")` ## Example + tag(email, "welcome-email") """ def tag(email, tag) do @@ -26,6 +27,7 @@ defmodule Bamboo.PostmarkHelper do Postmarks's API docs for this can be found [here](https://www.mandrillapp.com/api/docs/messages.JSON.html#method=send-template). ## Example + template(email, "9746128") template(email, "9746128", %{"name" => "Name", "content" => "John"}) """ @@ -36,16 +38,27 @@ defmodule Bamboo.PostmarkHelper do end @doc """ - Put extra message parameters that are used by Postmark. You can set things like TrackOpens or TrackLinks. + Put extra message parameters that are used by Postmark. You can set things + like TrackOpens, TrackLinks or Attachments. ## Example - put_params(email, "TrackLinks", "HtmlAndText") - put_params(email, "TrackOpens", true) + + put_param(email, "TrackLinks", "HtmlAndText") + put_param(email, "TrackOpens", true) + put_param(email, "Attachments", [ + %{ + Name: "file.txt", + Content: "/some/file.txt" |> File.read!() |> Base.encode64(), + ContentType: "txt" + } + ]) """ 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) + 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 86e04c5..e309edd 100644 --- a/test/lib/bamboo/postmark_adapter_test.exs +++ b/test/lib/bamboo/postmark_adapter_test.exs @@ -73,7 +73,7 @@ defmodule Bamboo.PostmarkAdapterTest do test "raises if the api key is nil" do assert_raise ArgumentError, ~r/no API key set/, fn -> - new_email(from: "foo@bar.com") |> PostmarkAdapter.deliver(@config_with_bad_key) + PostmarkAdapter.deliver(new_email(from: "foo@bar.com"), @config_with_bad_key) end assert_raise ArgumentError, ~r/no API key set/, fn -> @@ -97,7 +97,7 @@ defmodule Bamboo.PostmarkAdapterTest do end test "deliver/2 makes the request to the right url" do - new_email() |> PostmarkAdapter.deliver(@config) + PostmarkAdapter.deliver(new_email(), @config) assert_receive {:fake_postmark, %{request_path: request_path}} @@ -113,23 +113,25 @@ defmodule Bamboo.PostmarkAdapterTest do end test "deliver/2 sends from, html and text body, subject, and headers" do - email = new_email( - from: {"From", "from@foo.com"}, - subject: "My Subject", - text_body: "TEXT BODY", - html_body: "HTML BODY", - ) - |> Email.put_header("Reply-To", "reply@foo.com") - - email |> PostmarkAdapter.deliver(@config) + email = + [ + from: {"From", "from@foo.com"}, + subject: "My Subject", + text_body: "TEXT BODY", + html_body: "HTML BODY", + ] + |> new_email() + |> Email.put_header("Reply-To", "reply@foo.com") + + PostmarkAdapter.deliver(email, @config) assert_receive {:fake_postmark, %{params: params}} - assert params["From"] == "#{email.from |> elem(0)} <#{email.from |> elem(1)}>" + assert params["From"] == "#{elem(email.from, 0)} <#{elem(email.from, 1)}>" assert params["Subject"] == email.subject assert params["TextBody"] == email.text_body assert params["HtmlBody"] == email.html_body - assert params["Headers"] == [%{"Name" => "Reply-To", - "Value" => "reply@foo.com"}] + assert params["Headers"] == + [%{"Name" => "Reply-To", "Value" => "reply@foo.com"}] end test "deliver/2 correctly formats recipients" do @@ -139,7 +141,7 @@ defmodule Bamboo.PostmarkAdapterTest do bcc: [{"BCC", "bcc@bar.com"}], ) - email |> PostmarkAdapter.deliver(@config) + PostmarkAdapter.deliver(email, @config) assert_receive {:fake_postmark, %{params: params}} assert params["To"] == "To " @@ -148,9 +150,9 @@ defmodule Bamboo.PostmarkAdapterTest do end test "deliver/2 puts template name and empty content" do - email = new_email() |> PostmarkHelper.template("hello") + email = PostmarkHelper.template(new_email(), "hello") - email |> PostmarkAdapter.deliver(@config) + PostmarkAdapter.deliver(email, @config) assert_receive {:fake_postmark, %{params: %{"TemplateId" => template_id, "TemplateModel" => template_model}}} @@ -159,11 +161,11 @@ defmodule Bamboo.PostmarkAdapterTest do end test "deliver/2 puts template name and content" do - email = new_email() |> PostmarkHelper.template("hello", [ + email = PostmarkHelper.template(new_email(), "hello", [ %{name: 'example name', content: 'example content'} ]) - email |> PostmarkAdapter.deliver(@config) + PostmarkAdapter.deliver(email, @config) assert_receive {:fake_postmark, %{params: %{"TemplateId" => template_id, "TemplateModel" => template_model}}} @@ -173,18 +175,21 @@ defmodule Bamboo.PostmarkAdapterTest do end test "deliver/2 puts tag param" do - email = new_email() |> PostmarkHelper.tag("some_tag") - email |> PostmarkAdapter.deliver(@config) + email = PostmarkHelper.tag(new_email(), "some_tag") + + PostmarkAdapter.deliver(email, @config) + assert_receive {:fake_postmark, %{params: %{"Tag" => "some_tag"}}} end test "deliver/2 puts tracking params" do - email = new_email() - |> PostmarkHelper.template("hello") - |> PostmarkHelper.put_param("TrackOpens", true) - |> PostmarkHelper.put_param("TrackLinks", "HtmlOnly") + email = + new_email() + |> PostmarkHelper.template("hello") + |> PostmarkHelper.put_param("TrackOpens", true) + |> PostmarkHelper.put_param("TrackLinks", "HtmlOnly") - email |> PostmarkAdapter.deliver(@config) + PostmarkAdapter.deliver(email, @config) assert_receive {:fake_postmark, %{params: %{ "TrackLinks" => "HtmlOnly", "TrackOpens" => true, "TemplateId" => "hello"} @@ -195,12 +200,14 @@ defmodule Bamboo.PostmarkAdapterTest do email = new_email(from: "INVALID_EMAIL") assert_raise Bamboo.PostmarkAdapter.ApiError, fn -> - email |> PostmarkAdapter.deliver(@config) + PostmarkAdapter.deliver(email, @config) end end defp new_email(attrs \\ []) do - attrs = Keyword.merge([from: "foo@bar.com", to: []], attrs) - Email.new_email(attrs) |> Bamboo.Mailer.normalize_addresses + [from: "foo@bar.com", to: []] + |> Keyword.merge(attrs) + |> Email.new_email() + |> Bamboo.Mailer.normalize_addresses() end end