Skip to content

Commit

Permalink
Change the do_add_member/2 function to use PUT instead of POST and ad…
Browse files Browse the repository at this point in the history
…d destroy_member/2 (#3)
  • Loading branch information
jnylen committed Oct 20, 2020
1 parent 5635668 commit 668a1bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 20 additions & 3 deletions lib/ex_chimp/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ defmodule ExChimp.List do
|> Map.put(:status, status)
|> Map.put(:merge_fields, merge_fields)
|> Map.merge(other_fields)
|> Jason.encode!()
|> do_add_member(list_id)
end

Expand All @@ -46,14 +45,32 @@ defmodule ExChimp.List do
%{}
|> Map.put(:email_address, email)
|> Map.put(:status, status)
|> Jason.encode!()
|> do_add_member(list_id)
end

@spec destroy_member(binary, binary) :: {:ok, map} | {:error, binary}
def destroy_member(list_id, email) do
url = "lists/#{list_id}/members/#{md5_sum(email)}/actions/delete-permanent"

case Client.post(url, "") do
{:ok, %{body: %{"status" => 400, "detail" => error}}} -> {:error, error}
{:ok, %{body: body}} -> {:ok, body}
end
end

defp do_add_member(data, list_id) do
case Client.post("lists/#{list_id}/members", data) do
url = "lists/#{list_id}/members/#{hash_email(data)}"

case Client.put(url, Jason.encode!(data)) do
{:ok, %{body: %{"status" => 400, "detail" => error}}} -> {:error, error}
{:ok, %{body: body}} -> {:ok, body}
end
end

defp hash_email(%{email_address: email}), do: md5_sum(email)

defp md5_sum(binary),
do:
:crypto.hash(:md5, String.downcase(binary))
|> Base.encode16(case: :lower)
end
2 changes: 1 addition & 1 deletion test/api/list_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule ExChimp.ListTest do
test "add member to list" do
use_cassette :stub,
url: "https://us12.api.mailchimp.com/3.0/lists/asdf1234/members",
method: "post",
method: "put",
status_code: ["HTTP/1.1", 200, "OK"],
body: @list_add_member_success_json do
{:ok, body} =
Expand Down

0 comments on commit 668a1bf

Please sign in to comment.