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

Attachments: Support file data in attachment struct + mailgun attachment support #292

Merged
merged 3 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/bamboo/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ defmodule Bamboo.Email do
#...
end
"""
def put_attachment(%__MODULE__{attachments: attachments} = email, %Attachment{filename: _filename, data: _data} = attachment) do
def put_attachment(%__MODULE__{attachments: attachments} = email, %Attachment{filename: nil}) do
raise "You must provide a filename for the attachment."
end
def put_attachment(%__MODULE__{attachments: attachments} = email, %Attachment{data: nil}) do
raise "The attachment must contain data."
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you include the attachment in both error message so that it's easier to see what they passed in?

raise "The attachment must contain data, instead got: #{inspect attachment}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! Done. Also cleaned up a bit. There were some warnings about not used variables.

end
def put_attachment(%__MODULE__{attachments: attachments} = email, %Attachment{} = attachment) do
%{email | attachments: [attachment | attachments]}
end

Expand Down
26 changes: 22 additions & 4 deletions test/lib/bamboo/email_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,29 @@ defmodule Bamboo.EmailTest do
assert email.private["foo"] == "bar"
end

test "put_attachment/2 adds an attachment to the attachments list" do
attachment = %Bamboo.Attachment{filename: "attachment.docx", data: "content"}
email = new_email() |> put_attachment(attachment)
describe "put_attachment/2" do
test "adds an attachment to the attachments list" do
attachment = %Bamboo.Attachment{filename: "attachment.docx", data: "content"}
email = new_email() |> put_attachment(attachment)

assert [%Bamboo.Attachment{filename: "attachment.docx"}] = email.attachments
assert [%Bamboo.Attachment{filename: "attachment.docx"}] = email.attachments
end

test "with no filename throws an error" do
attachment = %Bamboo.Attachment{filename: nil, data: "content"}

assert_raise RuntimeError, "You must provide a filename for the attachment.", fn ->
new_email() |> put_attachment(attachment)
end
end

test "with no data throws an error" do
attachment = %Bamboo.Attachment{filename: "attachment.docx", data: nil}

assert_raise RuntimeError, "The attachment must contain data.", fn ->
new_email() |> put_attachment(attachment)
end
end
end

test "put_attachment/3 adds an attachment to the attachments list" do
Expand Down