Skip to content
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[locals_without_parens: [plug: :*]]
17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Add ``mandrill`` to your dependencies in your ``mix.exs``.
```Elixir
defp deps do
[...
{:mandrill, "~> 0.4"}]
{:mandrill, "~> 0.7"}]
end
```

Expand All @@ -20,12 +20,6 @@ end
> `:mandrill, :key`, or the `MANDRILL_KEY` environment variable.

```elixir
iex> Mandrill.start
:ok

iex> Mandrill.key
"your_key"

iex> Mandrill.Users.info
[username: "your_username", created_at: "2013-12-05 00:24:19.47554",
public_id: "your_public_id", reputation: 0, hourly_quota: 25,
Expand Down Expand Up @@ -57,13 +51,8 @@ iex> Mandrill.Users.info[:username]

See [Mandrill's API docs](https://mandrillapp.com/api/docs/).

## Dependencies

- [HTTPoison](https://github.com/edgurgel/httpoison)
- [JSEX](https://github.com/talentdeficit/jsex)

## License

See [LICENSE](https://github.com/slogsdon/mandrill/blob/master/LICENSE)
See [LICENSE](https://github.com/slogsdon/mandrill-elixir/blob/master/LICENSE)

[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/949f7db1a2574d19a36cf9a21a760a6a "githalytics.com")](http://githalytics.com/slogsdon/mandrill)
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/949f7db1a2574d19a36cf9a21a760a6a "githalytics.com")](http://githalytics.com/slogsdon/mandrill-elixir)
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Config

config :tesla, adapter: Tesla.Adapter.Hackney
40 changes: 12 additions & 28 deletions lib/mandrill.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,12 @@ defmodule Mandrill do
An HTTP client for Mandrill.
"""

# Let's build on top of HTTPoison
use Application
use HTTPoison.Base
defmodule Client do
@moduledoc false
use Tesla

def start(_type, _args) do
Mandrill.Supervisor.start_link
end

@doc """
Creates the URL for our endpoint.
Args:
* endpoint - part of the API we're hitting
Returns string
"""
def process_url(endpoint) do
"https://mandrillapp.com/api/1.0/" <> endpoint <> ".json"
end

@doc """
Converts the binary keys in our response to strings.
Args:
* body - string binary response
Returns Record or ArgumentError
"""
def process_response_body(body) do
JSX.decode!(body)
plug Tesla.Middleware.BaseUrl, "https://mandrillapp.com/api/1.0/"
plug Tesla.Middleware.JSON
end

@doc """
Expand All @@ -39,7 +19,8 @@ defmodule Mandrill do
Returns dict
"""
def request(endpoint, body) do
Mandrill.post!(endpoint, JSX.encode! body).body
body = json_library().encode!(Map.new(body))
Client.post!(endpoint <> ".json", body).body
end

@doc """
Expand All @@ -48,7 +29,10 @@ defmodule Mandrill do
Returns binary
"""
def key do
Application.get_env(:mandrill, :key) ||
System.get_env("MANDRILL_KEY")
Application.get_env(:mandrill, :key) || System.get_env("MANDRILL_KEY")
end

def json_library do
Application.get_env(:mandrill, :json_library, Jason)
end
end
32 changes: 20 additions & 12 deletions lib/mandrill/exports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ defmodule Mandrill.Exports do
format for that job type.
"""
def info(params) when is_list(params) do
Mandrill.request("exports/info", Enum.concat([key: Mandrill.key], params))
Mandrill.request("exports/info", Enum.concat([key: Mandrill.key()], params))
end

def info(id) do
params = [
key: Mandrill.key,
key: Mandrill.key(),
id: id
]

Mandrill.request("exports/info", params)
end

@doc """
Returns a list of your exports.
"""
def list do
params = [ key: Mandrill.key ]
params = [key: Mandrill.key()]
Mandrill.request("exports/list", params)
end

Expand All @@ -43,13 +45,15 @@ defmodule Mandrill.Exports do
expires_at.
"""
def rejects(params) when is_list(params) do
Mandrill.request("exports/rejects", Enum.concat([key: Mandrill.key], params))
Mandrill.request("exports/rejects", Enum.concat([key: Mandrill.key()], params))
end

def rejects(notify_email) do
params = [
key: Mandrill.key,
params = [
key: Mandrill.key(),
notify_email: notify_email
]

Mandrill.request("exports/rejects", params)
end

Expand All @@ -62,13 +66,15 @@ defmodule Mandrill.Exports do
created_at.
"""
def whitelist(params) when is_list(params) do
Mandrill.request("exports/whitelist", Enum.concat([key: Mandrill.key], params))
Mandrill.request("exports/whitelist", Enum.concat([key: Mandrill.key()], params))
end

def whitelist(notify_email) do
params = [
key: Mandrill.key,
params = [
key: Mandrill.key(),
notify_email: notify_email
]

Mandrill.request("exports/whitelist", params)
end

Expand All @@ -86,11 +92,12 @@ defmodule Mandrill.Exports do
included in the exported data.
"""
def activity(params) when is_list(params) do
Mandrill.request("exports/activity", Enum.concat([key: Mandrill.key], params))
Mandrill.request("exports/activity", Enum.concat([key: Mandrill.key()], params))
end

def activity(notify_email, date_from, date_to, tags, senders, states, api_keys) do
params = [
key: Mandrill.key,
params = [
key: Mandrill.key(),
notify_email: notify_email,
date_from: date_from,
date_to: date_to,
Expand All @@ -99,6 +106,7 @@ defmodule Mandrill.Exports do
states: states,
api_keys: api_keys
]

Mandrill.request("exports/activity", params)
end
end
78 changes: 47 additions & 31 deletions lib/mandrill/inbound.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ defmodule Mandrill.Inbound do
for inbound delivery
"""
def domains do
params = [ key: Mandrill.key ]
params = [key: Mandrill.key()]
Mandrill.request("inbound/domains", params)
end

@doc """
Add an inbound domain to your account
"""
def add_domain(params) when is_list(params) do
Mandrill.request("inbound/add_domain", Enum.concat([key: Mandrill.key], params))
Mandrill.request("inbound/add_domain", Enum.concat([key: Mandrill.key()], params))
end

def add_domain(domain) do
params = [
key: Mandrill.key,
domain: domain
params = [
key: Mandrill.key(),
domain: domain
]

Mandrill.request("inbound/add-domain", params)
end

Expand All @@ -32,13 +34,15 @@ defmodule Mandrill.Inbound do
with the add-domain call
"""
def check_domain(params) when is_list(params) do
Mandrill.request("inbound/check-domain", Enum.concat([key: Mandrill.key], params))
Mandrill.request("inbound/check-domain", Enum.concat([key: Mandrill.key()], params))
end

def check_domain(domain) do
params = [
key: Mandrill.key,
domain: domain
params = [
key: Mandrill.key(),
domain: domain
]

Mandrill.request("inbound/check-domain", params)
end

Expand All @@ -48,13 +52,15 @@ defmodule Mandrill.Inbound do
immediately.
"""
def delete_domain(params) when is_list(params) do
Mandrill.request("inbound/delete-domain", Enum.concat([key: Mandrill.key], params))
Mandrill.request("inbound/delete-domain", Enum.concat([key: Mandrill.key()], params))
end

def delete_domain(domain) do
params = [
key: Mandrill.key,
domain: domain
params = [
key: Mandrill.key(),
domain: domain
]

Mandrill.request("inbound/delete-domain", params)
end

Expand All @@ -63,29 +69,33 @@ defmodule Mandrill.Inbound do
inbound domain
"""
def routes(params) when is_list(params) do
Mandrill.request("inbound/routes", Enum.concat([key: Mandrill.key], params))
Mandrill.request("inbound/routes", Enum.concat([key: Mandrill.key()], params))
end

def routes(domain) do
params = [
key: Mandrill.key,
domain: domain
params = [
key: Mandrill.key(),
domain: domain
]

Mandrill.request("inbound/routes", params)
end

@doc """
Add a new mailbox route to an inbound domain
"""
def add_route(params) when is_list(params) do
Mandrill.request("inbound/add-route", Enum.concat([key: Mandrill.key], params))
Mandrill.request("inbound/add-route", Enum.concat([key: Mandrill.key()], params))
end

def add_route(domain, pattern, url) do
params = [
key: Mandrill.key,
params = [
key: Mandrill.key(),
domain: domain,
pattern: pattern,
url: url
]

Mandrill.request("inbound/add-route", params)
end

Expand All @@ -96,29 +106,33 @@ defmodule Mandrill.Inbound do
unchanged.
"""
def update_route(params) when is_list(params) do
Mandrill.request("inbound/update-route", Enum.concat([key: Mandrill.key], params))
Mandrill.request("inbound/update-route", Enum.concat([key: Mandrill.key()], params))
end

def update_route(id, pattern, url) do
params = [
key: Mandrill.key,
params = [
key: Mandrill.key(),
id: id,
pattern: pattern,
url: url
]

Mandrill.request("inbound/update-route", params)
end

@doc """
Delete an existing inbound mailbox route
"""
def delete_route(params) when is_list(params) do
Mandrill.request("inbound/delete-route", Enum.concat([key: Mandrill.key], params))
Mandrill.request("inbound/delete-route", Enum.concat([key: Mandrill.key()], params))
end

def delete_route(id) do
params = [
key: Mandrill.key,
id: id
params = [
key: Mandrill.key(),
id: id
]

Mandrill.request("inbound/delete-route", params)
end

Expand All @@ -129,17 +143,19 @@ defmodule Mandrill.Inbound do
been sent over SMTP
"""
def send_raw(params) when is_list(params) do
Mandrill.request("inbound/send-raw", Enum.concat([key: Mandrill.key], params))
Mandrill.request("inbound/send-raw", Enum.concat([key: Mandrill.key()], params))
end

def send_raw(raw_message, to \\ nil, mail_from, helo, client_address) do
params = [
key: Mandrill.key,
params = [
key: Mandrill.key(),
raw_message: raw_message,
to: to,
mail_from: mail_from,
helo: helo,
client_address: client_address
client_address: client_address
]

Mandrill.request("inbound/send-raw", params)
end
end
Loading