Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mandrill Merge Vars #219

Merged
merged 4 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lib/bamboo/adapters/mandrill_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,59 @@ defmodule Bamboo.MandrillHelper do
email |> Email.put_private(:message_params, %{}) |> put_param(key, value)
end

@doc """
Set merge_vars that are used by Mandrill

A convenience function for:

email
|> put_param(email, "merge_vars", [
%{
rcpt: "user1@example.com",
vars: [
%{
"name": "full_name",
"content": "User 1"
}
]
},
%{
rcpt: "user2@example.com",
vars: [
%{
"name": "full_name",
"content": "User 2"
}
]
}
])

## Example
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving the example above the "A convenience function for:"


email
|> put_merge_vars(users, fn(user) -> %{first_name: user.first_name} end)
"""
def put_merge_vars(email, enumerable, fun) do
merge_vars = Enum.map(enumerable, fn(e) ->
%{
rcpt: e.email,
vars: merge_vars(e, fun)
}
end)

email |> put_param("merge_vars", merge_vars)
end

defp merge_vars(e, fun) do
fun.(e)
|> Enum.map(fn({ key, value }) ->
%{
"name": to_string(key),
"content": value
}
end)
end

@doc """
Set a single tag or multiple tags for an email.

Expand Down
38 changes: 38 additions & 0 deletions test/lib/bamboo/adapters/mandrill_helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@ defmodule Bamboo.MandrillHelperTest do
assert email.private.message_params == %{"track_links" => true}
end

test "put_merge_vars/3 puts a list of merge_vars in private.merge_vars" do
users = [
%{
email: "user1@example.com",
full_name: "User 1"
},
%{
email: "user2@example.com",
full_name: "User 2"
}
]

email = new_email
|> MandrillHelper.put_merge_vars(users, fn(user) -> %{ full_name: user.full_name } end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you indent this two spaces? Either that or put it on the same line and make the function multiline like this:

email = MandrillHelper.put_merge_vars email, users, fn(user) ->
  %{full_name: user.full_name}
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still trying to get used to the code standards x)

Fixed.


assert email.private.message_params == %{"merge_vars" => [
%{
rcpt: "user1@example.com",
vars: [
%{
"name": "full_name",
"content": "User 1"
}
]
},
%{
rcpt: "user2@example.com",
vars: [
%{
"name": "full_name",
"content": "User 2"
}
]
}
]
}
end

test "adds tags to mandrill emails" do
email = new_email |> MandrillHelper.tag("welcome-email")
assert email.private.message_params == %{"tags" => ["welcome-email"]}
Expand Down