Skip to content

Commit bd6f232

Browse files
authored
feat: Update contact by email (#93)
1 parent d441b4a commit bd6f232

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

examples/contacts.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def example
2323

2424
update_params = {
2525
audience_id: audience_id,
26-
id: contact[:id],
27-
unsubscribed: true,
26+
email: params[:email],
27+
# id: contact[:id],
28+
unsubscribed: false,
29+
first_name: "Updated",
2830
}
2931

3032
retrieved = Resend::Contacts.get(audience_id, contact[:id])

lib/resend/contacts.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def get(audience_id, id)
1919
Resend::Request.new(path, {}, "get").perform
2020
end
2121

22+
#
23+
# List contacts in an audience
24+
#
25+
# @param audience_id [String] the audience id
2226
# https://resend.com/docs/api-reference/contacts/list-contacts
2327
def list(audience_id)
2428
path = "audiences/#{audience_id}/contacts"
@@ -37,9 +41,15 @@ def remove(audience_id, contact_id)
3741
Resend::Request.new(path, {}, "delete").perform
3842
end
3943

44+
#
45+
# Update a contact
46+
#
47+
# @param params [Hash] the contact params
4048
# https://resend.com/docs/api-reference/contacts/update-contact
4149
def update(params)
42-
path = "audiences/#{params[:audience_id]}/contacts/#{params[:id]}"
50+
raise ArgumentError, "id or email is required" if params[:id].nil? && params[:email].nil?
51+
52+
path = "audiences/#{params[:audience_id]}/contacts/#{params[:id] || params[:email]}"
4353
Resend::Request.new(path, params, "patch").perform
4454
end
4555
end

spec/contacts_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,13 @@
136136
expect(contact[:id]).to eql("479e3145-dd38-476b-932c-529ceb705947")
137137
expect(contact[:object]).to eql("contact")
138138
end
139+
140+
it "raise when required fields are not provided" do
141+
begin
142+
Resend::Contacts.update({ audience_id: "123" })
143+
rescue ArgumentError => e
144+
expect(e.message).to eql("id or email is required")
145+
end
146+
end
139147
end
140148
end

0 commit comments

Comments
 (0)