Class: KlaviyoAPI::ListMember
- Defined in:
- lib/klaviyo_api/resources/list_member.rb
Constant Summary collapse
- ORIGINAL_PREFIX =
'/api/v2/list/:list_id/'
Class Method Summary collapse
-
.all_members(options) ⇒ Object
As opposed to #all, which hits and requires a list of emails to check for membership, this method hits www.klaviyo.com/docs/api/v2/lists#get-members-all and gets all ListMembers for a List.
-
.bulk_create(list_members, options = {}) ⇒ Object
A shortcut to create multiple ListMembers at once, as supported by the Klaviyo API.
-
.delete(email, options = {}) ⇒ Object
Removing a Member from a List is a DELETE call, but no ID is given.
Instance Method Summary collapse
-
#create ⇒ Object
Adding a single Member to a List does not exist - they must be added as an array of Profiles.
-
#destroy ⇒ Object
Can only delete by email, not ID.
- #update ⇒ Object
Methods inherited from Base
activate_session, element_path, headers, reset_session, #to_h
Class Method Details
.all_members(options) ⇒ Object
As opposed to #all, which hits and requires a list of emails to check for membership, this method hits www.klaviyo.com/docs/api/v2/lists#get-members-all and gets all ListMembers for a List.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/klaviyo_api/resources/list_member.rb', line 45 def all_members() # The rest becomes much easier if we temporarily modify the prefix. # We set it back later, have no fear. self.prefix = '/api/v2/group/:list_id/members/all' , = ([:params]) path = "#{prefix()}#{format_extension}#{query_string()}" # Very heavily inspired by `find_every` response = format.decode(connection.get(path, headers).body) || [] collection = KlaviyoAPI::Collections::ListMembershipCollection.new(response).tap do |parser| parser.resource_class = self parser.original_params = end collection.collect! { |record| instantiate_record(record, ) } self.prefix = ORIGINAL_PREFIX collection end |
.bulk_create(list_members, options = {}) ⇒ Object
A shortcut to create multiple ListMembers at once, as supported by the Klaviyo API.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/klaviyo_api/resources/list_member.rb', line 29 def bulk_create(list_members, = {}) payload = { profiles: list_members }.to_json saved_list_members = [] connection.post(collection_path(), payload, headers).tap do |response| list_members_json = JSON.parse(response.body) list_members_json.each do |list_member_json| saved_list_members << KlaviyoAPI::ListMember.new(list_member_json) end end saved_list_members end |
.delete(email, options = {}) ⇒ Object
Removing a Member from a List is a DELETE call, but no ID is given. Instead, the email must be provided in the query params. Klaviyo also accepts an array in the body, but Ruby’s HTTP library does not support DELETE bodies (as per the HTTP spec).
20 21 22 23 |
# File 'lib/klaviyo_api/resources/list_member.rb', line 20 def delete(email, = {}) = .merge emails: email connection.delete(element_path('', ), headers) end |
Instance Method Details
#create ⇒ Object
Adding a single Member to a List does not exist - they must be added as an array of Profiles. In order to fit AR, we need to wrap self in an array, and remove it in the response.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/klaviyo_api/resources/list_member.rb', line 73 def create payload = { profiles: [self] }.to_json run_callbacks :create do connection.post(collection_path, payload, self.class.headers).tap do |response| response.body = JSON.parse(response.body)[0].to_json self.id = id_from_response(response) load_attributes_from_response(response) end end end |
#destroy ⇒ Object
Can only delete by email, not ID.
88 89 90 91 92 |
# File 'lib/klaviyo_api/resources/list_member.rb', line 88 def destroy run_callbacks :destroy do KlaviyoAPI::ListMember.delete email, end end |
#update ⇒ Object
94 95 96 |
# File 'lib/klaviyo_api/resources/list_member.rb', line 94 def update raise KlaviyoAPI::InvalidOperation, 'Cannot update list members. You might be looking for delete and/or create.' end |