aboutsummaryrefslogtreecommitdiff
path: root/lib/mailchimp.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mailchimp.ex')
-rw-r--r--lib/mailchimp.ex76
1 files changed, 7 insertions, 69 deletions
diff --git a/lib/mailchimp.ex b/lib/mailchimp.ex
index 4281a36..5a40919 100644
--- a/lib/mailchimp.ex
+++ b/lib/mailchimp.ex
@@ -1,76 +1,14 @@
defmodule Mailchimp do
use Application
- use GenServer
- require Logger
- @apikey Application.get_env :mailchimp, :apikey
+ def start(_type, _args) do
+ import Supervisor.Spec, warn: false
- ### Public API
- def start_link 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
-
- 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
+ children = [
+ worker(Mailchimp.Config, []),
+ ]
- def add_pending_member(list_id, email) do
- GenServer.call(:mailchimp, {:add_pending_member, list_id, email})
+ opts = [strategy: :one_for_one, name: Aliver.Supervisor]
+ Supervisor.start_link(children, opts)
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.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 handle_call({:add_pending_member, list_id, email}, _from, config) do
- member = Mailchimp.List.add_pending_member(config, %{"list_id" => list_id, "email" => email})
- {:reply, member, 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