Skip to content

Commit

Permalink
Fix code style issues
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pablo-co authored Oct 16, 2017
1 parent 77d0bc9 commit 75d4783
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 68 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 0 additions & 29 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -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"
18 changes: 16 additions & 2 deletions lib/bamboo/postmark_adapter.ex
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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}
Expand Down
21 changes: 17 additions & 4 deletions lib/bamboo/postmark_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"})
"""
Expand All @@ -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
65 changes: 36 additions & 29 deletions test/lib/bamboo/postmark_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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}}

Expand All @@ -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
Expand All @@ -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 <to@bar.com>"
Expand All @@ -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}}}
Expand All @@ -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}}}
Expand All @@ -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"}
Expand All @@ -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

0 comments on commit 75d4783

Please sign in to comment.