aboutsummaryrefslogtreecommitdiff
path: root/lib/list.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/list.ex')
-rw-r--r--lib/list.ex88
1 files changed, 52 insertions, 36 deletions
diff --git a/lib/list.ex b/lib/list.ex
index 8589eb2..81853e4 100644
--- a/lib/list.ex
+++ b/lib/list.ex
@@ -1,47 +1,63 @@
defmodule Mailchimp.List do
- import Mailchimp.HTTPClient
+ alias HTTPoison.Response
+ alias Mailchimp.HTTPClient
+ alias Mailchimp.Link
+ alias Mailchimp.Member
- def all(config) do
- map_header = %{"Authorization" => "apikey #{config.apikey}"}
- config.apiroot
- |> build_url
- |> get(map_header)
- end
+ defstruct id: nil, name: nil, contact: nil, permission_reminder: nil, use_archive_bar: nil, campaign_defaults: nil, notify_on_subscribe: nil, notify_on_unsubscribe: nil, list_rating: nil, email_type_option: nil, subscribe_url_short: nil, subscribe_url_long: nil, beamer_address: nil, visibility: nil, modules: [], stats: nil, links: []
- def members(config, list_id) do
- map_header = %{"Authorization" => "apikey #{config.apikey}"}
- config.apiroot <> "lists/" <> list_id <> "/members"
- |> get(map_header)
+ def new(attributes) do
+ %__MODULE__{
+ id: attributes[:id],
+ name: attributes[:name],
+ contact: attributes[:contact],
+ permission_reminder: attributes[:permission_reminder],
+ use_archive_bar: attributes[:use_archive_bar],
+ campaign_defaults: attributes[:campaign_defaults],
+ notify_on_subscribe: attributes[:notify_on_subscribe],
+ notify_on_unsubscribe: attributes[:notify_on_unsubscribe],
+ list_rating: attributes[:list_rating],
+ email_type_option: attributes[:email_type_option],
+ subscribe_url_short: attributes[:subscribe_url_short],
+ subscribe_url_long: attributes[:subscribe_url_long],
+ beamer_address: attributes[:beamer_address],
+ visibility: attributes[:visibility],
+ modules: attributes[:modules],
+ stats: attributes[:stats],
+ links: Link.get_links_from_attributes(attributes)
+ }
end
- def add_member(config, %{"list_id" => list_id, "email" => email}) do
- map_header = %{"Authorization" => "apikey #{config.apikey}"}
- {:ok, body} = Poison.encode(%{email_address: email, status: "subscribed"})
- config.apiroot <> "lists/" <> list_id <> "/members"
- |> post(body, map_header)
- end
+ def members(%__MODULE__{links: %{"members" => %Link{href: href}}}) do
+ {:ok, response} = HTTPClient.get(href)
+ case response do
+ %Response{status_code: 200, body: body} ->
+ {:ok, Enum.map(body.members, &Member.new(&1))}
- def add_pending_member(config, %{"list_id" => list_id, "email" => email}) do
- map_header = %{"Authorization" => "apikey #{config.apikey}"}
- {:ok, body} = Poison.encode(%{email_address: email, status: "pending"})
- config.apiroot <> "lists/" <> list_id <> "/members"
- |> post(body, map_header)
+ %Response{status_code: _, body: body} ->
+ {:error, body}
+ end
end
- def build_url(root) do
- params = [
- {:fields, ["lists.id", "lists.name", "lists.stats.member_count"]},
- {:count, 10},
- {:offset, 0}
- ]
- fields = "fields=" <> as_string(params[:fields])
- count = "count=" <> to_string params[:count]
- offset = "offset=" <> to_string params[:offset]
- url = root <> "lists?" <> fields <> "&" <> count <> "&" <> offset
- end
+ def create_member(%__MODULE__{links: %{"members" => %Link{href: href}}}, email_address, status, merge_fields \\ %{}) when is_binary(email_address) and is_map(merge_fields) and status in [:subscribed, :pending, :unsubscribed, :cleaned] do
+ {:ok, response} = HTTPClient.get(href)
+ case response do
+ %Response{status_code: 200, body: body} ->
+ links = Link.get_links_from_attributes(body)
+ href = links["create"].href
+ data = %{email_address: email_address, status: status, merge_fields: merge_fields}
- def as_string(param) do
- Enum.reduce(param, fn(s, acc) -> acc<>","<>s end)
- end
+ {:ok, response} = HTTPClient.post href, Poison.encode!(data)
+ case response do
+ %Response{status_code: 200, body: body} ->
+ {:ok, Member.new(body)}
+
+ %Response{status_code: _, body: body} ->
+ {:error, body}
+ end
+ %Response{status_code: _, body: body} ->
+ {:error, body}
+ end
+ end
end