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

refactor: modernization #668

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is synced with beam-community/common-config. Any changes will be overwritten.

[
import_deps: [],
import_deps: [:plug],
Copy link
Member Author

Choose a reason for hiding this comment

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

Need to see why common-config didn't detect plug.

inputs: ["*.{heex,ex,exs}", "{config,lib,priv,test}/**/*.{heex,ex,exs}"],
line_length: 120,
plugins: []
Expand Down
67 changes: 0 additions & 67 deletions .github/workflows/ci.yml

This file was deleted.

21 changes: 0 additions & 21 deletions LICENSE.txt

This file was deleted.

21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
![bamboo](https://user-images.githubusercontent.com/22394/39895001-b13a9c9a-5476-11e8-9c58-f5fc5f09b697.png)

# Bamboo [![Circle CI](https://circleci.com/gh/thoughtbot/bamboo/tree/master.svg?style=svg)](https://circleci.com/gh/thoughtbot/bamboo/tree/master) [![Coverage Status](https://coveralls.io/repos/github/thoughtbot/bamboo/badge.png?branch=master)](https://coveralls.io/github/thoughtbot/bamboo?branch=master)
# Bamboo

[![Build](https://github.com/beam-community/bamboo/actions/workflows/ci.yml/badge.svg)](https://github.com/beam-community/bamboo/actions/workflows/ci.yml)

---

**This README follows master, which may not be the currently published version!** Use
**This README follows the main branch, which may not be the currently published version!** Use
[the docs for the published version of Bamboo](https://hexdocs.pm/bamboo/readme.html).

---

**Bamboo is part of the [thoughtbot Elixir family][elixir-phoenix] of projects.**
**Bamboo is part of the [BEAM Community](https://github.com/beam-community) family of projects.**

![thoughtbot](http://presskit.thoughtbot.com/images/thoughtbot-logo-for-readmes.svg)

Thank you to [thoughtbot](https://thoughtbot.com) for making Bamboo and supporting the BEAM community!

Flexible and easy to use email for Elixir.

Expand Down Expand Up @@ -433,12 +439,9 @@ We run the test suite as well as formatter checks on CI. Make sure you are using
the Elixir version defined in the `.tool-versions` file to have consistent
formatting with what's being run on CI.

## About thoughtbot
##

![thoughtbot](http://presskit.thoughtbot.com/images/thoughtbot-logo-for-readmes.svg)

Bamboo is maintained and funded by thoughtbot, inc.
The names and logos for thoughtbot are trademarks of thoughtbot, inc.

We love open-source software, Elixir, and Phoenix. See [our other Elixir
projects][elixir-phoenix], or [hire our Elixir Phoenix development team][hire]
Expand All @@ -465,12 +468,12 @@ Thanks to @mtwilliams for an early version of the `SendGridAdapter`.
[`bamboo.tasksupervisorstrategy`]: https://hexdocs.pm/bamboo/Bamboo.TaskSupervisorStrategy.html
[`bamboo.test`]: https://hexdocs.pm/bamboo/Bamboo.Test.html
[`bamboo.testadapter`]: https://hexdocs.pm/bamboo/Bamboo.TestAdapter.html
[`bamboo`]: http://github.com/thoughtbot/bamboo
[`bamboo`]: http://github.com/beam-community/bamboo
[available-adapters]: #available-adapters
[compose emails using pipes]: #composing-with-pipes
[create your own adapter]: https://hexdocs.pm/bamboo/Bamboo.Adapter.html
[docs]: https://hexdocs.pm/bamboo/readme.html
[exq]: https://github.com/akira/exq
[free bamboo screencasts from ElixirCasts]: https://elixircasts.io/sending-email-with-bamboo-part-1
[jason]: https://github.com/michalmuskala/jason
[toniq]: https://github.com/joakimk/toniq
[toniq]: https://github.com/joakimk/toniq
30 changes: 0 additions & 30 deletions config/config.exs

This file was deleted.

4 changes: 2 additions & 2 deletions lib/bamboo/adapters/local_adapter.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# credo:disable-for-this-file Credo.Check.Warning.LeakyEnvironment
defmodule Bamboo.LocalAdapter do
@moduledoc """
Stores emails locally. Can be queried to see sent emails.
Expand Down Expand Up @@ -25,11 +26,10 @@ defmodule Bamboo.LocalAdapter do
use Bamboo.Mailer, otp_app: :my_app
end
"""
@behaviour Bamboo.Adapter

alias Bamboo.SentEmail

@behaviour Bamboo.Adapter

@doc "Adds email to `Bamboo.SentEmail`, can automatically open it in new browser tab"
def deliver(email, %{open_email_in_browser_url: open_email_in_browser_url}) do
delivered_email = SentEmail.push(email)
Expand Down
63 changes: 30 additions & 33 deletions lib/bamboo/adapters/mailgun_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ defmodule Bamboo.MailgunAdapter do
base_uri: "https://api.eu.mailgun.net/v3"

"""

@service_name "Mailgun"
@default_base_uri "https://api.mailgun.net/v3"
@behaviour Bamboo.Adapter

alias Bamboo.{Email, Attachment, AdapterHelper}
import Bamboo.ApiError

alias Bamboo.AdapterHelper
alias Bamboo.Attachment
alias Bamboo.Email

@default_base_uri "https://api.mailgun.net/v3"
@internal_fields ~w(attachments)a
@mailgun_message_fields ~w(from to cc bcc subject text html template recipient-variables)a
@service_name "Mailgun"

@doc false
def handle_config(config) do
config
Expand All @@ -60,20 +65,21 @@ defmodule Bamboo.MailgunAdapter do
|> Map.put_new(:base_uri, base_uri())
end

defp base_uri() do
defp base_uri do
Application.get_env(:bamboo, :mailgun_base_uri, @default_base_uri)
end

defp get_setting(config, key) do
config[key]
|> case do
{:system, var} ->
System.get_env(var)
config_value =
case config[key] do
{:system, var} ->
System.get_env(var)

value ->
value
end
|> case do
value ->
value
end

case config_value do
value when value in [nil, ""] ->
raise_missing_setting_error(config, key)

Expand All @@ -95,13 +101,10 @@ defmodule Bamboo.MailgunAdapter do
def deliver(email, config) do
body = to_mailgun_body(email)
config = handle_config(config)
uri = full_uri(config)
headers = headers(email, config)

case :hackney.post(
full_uri(config),
headers(email, config),
body,
AdapterHelper.hackney_opts(config)
) do
case :hackney.post(uri, headers, body, AdapterHelper.hackney_opts(config)) do
{:ok, status, _headers, response} when status > 299 ->
body = decode_body(body)
{:error, build_api_error(@service_name, response, body)}
Expand Down Expand Up @@ -171,9 +174,7 @@ defmodule Bamboo.MailgunAdapter do
defp put_bcc(body, %Email{bcc: bcc}), do: Map.put(body, :bcc, prepare_recipients(bcc))

defp prepare_recipients(recipients) do
recipients
|> Enum.map(&prepare_recipient(&1))
|> Enum.join(",")
Enum.map_join(recipients, ",", &prepare_recipient(&1))
end

defp prepare_recipient({nil, address}), do: address
Expand All @@ -190,6 +191,7 @@ defmodule Bamboo.MailgunAdapter do

defp put_headers(body, %Email{headers: headers}) do
Enum.reduce(headers, body, fn {key, value}, acc ->
# credo:disable-for-this-file Credo.Check.Warning.UnsafeToAtom
Map.put(acc, :"h:#{key}", value)
end)
end
Expand Down Expand Up @@ -223,6 +225,7 @@ defmodule Bamboo.MailgunAdapter do
custom_vars = Map.get(private, :mailgun_custom_vars, %{})

Enum.reduce(custom_vars, body, fn {key, value}, acc ->
# credo:disable-for-this-file Credo.Check.Warning.UnsafeToAtom
Map.put(acc, :"v:#{key}", value)
end)
end
Expand All @@ -240,27 +243,21 @@ defmodule Bamboo.MailgunAdapter do
defp put_attachments(body, %Email{attachments: []}), do: body

defp put_attachments(body, %Email{attachments: attachments}) do
attachment_data =
attachments
|> Enum.reverse()
|> Enum.map(&prepare_file(&1))
attachment_data = Enum.map(attachments, &prepare_file(&1))

Map.put(body, :attachments, attachment_data)
end

defp prepare_file(%Attachment{} = attachment) do
{"", attachment.data,
{"form-data", [{"name", ~s/"attachment"/}, {"filename", ~s/"#{attachment.filename}"/}]}, []}
{"", attachment.data, {"form-data", [{"name", ~s/"attachment"/}, {"filename", ~s/"#{attachment.filename}"/}]}, []}
end

@mailgun_message_fields ~w(from to cc bcc subject text html template recipient-variables)a
@internal_fields ~w(attachments)a

def filter_non_empty_mailgun_fields(body) do
Enum.filter(body, fn {key, value} ->
body
|> Enum.filter(fn {key, value} ->
# Key is a well known mailgun field (including header and custom var field) and its value is not empty
(key in @mailgun_message_fields || key in @internal_fields ||
String.starts_with?(Atom.to_string(key), ["h:", "v:", "o:", "t:"])) &&
key |> Atom.to_string() |> String.starts_with?(["h:", "v:", "o:", "t:"])) &&
!(value in [nil, "", []])
end)
|> Enum.into(%{})
Expand Down
2 changes: 1 addition & 1 deletion lib/bamboo/adapters/mailgun_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ defmodule Bamboo.MailgunHelper do
|> MailgunHelper.substitute_variables(%{ "greeting" => "Hello!", "password_reset_link" => "https://example.com/123" })

"""
def substitute_variables(%Email{headers: headers} = email, variables = %{}) do
def substitute_variables(%Email{headers: headers} = email, %{} = variables) do
custom_vars =
headers
|> Map.get(@mailgun_header_for_custom_vars, "{}")
Expand Down
Loading
Loading