klaviyo_api 1.1.9 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/lib/klaviyo_api/resources/base.rb +3 -1
- data/lib/klaviyo_api/resources/list.rb +27 -0
- data/lib/klaviyo_api/resources/list_member.rb +71 -0
- data/lib/klaviyo_api/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74537aa6edf57066528bfcefcf4fcb883ed5e85f68ed3bf562892dad33913d7d
|
4
|
+
data.tar.gz: a885389e5301c6377a244a8a78af4c25b70981859f789cd4e53e2d72f69b8fe7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd11a8738de6446e4d3619a1b703528021dbcd07328ceb3d01d3f335fff14bc3a21409995423b2f15f3356adb9a2a3a6702cf9db89549f2f59b59cbfefa91466
|
7
|
+
data.tar.gz: 6a4c7bf744142bed009aa223e297b19f15d5073a57faaa123def8304b75d9e415ad85fd86e7e10eb59027148c750adbdc6cc452944dc89eba8c95432aac9fafc
|
data/CHANGELOG.md
CHANGED
@@ -34,8 +34,10 @@ module KlaviyoAPI
|
|
34
34
|
def element_path(id, prefix_options = {}, query_options = nil)
|
35
35
|
check_prefix_options(prefix_options)
|
36
36
|
|
37
|
+
id = URI.parser.escape id.to_s
|
37
38
|
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
|
38
|
-
|
39
|
+
|
40
|
+
"#{prefix(prefix_options)}#{element_name}#{'/' + id if id.present?}#{format_extension}#{query_string(query_options)}"
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
@@ -12,5 +12,32 @@ module KlaviyoAPI
|
|
12
12
|
super.tap { |record| record.id = scope }
|
13
13
|
end
|
14
14
|
end
|
15
|
+
|
16
|
+
# Gets all Members of this List. Uses the Groups endpoint.
|
17
|
+
# Returns an enumerator that knows how to transparently deal
|
18
|
+
# with Klaviyo's `marker` for pagination. Pages seem to be
|
19
|
+
# 1000 items.
|
20
|
+
#
|
21
|
+
# https://www.klaviyo.com/docs/api/v2/lists#get-members-all
|
22
|
+
def members(options = {})
|
23
|
+
CachingEnumerator.new do |yielder|
|
24
|
+
marker = nil
|
25
|
+
|
26
|
+
loop do
|
27
|
+
path = "#{self.class.prefix}group/#{id}/members/all"
|
28
|
+
path += "?marker=#{marker}" if marker
|
29
|
+
|
30
|
+
response = JSON.parse(connection.get(path, self.class.headers).body)
|
31
|
+
marker = response['marker']
|
32
|
+
list_members = response['records']
|
33
|
+
|
34
|
+
list_members.each do |list_member|
|
35
|
+
yielder.yield KlaviyoAPI::ListMember.new list_member
|
36
|
+
end
|
37
|
+
|
38
|
+
break if marker.nil?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
15
42
|
end
|
16
43
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KlaviyoAPI
|
4
|
+
class ListMember < Base
|
5
|
+
self.prefix += 'v2/list/:list_id/'
|
6
|
+
self.primary_key = :id
|
7
|
+
|
8
|
+
# This is always plural
|
9
|
+
self.element_name = 'members'
|
10
|
+
|
11
|
+
class << self
|
12
|
+
# Removing a Member from a List is a DELETE call, but no ID is given.
|
13
|
+
# Instead, the email must be provided in the query params.
|
14
|
+
# Klaviyo also accepts an array in the body, but Ruby's HTTP library
|
15
|
+
# does not support DELETE bodies (as per the HTTP spec).
|
16
|
+
#
|
17
|
+
# https://www.klaviyo.com/docs/api/v2/lists#delete-members
|
18
|
+
def delete(email, options = {})
|
19
|
+
options = options.merge({emails: email})
|
20
|
+
connection.delete(element_path('', options), headers)
|
21
|
+
end
|
22
|
+
|
23
|
+
# A shortcut to create multiple ListMembers at once, as supported
|
24
|
+
# by the Klaviyo API.
|
25
|
+
#
|
26
|
+
# https://www.klaviyo.com/docs/api/v2/lists#post-members
|
27
|
+
def bulk_create(list_members, options = {})
|
28
|
+
payload = { profiles: list_members }.to_json
|
29
|
+
|
30
|
+
saved_list_members = []
|
31
|
+
connection.post(collection_path(options), payload, headers).tap do |response|
|
32
|
+
list_members_json = JSON.parse(response.body)
|
33
|
+
list_members_json.each do |list_member_json|
|
34
|
+
saved_list_members << KlaviyoAPI::ListMember.new(list_member_json)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
saved_list_members
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Adding a single Member to a List does not exist - they must be added as an
|
42
|
+
# array of Profiles. In order to fit AR, we need to wrap self in an array,
|
43
|
+
# and remove it in the response.
|
44
|
+
#
|
45
|
+
# https://www.klaviyo.com/docs/api/v2/lists#post-members
|
46
|
+
def create
|
47
|
+
payload = { profiles: [self] }.to_json
|
48
|
+
|
49
|
+
run_callbacks :create do
|
50
|
+
connection.post(collection_path, payload, self.class.headers).tap do |response|
|
51
|
+
response.body = JSON.parse(response.body)[0].to_json
|
52
|
+
self.id = id_from_response(response)
|
53
|
+
load_attributes_from_response(response)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Can only delete by email, not ID.
|
59
|
+
#
|
60
|
+
# https://www.klaviyo.com/docs/api/v2/lists#delete-members
|
61
|
+
def destroy
|
62
|
+
run_callbacks :destroy do
|
63
|
+
KlaviyoAPI::ListMember.delete self.email, prefix_options
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def update
|
68
|
+
raise KlaviyoAPI::InvalidOperation.new 'Cannot update list members. You might be looking for delete and/or create.'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/klaviyo_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: klaviyo_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rewind.io
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activeresource
|
@@ -212,6 +212,7 @@ files:
|
|
212
212
|
- lib/klaviyo_api/resources.rb
|
213
213
|
- lib/klaviyo_api/resources/base.rb
|
214
214
|
- lib/klaviyo_api/resources/list.rb
|
215
|
+
- lib/klaviyo_api/resources/list_member.rb
|
215
216
|
- lib/klaviyo_api/session.rb
|
216
217
|
- lib/klaviyo_api/version.rb
|
217
218
|
homepage: https://github.com/rewindio/klaviyo_api.git
|