aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRawane Zossou <dev@raw1z.fr>2015-09-19 02:49:52 +0200
committerRawane Zossou <dev@raw1z.fr>2015-09-19 02:50:44 +0200
commitcedebfbae736e26d5d96129bb46dd868de8e1224 (patch)
tree72e7a660b6476ea3402ca45f53746d4abf96697c /lib
parent818c1bb791a30a88df54c286a3cb1571845a3d27 (diff)
downloadmailchimp-cedebfbae736e26d5d96129bb46dd868de8e1224.tar.gz
mailchimp-cedebfbae736e26d5d96129bb46dd868de8e1224.tar.xz
refactor the account and list modules
Diffstat (limited to 'lib')
-rw-r--r--lib/account.ex41
-rw-r--r--lib/httpclient.ex7
-rw-r--r--lib/link.ex20
-rw-r--r--lib/list.ex58
4 files changed, 85 insertions, 41 deletions
diff --git a/lib/account.ex b/lib/account.ex
index 586d274..7e24858 100644
--- a/lib/account.ex
+++ b/lib/account.ex
@@ -1,10 +1,41 @@
defmodule Mailchimp.Account do
- import Mailchimp.HTTPClient
+ alias HTTPoison.Response
+ alias Mailchimp.HTTPClient
+ alias Mailchimp.Link
+ alias Mailchimp.List
- def get_details(config) do
- map_header = %{"Authorization" => "apikey #{config.apikey}"}
- url = config.apiroot
- get(url, map_header)
+ defstruct account_id: nil, account_name: nil, contact: nil, last_login: nil, total_subscribers: 0, links: []
+
+ def new(attributes) do
+ %__MODULE__{
+ account_id: attributes[:account_id],
+ account_name: attributes[:account_name],
+ contact: attributes[:contact],
+ last_login: attributes[:last_login],
+ total_subscribers: attributes[:total_subscribers],
+ links: Link.get_links_from_attributes(attributes)
+ }
+ end
+
+ def get do
+ {:ok, response} = HTTPClient.get("/")
+ case response do
+ %Response{status_code: 200, body: body} ->
+ {:ok, __MODULE__.new(body)}
+
+ %Response{status_code: _, body: body} ->
+ {:error, body}
+ end
end
+ def lists(%__MODULE__{links: %{"lists" => %Link{href: href}}}) do
+ {:ok, response} = HTTPClient.get(href)
+ case response do
+ %Response{status_code: 200, body: body} ->
+ {:ok, Enum.map(body.lists, &List.new(&1))}
+
+ %Response{status_code: _, body: body} ->
+ {:error, body}
+ end
+ end
end
diff --git a/lib/httpclient.ex b/lib/httpclient.ex
index e34312f..e84ac2d 100644
--- a/lib/httpclient.ex
+++ b/lib/httpclient.ex
@@ -4,7 +4,12 @@ defmodule Mailchimp.HTTPClient do
alias Mailchimp.Config
def process_url(url) do
- Config.root_endpoint <> url
+ root_endpoint = Config.root_endpoint
+ if String.starts_with?(url, root_endpoint) do
+ url
+ else
+ root_endpoint <> url
+ end
end
def process_response_body(body) do
diff --git a/lib/link.ex b/lib/link.ex
new file mode 100644
index 0000000..fc355b9
--- /dev/null
+++ b/lib/link.ex
@@ -0,0 +1,20 @@
+defmodule Mailchimp.Link do
+ defstruct rel: nil, href: nil, method: nil, schema: nil, target_schema: nil
+
+ def new(attributes) do
+ %__MODULE__{
+ rel: attributes[:rel],
+ href: attributes[:href],
+ method: attributes[:method],
+ schema: attributes[:schema],
+ target_schema: attributes[:targetSchema]
+ }
+ end
+
+ def get_links_from_attributes(attributes) do
+ (attributes._links || [])
+ |> Enum.map(&__MODULE__.new(&1))
+ |> Enum.map(&({&1.rel, &1}))
+ |> Enum.into(%{})
+ end
+end
diff --git a/lib/list.ex b/lib/list.ex
index 43ec63a..4fff8a1 100644
--- a/lib/list.ex
+++ b/lib/list.ex
@@ -1,40 +1,28 @@
defmodule Mailchimp.List do
- import Mailchimp.HTTPClient
+ alias Mailchimp.HTTPClient
+ alias Mailchimp.Link
- def all(config) do
- map_header = %{"Authorization" => "apikey #{config.apikey}"}
- config.apiroot
- |> build_url
- |> get(map_header)
- end
-
- def members(config, list_id) do
- map_header = %{"Authorization" => "apikey #{config.apikey}"}
- config.apiroot <> "lists/" <> list_id <> "/members"
- |> get(map_header)
- 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 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
+ 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 as_string(param) do
- Enum.reduce(param, fn(s, acc) -> acc<>","<>s end)
+ 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
-
end