Skip to content

Commit

Permalink
refactor: incorporate Credo changes
Browse files Browse the repository at this point in the history
  • Loading branch information
doomspork committed Jun 26, 2024
1 parent 9c3cf81 commit eca5aa9
Show file tree
Hide file tree
Showing 41 changed files with 485 additions and 614 deletions.
5 changes: 3 additions & 2 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
[
order:
~w(moduledoc behaviour use import require alias module_attribute defstruct callback macrocallback optional_callback)a,
ignore: [:type]
ignore: [:type],
ignore_module_attributes: [:tag, :trace]
]},
{Credo.Check.Readability.StringSigils, []},
{Credo.Check.Readability.TrailingBlankLine, []},
Expand Down Expand Up @@ -158,7 +159,7 @@
{Credo.Check.Warning.MapGetUnsafePass, []},
# disabling this check by default, as if not included, it will be
# run on version 1.7.0 and above
{Credo.Check.Warning.MissedMetadataKeyInLoggerConfig, false},
{Credo.Check.Warning.MissedMetadataKeyInLoggerConfig, []},
{Credo.Check.Warning.MixEnv, []},
{Credo.Check.Warning.OperationOnSameValues, []},
{Credo.Check.Warning.OperationWithConstantResult, []},
Expand Down
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],
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.

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
60 changes: 29 additions & 31 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,10 +243,7 @@ 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
Expand All @@ -252,14 +252,12 @@ defmodule Bamboo.MailgunAdapter do
{"", 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
42 changes: 21 additions & 21 deletions lib/bamboo/adapters/mandrill_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ defmodule Bamboo.MandrillAdapter do
use Bamboo.Mailer, otp_app: :my_app
end
"""
@behaviour Bamboo.Adapter

import Bamboo.ApiError

alias Bamboo.AdapterHelper

@service_name "Mandrill"
@default_base_uri "https://mandrillapp.com"
@send_message_path "api/1.0/messages/send.json"
@send_message_template_path "api/1.0/messages/send-template.json"
@behaviour Bamboo.Adapter

alias Bamboo.AdapterHelper
import Bamboo.ApiError
@service_name "Mandrill"

def deliver(email, config) do
api_key = get_key(config)
Expand Down Expand Up @@ -81,8 +82,7 @@ defmodule Bamboo.MandrillAdapter do
end

defp convert_to_mandrill_params(email, api_key) do
%{key: api_key, message: message_params(email)}
|> maybe_put_template_params(email)
maybe_put_template_params(%{key: api_key, message: message_params(email)}, email)
end

defp maybe_put_template_params(params, %{
Expand All @@ -97,8 +97,8 @@ defmodule Bamboo.MandrillAdapter do

defp message_params(email) do
%{
from_name: email.from |> elem(0),
from_email: email.from |> elem(1),
from_name: elem(email.from, 0),
from_email: elem(email.from, 1),
to: recipients(email),
subject: email.subject,
text: email.text_body,
Expand All @@ -113,7 +113,7 @@ defmodule Bamboo.MandrillAdapter do
{images, files} =
attachments
|> Enum.reverse()
|> Enum.split_with(&is_inline_image?/1)
|> Enum.split_with(&inline_image?/1)

mandrill_message
|> Map.put(:attachments, format_attachments(files))
Expand All @@ -130,7 +130,7 @@ defmodule Bamboo.MandrillAdapter do

defp format_attachments(attachments) do
Enum.map(attachments, fn attachment ->
name = if is_inline_image?(attachment), do: attachment.content_id, else: attachment.filename
name = if inline_image?(attachment), do: attachment.content_id, else: attachment.filename

%{
name: name,
Expand All @@ -140,10 +140,10 @@ defmodule Bamboo.MandrillAdapter do
end)
end

defp is_inline_image?(%_{content_type: "image/" <> _, content_id: cid}) when not is_nil(cid),
defp inline_image?(%_{content_type: "image/" <> _, content_id: cid}) when is_binary(cid),
do: true

defp is_inline_image?(_), do: false
defp inline_image?(_), do: false

defp recipients(email) do
[]
Expand All @@ -154,14 +154,14 @@ defmodule Bamboo.MandrillAdapter do

defp add_recipients(recipients, new_recipients, type: recipient_type) do
Enum.reduce(new_recipients, recipients, fn recipient, recipients ->
recipients ++
[
%{
name: recipient |> elem(0),
email: recipient |> elem(1),
type: recipient_type
}
]
[
%{
name: elem(recipient, 0),
email: elem(recipient, 1),
type: recipient_type
}
| recipients
]
end)
end

Expand Down
5 changes: 3 additions & 2 deletions lib/bamboo/adapters/mandrill_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ defmodule Bamboo.MandrillHelper do
}
end)

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

defp merge_vars(e, fun) do
fun.(e)
e
|> fun.()
|> Enum.map(fn {key, value} ->
%{
name: to_string(key),
Expand Down
Loading

0 comments on commit eca5aa9

Please sign in to comment.