aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMentor Ágil <mentoragil@gmail.com>2015-05-12 23:52:51 -0300
committerMentor Ágil <mentoragil@gmail.com>2015-05-12 23:52:51 -0300
commit68d6b62493e118667588a04260dac67a3ae4ad0b (patch)
tree2f2179456151cc60c6481b363c22186b7ff96d3b /lib
parent765438c4c2c300f84364b978a0b199a0e69c4e80 (diff)
downloadmailchimp-68d6b62493e118667588a04260dac67a3ae4ad0b.tar.gz
mailchimp-68d6b62493e118667588a04260dac67a3ae4ad0b.tar.xz
added functions to manipulate lists
Diffstat (limited to 'lib')
-rw-r--r--lib/httpclient.ex11
-rw-r--r--lib/list.ex15
-rw-r--r--lib/mailchimp.ex22
3 files changed, 45 insertions, 3 deletions
diff --git a/lib/httpclient.ex b/lib/httpclient.ex
index b5d0f1e..2b1594b 100644
--- a/lib/httpclient.ex
+++ b/lib/httpclient.ex
@@ -11,6 +11,17 @@ defmodule Mailchimp.HTTPClient do
end
end
+ def post(url, body, header) do
+ case HTTPoison.post(url, body, 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!
diff --git a/lib/list.ex b/lib/list.ex
index 7363f03..43ec63a 100644
--- a/lib/list.ex
+++ b/lib/list.ex
@@ -1,13 +1,26 @@
defmodule Mailchimp.List do
import Mailchimp.HTTPClient
- def get_all(config) do
+ 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"]},
diff --git a/lib/mailchimp.ex b/lib/mailchimp.ex
index ae8c8ee..e09f1e6 100644
--- a/lib/mailchimp.ex
+++ b/lib/mailchimp.ex
@@ -6,7 +6,7 @@ defmodule Mailchimp do
@apikey Application.get_env :mailchimp, :apikey
### Public API
- def start_link(_opts) do
+ def start do
shard = get_shard
apiroot = "https://#{shard}.api.mailchimp.com/3.0/"
config = %{apiroot: apiroot, apikey: @apikey}
@@ -21,6 +21,14 @@ defmodule Mailchimp do
GenServer.call(:mailchimp, :all_lists)
end
+ def get_list_members(list_id) do
+ GenServer.call(:mailchimp, {:list_members, list_id})
+ end
+
+ def add_member(list_id, email) do
+ GenServer.call(:mailchimp, {:add_member, list_id, email})
+ end
+
### Server API
def handle_call(:account_details, _from, config) do
details = Mailchimp.Account.get_details(config)
@@ -28,10 +36,20 @@ defmodule Mailchimp do
end
def handle_call(:all_lists, _from, config) do
- lists = Mailchimp.List.get_all(config)
+ lists = Mailchimp.List.all(config)
{:reply, lists, config}
end
+ def handle_call({:list_members, list_id}, _from, config) do
+ members = Mailchimp.List.members(config, list_id)
+ {:reply, members, config}
+ end
+
+ def handle_call({:add_member, list_id, email}, _from, config) do
+ member = Mailchimp.List.add_member(config, %{"list_id" => list_id, "email" => email})
+ {:reply, member, config}
+ end
+
def get_shard do
parts = @apikey
|> String.split(~r{-})