Skip to content

Commit

Permalink
Updating Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarvsilva committed Mar 24, 2022
1 parent 8e143bf commit f1b858f
Show file tree
Hide file tree
Showing 19 changed files with 588 additions and 79 deletions.
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

config :mailchimp,
api_key: "your apikey-us19"
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use Mix.Config
import Config
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

config :mailchimp,
api_key: System.get_env("MAILCHIMP_TEST_API_KEY", "your apikey-us19")
55 changes: 47 additions & 8 deletions lib/mailchimp/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,38 @@ defmodule Mailchimp.Account do
alias Mailchimp.Link
alias Mailchimp.List

defstruct account_id: nil,
account_name: nil,
contact: nil,
last_login: nil,
total_subscribers: 0,
links: []
@moduledoc """
Details about the Mailchimp user account.
### Struct Fields
* `account_id` - The Mailchimp account ID.
* `account_name` - The name of the account.
* `contact` - Information about the account contact.
* `last_login` - The date and time of the last login for this account in ISO 8601 format.
* `total_subscribers` - The total number of subscribers across all lists in the account.
* `links` - A list of `Mailchimp.Link` types and descriptions for the API schema documents.
"""


defstruct [
account_id: nil,
account_name: nil,
contact: nil,
last_login: nil,
total_subscribers: 0,
links: []
]

@doc """
Generates an `Mailchimp.Account` struct from the given attributes.
"""
def new(attributes) do
%__MODULE__{
account_id: attributes[:account_id],
Expand All @@ -22,10 +47,11 @@ defmodule Mailchimp.Account do
}
end

@doc false
def get(override_url \\ "/") do
case HTTPClient.get(override_url) do
{:ok, %Response{status_code: 200, body: body}} ->
{:ok, __MODULE__.new(body)}
{:ok, new(body)}

{:ok, %Response{status_code: _, body: body}} ->
{:error, body}
Expand All @@ -35,11 +61,15 @@ defmodule Mailchimp.Account do
end
end

@doc false
def get!(override_url \\ "/") do
{:ok, account} = get(override_url)
account
end

@doc """
Fetch a list of `Mailchimp.List` for the given account with an optional query params.
"""
def lists(%__MODULE__{links: %{"lists" => %Link{href: href}}}, query_params \\ %{}) do
{:ok, response} = HTTPClient.get(href, [], params: query_params)

Expand All @@ -52,13 +82,18 @@ defmodule Mailchimp.Account do
end
end

@doc """
Same as `lists/2`
but raises errors.
"""
def lists!(account, query_params \\ %{}) do
{:ok, lists} = lists(account, query_params)
lists
end


@doc """
Fetch a List from an ID
Fetch a `Mailchimp.List` from an ID
"""
def get_list(%__MODULE__{links: %{"lists" => %Link{href: href}}}, list_id, query_params \\ %{}) do
{:ok, response} = HTTPClient.get(href <> "/#{list_id}", [], params: query_params)
Expand All @@ -72,6 +107,10 @@ defmodule Mailchimp.Account do
end
end

@doc """
Same as `get_list/3`
but raises errors.
"""
def get_list!(account, list_id, query_params \\ %{}) do
{:ok, list} = get_list(account, list_id, query_params)
list
Expand Down
18 changes: 17 additions & 1 deletion lib/mailchimp/batch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ defmodule Mailchimp.Batch do
alias HTTPoison.Response
alias Mailchimp.HTTPClient

@moduledoc """
Use batch operations to complete multiple operations with a single call.
"""

@doc """
Begin processing a batch operations request.
"""
def create_batch(operations) when is_list(operations) do
case HTTPClient.post("/batches", Jason.encode!(%{operations: operations})) do
{:ok, %Response{status_code: 200, body: body}} ->
Expand All @@ -17,6 +24,9 @@ defmodule Mailchimp.Batch do

def create_batch(_operations), do: {:error, "No batch given"}

@doc """
Get the status of a batch request.
"""
def get_batch(batch_id) do
case HTTPClient.get("/batches/" <> batch_id) do
{:ok, %Response{status_code: 200, body: body}} ->
Expand All @@ -29,7 +39,9 @@ defmodule Mailchimp.Batch do
{:error, error}
end
end

@doc """
Get a summary of batch requests that have been made.
"""
def batches(query_params \\ %{}) do
case HTTPClient.get("/batches", [], params: query_params) do
{:ok, %Response{status_code: 200, body: body}} ->
Expand All @@ -43,6 +55,10 @@ defmodule Mailchimp.Batch do
end
end

