aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMentor Ágil <mentoragil@gmail.com>2015-05-12 22:11:49 -0300
committerMentor Ágil <mentoragil@gmail.com>2015-05-12 22:11:49 -0300
commit765438c4c2c300f84364b978a0b199a0e69c4e80 (patch)
tree8641ea4d3910a28fa8c702ee057251ce2d751abe /lib
downloadmailchimp-765438c4c2c300f84364b978a0b199a0e69c4e80.tar.gz
mailchimp-765438c4c2c300f84364b978a0b199a0e69c4e80.tar.xz
first commit
Diffstat (limited to 'lib')
-rw-r--r--lib/account.ex10
-rw-r--r--lib/httpclient.ex20
-rw-r--r--lib/list.ex27
-rw-r--r--lib/mailchimp.ex49
4 files changed, 106 insertions, 0 deletions
diff --git a/lib/account.ex b/lib/account.ex
new file mode 100644
index 0000000..586d274
--- /dev/null
+++ b/lib/account.ex
@@ -0,0 +1,10 @@
+defmodule Mailchimp.Account do
+ import Mailchimp.HTTPClient
+
+ def get_details(config) do
+ map_header = %{"Authorization" => "apikey #{config.apikey}"}
+ url = config.apiroot
+ get(url, map_header)
+ end
+
+end
diff --git a/lib/httpclient.ex b/lib/httpclient.ex
new file mode 100644
index 0000000..b5d0f1e
--- /dev/null
+++ b/lib/httpclient.ex
@@ -0,0 +1,20 @@
+defmodule Mailchimp.HTTPClient do
+
+ def get(url, header) do
+ case HTTPoison.get(url, header) do
+ {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
+ process_response_body body
+ {:ok, %HTTPoison.Response{status_code: 404}} ->
+ "Not found :("
+ {:error, %HTTPoison.Error{reason: reason}} ->
+ reason
+ end
+ end
+
+ def process_response_body(body) do
+ body
+ |> Poison.decode!
+ |> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end)
+ end
+
+end
diff --git a/lib/list.ex b/lib/list.ex
new file mode 100644
index 0000000..7363f03
--- /dev/null
+++ b/lib/list.ex
@@ -0,0 +1,27 @@
+defmodule Mailchimp.List do
+ import Mailchimp.HTTPClient
+
+ def get_all(config) do
+ map_header = %{"Authorization" => "apikey #{config.apikey}"}
+ config.apiroot
+ |> build_url
+ |> get(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
+
+ def as_string(param) do
+ Enum.reduce(param, fn(s, acc) -> acc<>","<>s end)
+ end
+
+end
diff --git a/lib/mailchimp.ex b/lib/mailchimp.ex
new file mode 100644
index 0000000..ae8c8ee
--- /dev/null
+++ b/lib/mailchimp.ex
@@ -0,0 +1,49 @@
+defmodule Mailchimp do
+ use Application
+ use GenServer
+ require Logger
+
+ @apikey Application.get_env :mailchimp, :apikey
+
+ ### Public API
+ def start_link(_opts) do
+ shard = get_shard
+ apiroot = "https://#{shard}.api.mailchimp.com/3.0/"
+ config = %{apiroot: apiroot, apikey: @apikey}
+ GenServer.start_link(Mailchimp, config, name: :mailchimp)
+ end
+
+ def get_account_details do
+ GenServer.call(:mailchimp, :account_details)
+ end
+
+ def get_all_lists do
+ GenServer.call(:mailchimp, :all_lists)
+ end
+
+ ### Server API
+ def handle_call(:account_details, _from, config) do
+ details = Mailchimp.Account.get_details(config)
+ {:reply, details, config}
+ end
+
+ def handle_call(:all_lists, _from, config) do
+ lists = Mailchimp.List.get_all(config)
+ {:reply, lists, config}
+ end
+
+ def get_shard do
+ parts = @apikey
+ |> String.split(~r{-})
+
+ case length(parts) do
+ 2 ->
+ List.last parts
+ _ ->
+ Logger.error "This doesn't look like an API Key: #{@apikey}"
+ Logger.info "The API Key should have both a key and a server name, separated by a dash, like this: abcdefg8abcdefg6abcdefg4-us1"
+ {:error}
+ end
+ end
+
+end