diff options
author | Rawane Zossou <dev@raw1z.fr> | 2015-09-19 03:57:04 +0200 |
---|---|---|
committer | Rawane Zossou <dev@raw1z.fr> | 2015-09-19 03:57:04 +0200 |
commit | d46896cf276579618afb9b6ad46100f78b77bb25 (patch) | |
tree | 2b053f0e2f895545c9448fd91a6912effb7e060b /lib | |
parent | cedebfbae736e26d5d96129bb46dd868de8e1224 (diff) | |
download | mailchimp-d46896cf276579618afb9b6ad46100f78b77bb25.tar.gz mailchimp-d46896cf276579618afb9b6ad46100f78b77bb25.tar.xz |
refactor the list members related apis
Diffstat (limited to 'lib')
-rw-r--r-- | lib/list.ex | 35 | ||||
-rw-r--r-- | lib/member.ex | 30 |
2 files changed, 65 insertions, 0 deletions
diff --git a/lib/list.ex b/lib/list.ex index 4fff8a1..81853e4 100644 --- a/lib/list.ex +++ b/lib/list.ex @@ -1,6 +1,8 @@ defmodule Mailchimp.List do + alias HTTPoison.Response alias Mailchimp.HTTPClient alias Mailchimp.Link + alias Mailchimp.Member 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: [] @@ -25,4 +27,37 @@ defmodule Mailchimp.List do links: Link.get_links_from_attributes(attributes) } 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))} + + %Response{status_code: _, body: body} -> + {:error, body} + end + 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} + + {: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 diff --git a/lib/member.ex b/lib/member.ex new file mode 100644 index 0000000..3f49722 --- /dev/null +++ b/lib/member.ex @@ -0,0 +1,30 @@ +defmodule Mailchimp.Member do + alias Mailchimp.Link + + defstruct email_address: nil, email_client: nil, email_type: nil, id: nil, ip_opt: nil, ip_signup: nil, language: nil, last_changed: nil, list_id: nil, location: nil, member_rating: nil, merge_fields: nil, stats: nil, status: nil, status_if_new: nil, timestamp_opt: nil, timestamp_signup: nil, unique_email_id: nil, vip: nil + + def new(attributes) do + %{ + email_address: attributes[:email_address], + email_client: attributes[:email_client], + email_type: attributes[:email_type], + id: attributes[:id], + ip_opt: attributes[:ip_opt], + ip_signup: attributes[:ip_signup], + language: attributes[:language], + last_changed: attributes[:last_changed], + list_id: attributes[:list_id], + location: attributes[:location], + member_rating: attributes[:member_rating], + merge_fields: attributes[:merge_fields], + stats: attributes[:stats], + status: attributes[:status], + status_if_new: attributes[:status_if_new], + timestamp_opt: attributes[:timestamp_opt], + timestamp_signup: attributes[:timestamp_signup], + unique_email_id: attributes[:unique_email_id], + vip: attributes[:vip], + links: Link.get_links_from_attributes(attributes) + } + end +end |