@doc """
Stops a batch request from running. Since only one batch request is run at a time, this can be used to cancel a long running request.
The results of any completed operations will not be available after this call.
"""
def destroy_batch(batch_id) do
case HTTPClient.delete("/batches/" <> batch_id) do
{:ok, %Response{status_code: 204, body: body}} ->
Expand Down
76 changes: 75 additions & 1 deletion lib/mailchimp/campaign.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@ defmodule Mailchimp.Campaign do
alias Mailchimp.HTTPClient
alias Mailchimp.Link
alias Mailchimp.Campaign.{Tracking, Content}
@moduledoc """
Campaigns are how you send emails to your Mailchimp list.
Use the Campaigns API calls to manage campaigns in your Mailchimp account.
### Struct Fields
* `id` - A string that uniquely identifies this campaign.
* `web_id` - The ID used in the Mailchimp web application. View this campaign in your Mailchimp account at https://{dc}.admin.mailchimp.com/campaigns/show/?id={web_id}.
* `type` - There are four types of [campaigns](https://mailchimp.com/help/getting-started-with-campaigns/) you can create in Mailchimp. A/B Split campaigns have been deprecated and variate campaigns should be used instead. Possible values: "regular", "plaintext", "absplit", "rss", or "variate".
* `create_time` - The date and time the campaign was created in ISO 8601 format.
* `archive_url` - The link to the campaign's archive version in ISO 8601 format.
* `long_archive_url` - The original link to the campaign's archive version.
* `status` - The current status of the campaign. Possible values: "save", "paused", "schedule", "sending", "sent", "canceled", "canceling", or "archived".
* `emails_sent` - The total number of emails sent for this campaign.
* `send_time` - The date and time a campaign was sent.
* `content_type` - How the campaign's content is put together. Possible values: "template", "html", "url", or "multichannel".
* `recipients` - List settings for the campaign.
* `settings` - The settings for your campaign, including subject, from name, reply-to address, and more.
* `variate_settings` - The settings specific to A/B test campaigns.
* `tracking` - The tracking options for a campaign.
* `rss_opts` - [RSS options](https://mailchimp.com/help/share-your-blog-posts-with-mailchimp/) for a campaign.
* `ab_split_opts` - [A/B Testing options](https://mailchimp.com/help/about-ab-testing-campaigns/) for a campaign.
* `social_card` - The preview for the campaign, rendered by social networks like Facebook and Twitter. [Learn more](https://mailchimp.com/help/enable-and-customize-social-cards/).
* `report_summary` - For sent campaigns, a summary of opens, clicks, and e-commerce data.
* `delivery_status` - Updates on campaigns in the process of sending.
"""

defstruct [
:id,
Expand All @@ -27,6 +73,9 @@ defmodule Mailchimp.Campaign do
:links
]

@doc """
Generates an `Mailchimp.Campaign` struct from the given attributes.
"""
def new(attributes) do
{:ok, create_time, _} = DateTime.from_iso8601(attributes[:create_time])

Expand Down Expand Up @@ -59,8 +108,14 @@ defmodule Mailchimp.Campaign do
}
end

@doc """
Creates an empty list
"""
def list(), do: list([])

@doc """
Fetch campaigns with query params
"""
def list(query_params) do
{:ok, response} = HTTPClient.get("/campaigns", [], params: query_params)

Expand All @@ -73,11 +128,18 @@ defmodule Mailchimp.Campaign do
end
end

@doc """
Same as `list/1`
but raises errors.
"""
def list!(query_params \\ %{}) do
{:ok, campaigns} = list(query_params)
campaigns
end

@doc """
Creates a `Mailchimp.Campaign` with additional fields for attributes and sends it to mailchimp
"""
def create(type, attrs \\ %{}) when type in [:regular, :plaintext, :absplit, :rss, :variate] do
{:ok, response} = HTTPClient.post("/campaigns", Jason.encode!(Map.put(attrs, :type, type)))

Expand All @@ -90,11 +152,19 @@ defmodule Mailchimp.Campaign do
end
end

def create!(type, attrs \\ %{}) do
@doc """
Same as `create/2`
but raises errors.
"""
def create!(type, attrs \\ %{}) when type in [:regular, :plaintext, :absplit, :rss, :variate] do
{:ok, campaign} = create(type, attrs)
campaign
end

@doc """
Fetches the `Mailchimp.Campaign.Content` for a given campaing
"""

def content(%__MODULE__{links: %{"content" => %Link{href: href}}}) do
{:ok, response} = HTTPClient.get(href)

Expand All @@ -107,6 +177,10 @@ defmodule Mailchimp.Campaign do
end
end

@doc """
Same as `content/1`
but raises errors.
"""
def content!(campaign) do
{:ok, content} = content(campaign)
content
Expand Down
4 changes: 1 addition & 3 deletions lib/mailchimp/config.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
defmodule Mailchimp.Config do
@moduledoc """
Mailchimp Config Module.
"""
@moduledoc false

@default_api_version "3.0"
@default_api_endpoint "api.mailchimp.com"
Expand Down
4 changes: 1 addition & 3 deletions lib/mailchimp/http_client.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
defmodule Mailchimp.HTTPClient do
@moduledoc """
Poison HTTP Client for Mailchimp.
"""
@moduledoc false

use HTTPoison.Base

Expand Down
31 changes: 30 additions & 1 deletion lib/mailchimp/link.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
defmodule Mailchimp.Link do
defstruct rel: nil, href: nil, method: nil, schema: nil, target_schema: nil
@moduledoc """
Stores links for a given Entity in Mailchimp's API
### Struct Fields
* `rel` - As with an HTML rel attribute, this describes the type of link.
* `href` - This property contains a fully-qualified URL that can be called to retrieve the linked resource or perform the linked action.
* `method` - The HTTP method that should be used when accessing the URL defined in 'href'. Possible values: "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", or "HEAD".
* `schema` - For HTTP methods that can receive bodies (POST and PUT), this is a URL representing the schema that the body should conform to.
* `target_schema` - For GETs, this is a URL representing the schema that the response should conform to.
"""

defstruct [
rel: nil,
href: nil,
method: nil,
schema: nil,
target_schema: nil
]

@doc """
Generates an `Mailchimp.Link` struct from the given attributes.
"""
def new(attributes) do
%__MODULE__{
rel: attributes[:rel],
Expand All @@ -11,6 +37,9 @@ defmodule Mailchimp.Link do
}
end

@doc """
Generates a list of `Mailchimp.Link` structs from the given links.
"""
def get_links_from_attributes(%{_links: links}) do
links
|> Enum.map(&__MODULE__.new(&1))
Expand Down
Loading

0 comments on commit f1b858f

Please sign in to comment.