Skip to content

Commit

Permalink
Merge pull request #37 from TrevorHinesley/create_or_update_member_up…
Browse files Browse the repository at this point in the history
…date_email

Added member-related functionality
  • Loading branch information
duartejc authored Jun 3, 2024
2 parents ad076a1 + ae07656 commit 8cad501
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 4 deletions.
107 changes: 107 additions & 0 deletions fixture/vcr_cassettes/member.add_event.json

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions fixture/vcr_cassettes/member.create_or_update.update_email.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/mailchimp/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Mailchimp.HTTPClient do
iex> Application.put_env(:mailchimp, :api_key, "your apikey-us12")
iex> Mailchimp.HTTPClient.process_request_headers([])
[{"Authorization", "Basic your apikey-us12"}]
[{"Authorization", "Bearer your apikey-us12"}]
"""
def process_request_headers(headers) do
Expand Down
11 changes: 9 additions & 2 deletions lib/mailchimp/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,22 @@ defmodule Mailchimp.List do
)
when is_binary(email_address) and is_map(merge_fields) and
status_if_new in ["subscribed", "pending", "unsubscribed", "cleaned"] do

subscriber_id =
email_address
|> String.downcase()
|> md5

# Allows additional_data[:email_address] to update the member's email address
additional_data =
Map.merge(
%{
email_address: email_address
},
additional_data
)

data =
Map.merge(additional_data, %{
email_address: email_address,
status_if_new: status_if_new,
merge_fields: merge_fields
})
Expand Down
35 changes: 35 additions & 0 deletions lib/mailchimp/member.ex
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,39 @@ defmodule Mailchimp.Member do

{:ok, status_code}
end

@doc """
Adds event to the member in Mailchimp
"""
def add_event(
user = %__MODULE__{links: %{"events" => %Link{href: href}}},
name,
optional_parameters \\ %{}
) do
attrs =
Map.merge(
%{name: name},
optional_parameters
)

case HTTPClient.post(href, Jason.encode!(attrs)) do
{:ok, %Response{status_code: 204, body: _body}} ->
{:ok, user}

{:ok, %Response{status_code: _, body: body}} ->
{:error, body}

{:error, %Error{reason: reason}} ->
{:error, reason}
end
end

@doc """
Same as `add_event/2`
but raises errors.
"""
def add_event!(user, name, optional_parameters \\ %{}) do
{:ok, user} = add_event(user, name, optional_parameters)
user
end
end
2 changes: 1 addition & 1 deletion test/mailchimp/http_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Mailchimp.HTTPClientTest do
Application.put_env(:mailchimp, :api_key, "your apikey-us12")

assert HTTPClient.process_request_headers([]) == [
{"Authorization", "Basic your apikey-us12"}
{"Authorization", "Bearer your apikey-us12"}
]

Application.delete_env(:mailchimp, :api_key)
Expand Down
28 changes: 28 additions & 0 deletions test/mailchimp/list_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ defmodule Mailchimp.ListTest do
)
end
end

test "updates existing member's email address" do
use_cassette "member.create_or_update.update_email" do
account = Account.get!()
[list] = Account.lists!(account)

{:ok, %Member{status: "subscribed", merge_fields: %{LNAME: "Test"}, language: "de"}} =
List.create_or_update_member(
list,
"[email protected]",
"subscribed",
%{LNAME: "Test"},
%{
language: "de",
email_address: "[email protected]"
}
)

%Member{} =
List.create_or_update_member!(
list,
"[email protected]",
"subscribed",
%{LNAME: "Test"},
%{language: "de", email_address: "[email protected]"}
)
end
end
end

describe "create_member/5" do
Expand Down
12 changes: 12 additions & 0 deletions test/mailchimp/member_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@ defmodule Mailchimp.MemberTest do
end
end
end

describe "add_event/1" do
test "adds event" do
use_cassette "member.add_event" do
account = Account.get!()
[list] = Account.lists!(account)
member = List.get_member!(list, "[email protected]")

{:ok, %Member{email_address: "[email protected]"}} = Member.add_event(member, "test_event")
end
end
end
end

0 comments on commit 8cad501

Please sign in to comment.