klaviyo 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 637b6125081b06c6f8121a331e4bfaef0976a5293496a3f9fe5aaed3b8b96afc
4
- data.tar.gz: 420103a5fec88c53524eeeeaf32826424feb3fe5bd4146b4127fc71059319512
3
+ metadata.gz: 1baf7e1870c03ff86c72a16d22660ca2eea9d4c8bbcbec8e23d6797a05114aa8
4
+ data.tar.gz: a51a43bdbbeafc98209be44e174931eabafa67d5cdb5119b9603037dcfb4e233
5
5
  SHA512:
6
- metadata.gz: 15ca790911f3306253271f36fe7e15da7c2e042a3a25614217aab530d52c4bfcfdf4cd675cd1bf4f761d7e4b9b126c07b87681def392daf9514d5ab072f016e2
7
- data.tar.gz: 4eca152856c1ce76571c0745a6d437ed8ccea93e6fbf057f35d81771c80c11219f20198fb52cf561447554a939e8eea65fa0c2db041c54f426c7b70270d73a10
6
+ metadata.gz: fb17e43af4d4f45cfc5e34c0710ff0c18e18db6152f657362d79980ba9315594bcb73edd1057e8d4e29888ed27038380058e061a14b32c7043f85dab0d3ad4c7
7
+ data.tar.gz: 59d2695a3867749aacdfe713cf9337ea7e6e427c65e5b508b21c1c70b656bb73dadd7a5cae0586d3e7d875422b7457c51abd6560a15c0330d6586c951d359f24
data/klaviyo.gemspec CHANGED
@@ -2,8 +2,8 @@ files = ['klaviyo.gemspec', '{lib}/**/**/*'].map {|f| Dir[f]}.flatten
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'klaviyo'
5
- s.version = '2.1.0'
6
- s.date = '2021-10-01'
5
+ s.version = '2.2.0'
6
+ s.date = '2021-10-04'
7
7
  s.summary = 'You heard us, a Ruby wrapper for the Klaviyo API'
8
8
  s.description = 'Ruby wrapper for the Klaviyo API'
9
9
  s.authors = ['Klaviyo Team']
@@ -6,36 +6,40 @@ module Klaviyo
6
6
  SEND = 'send'
7
7
 
8
8
  # Retrieves all the campaigns from Klaviyo account
9
+ # @kwarg :api_key [String] private API key for this request
9
10
  # @return [List] of JSON formatted campaing objects
10
- def self.get_campaigns()
11
- v1_request(HTTP_GET, CAMPAIGNS)
11
+ def self.get_campaigns(api_key: nil)
12
+ v1_request(HTTP_GET, CAMPAIGNS, api_key: api_key)
12
13
  end
13
14
 
14
15
  # Retrieves the details of the list
15
16
  # @param campaign_id the if of campaign
17
+ # @kwarg :api_key [String] private API key for this request
16
18
  # @return [JSON] a JSON object containing information about the campaign
17
- def self.get_campaign_details(campaign_id)
19
+ def self.get_campaign_details(campaign_id, api_key: nil)
18
20
  path = "#{CAMPAIGN}/#{campaign_id}"
19
21
 
20
- v1_request(HTTP_GET, path)
22
+ v1_request(HTTP_GET, path, api_key: api_key)
21
23
  end
22
24
 
23
25
  # Sends the campaign immediately
24
26
  # @param campaign_id [String] the id of campaign
27
+ # @kwarg :api_key [String] private API key for this request
25
28
  # @return will return with HTTP ok in case of success
26
- def self.send_campaign(campaign_id)
29
+ def self.send_campaign(campaign_id, api_key: nil)
27
30
  path = "#{CAMPAIGN}/#{campaign_id}/#{SEND}"
28
31
 
29
- v1_request(HTTP_POST, path)
32
+ v1_request(HTTP_POST, path, api_key: api_key)
30
33
  end
31
34
 
32
35
  # Cancels the campaign with specified campaign_id
33
36
  # @param campaign_id [String] the id of campaign
37
+ # @kwarg :api_key [String] private API key for this request
34
38
  # @return [JSON] a JSON object containing the campaign details
35
- def self.cancel_campaign(campaign_id)
39
+ def self.cancel_campaign(campaign_id, api_key: nil)
36
40
  path = "#{CAMPAIGN}/#{campaign_id}/#{CANCEL}"
37
41
 
38
- v1_request(HTTP_POST, path)
42
+ v1_request(HTTP_POST, path, api_key: api_key)
39
43
  end
40
44
  end
41
45
  end
@@ -6,14 +6,15 @@ module Klaviyo
6
6
  # Submits a data privacy-related deletion request
7
7
  # @param id_type [String] 'email' or 'phone_number' or 'person_id
8
8
  # @param identifier [String] value for the identifier specified
9
+ # @kwarg :api_key [String] private API key for this request
9
10
  # @return a dictionary with a confirmation that deletion task submitted for the customer
10
- def self.request_profile_deletion(id_type, identifier)
11
+ def self.request_profile_deletion(id_type, identifier, api_key: nil)
11
12
  unless ['email', 'phone_number', 'person_id'].include? id_type
12
13
  raise Klaviyo::KlaviyoError.new(INVALID_ID_TYPE_ERROR)
13
14
  end
14
15
  data = Hash[id_type.to_sym, identifier]
15
16
  path = "#{DATA_PRIVACY}/#{DELETION_REQUEST}"
16
- v2_request(HTTP_POST, path, **data)
17
+ v2_request(HTTP_POST, path, api_key: api_key, **data)
17
18
  end
18
19
  end
19
20
  end
@@ -8,21 +8,23 @@ module Klaviyo
8
8
 
9
9
  # Returns a list of all the email templates you've created.
10
10
  # The templates are returned in sorted order by name.
11
+ # @kwarg :api_key [String] private API key for this request
11
12
  # @return [List] of JSON formatted email template objects
12
- def self.get_templates()
13
- v1_request(HTTP_GET, EMAIL_TEMPLATES)
13
+ def self.get_templates(api_key: nil)
14
+ v1_request(HTTP_GET, EMAIL_TEMPLATES, api_key: api_key)
14
15
  end
15
16
 
16
17
  # Creates a new email template
17
18
  # @param :name [String] The name of the email template
18
19
  # @param :html [String] The HTML content for this template
20
+ # @kwarg :api_key [String] private API key for this request
19
21
  # @return [JSON] a JSON object containing information about the email template
20
- def self.create_template(name: nil, html: nil)
22
+ def self.create_template(name: nil, html: nil, api_key: nil)
21
23
  params = {
22
24
  name: name,
23
25
  html: html
24
26
  }
25
- v1_request(HTTP_POST, EMAIL_TEMPLATES, content_type: CONTENT_URL_FORM, params: params)
27
+ v1_request(HTTP_POST, EMAIL_TEMPLATES, api_key: api_key, content_type: CONTENT_URL_FORM, params: params)
26
28
  end
27
29
 
28
30
  # Updates the name and/or HTML content of a template. Only updates imported
@@ -30,47 +32,51 @@ module Klaviyo
30
32
  # @param template_id [String] The id of the email template
31
33
  # @param :name [String] The name of the email template
32
34
  # @param :html [String] The HTML content for this template
35
+ # @kwarg :api_key [String] private API key for this request
33
36
  # @return [JSON] a JSON object containing information about the email template
34
- def self.update_template(template_id, name:, html:)
37
+ def self.update_template(template_id, name:, html:, api_key: nil)
35
38
  path = "#{EMAIL_TEMPLATE}/#{template_id}"
36
39
  params = {
37
40
  name: name,
38
41
  html: html
39
42
  }
40
- v1_request(HTTP_PUT, path, **params)
43
+ v1_request(HTTP_PUT, path, api_key: api_key, **params)
41
44
  end
42
45
 
43
46
  # Deletes a given template.
44
47
  # @param template_id [String] The id of the email template
48
+ # @kwarg :api_key [String] private API key for this request
45
49
  # @return [JSON] a JSON object containing information about the email template
46
- def self.delete_template(template_id)
50
+ def self.delete_template(template_id, api_key: nil)
47
51
  path = "#{EMAIL_TEMPLATE}/#{template_id}"
48
- v1_request(HTTP_DELETE, path)
52
+ v1_request(HTTP_DELETE, path, api_key: api_key)
49
53
  end
50
54
 
51
55
  # Creates a copy of a given template with a new name
52
56
  # @param template_id [String] The id of the email template to copy
53
57
  # @param :name [String] The name of the newly cloned email template
58
+ # @kwarg :api_key [String] private API key for this request
54
59
  # @return [JSON] a JSON object containing information about the email template
55
- def self.clone_template(template_id, name:)
60
+ def self.clone_template(template_id, name:, api_key: nil)
56
61
  path = "#{EMAIL_TEMPLATE}/#{template_id}/#{CLONE}"
57
62
  params = {
58
63
  name: name
59
64
  }
60
- v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
65
+ v1_request(HTTP_POST, path, api_key: api_key, content_type: CONTENT_URL_FORM, params: params)
61
66
  end
62
67
 
63
68
  # Renders the specified template with the provided data and return HTML
64
69
  # and text versions of the email
65
70
  # @param template_id [String] The id of the email template to copy
66
71
  # @param :context [Hash] The context the email template will be rendered with
72
+ # @kwarg :api_key [String] private API key for this request
67
73
  # @return [JSON] a JSON object containing information about the email template
68
- def self.render_template(template_id, context: {})
74
+ def self.render_template(template_id, context: {}, api_key: nil)
69
75
  path = "#{EMAIL_TEMPLATE}/#{template_id}/#{RENDER}"
70
76
  params = {
71
77
  context: context
72
78
  }
73
- v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
79
+ v1_request(HTTP_POST, path, api_key: api_key, content_type: CONTENT_URL_FORM, params: params)
74
80
  end
75
81
 
76
82
  # Renders the specified template with the provided data and then send the
@@ -81,8 +87,9 @@ module Klaviyo
81
87
  # @param :subject [String] The subject of the email template
82
88
  # @param :to [Mixed] The email this template is being sent to
83
89
  # @param :context [Hash] The context the email template will be rendered with
90
+ # @kwarg :api_key [String] private API key for this request
84
91
  # @return [JSON] a JSON object containing information about the email template
85
- def self.send_template(template_id, from_email:, from_name:, subject:, to:, context: {})
92
+ def self.send_template(template_id, from_email:, from_name:, subject:, to:, context: {}, api_key: nil)
86
93
  path = "#{EMAIL_TEMPLATE}/#{template_id}/#{SEND}"
87
94
  params = {
88
95
  from_email: from_email,
@@ -91,7 +98,7 @@ module Klaviyo
91
98
  to: to,
92
99
  context: context
93
100
  }
94
- v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
101
+ v1_request(HTTP_POST, path, api_key: api_key, content_type: CONTENT_URL_FORM, params: params)
95
102
  end
96
103
  end
97
104
  end
@@ -9,46 +9,51 @@ module Klaviyo
9
9
 
10
10
  # Creates a new list
11
11
  # @param list_name [String] the list name
12
+ # @kwarg :api_key [String] private API key for this request
12
13
  # @return will return with HTTP OK on success
13
- def self.create_list(list_name)
14
+ def self.create_list(list_name, api_key: nil)
14
15
  body = {
15
16
  :list_name => list_name
16
17
  }
17
- v2_request(HTTP_POST, LISTS, **body)
18
+ v2_request(HTTP_POST, LISTS, api_key: api_key, **body)
18
19
  end
19
20
 
20
21
  # Retrieves all the lists in the Klaviyo account
22
+ # @kwarg :api_key [String] private API key for this request
21
23
  # @return [List] a list of JSON objects of the name and id for each list
22
- def self.get_lists()
23
- v2_request(HTTP_GET, LISTS)
24
+ def self.get_lists(api_key: nil)
25
+ v2_request(HTTP_GET, LISTS, api_key: api_key)
24
26
  end
25
27
 
26
28
  # Retrieves the details of the list
27
29
  # @param list_id [String] the id of the list
30
+ # @kwarg :api_key [String] private API key for this request
28
31
  # @return [JSON] a JSON object containing information about the list
29
- def self.get_list_details(list_id)
32
+ def self.get_list_details(list_id, api_key: nil)
30
33
  path = "#{LIST}/#{list_id}"
31
- v2_request(HTTP_GET, path)
34
+ v2_request(HTTP_GET, path, api_key: api_key)
32
35
  end
33
36
 
34
37
  # Updates the properties of a list
35
38
  # @param list_id [String] the id of the list
36
39
  # @param list_name [String] the new name of the list
40
+ # @kwarg :api_key [String] private API key for this request
37
41
  # @return will return with HTTP OK on success
38
- def self.update_list_details(list_id, list_name)
42
+ def self.update_list_details(list_id, list_name, api_key: nil)
39
43
  path = "#{LIST}/#{list_id}"
40
44
  body = {
41
45
  :list_name => list_name
42
46
  }
43
- v2_request(HTTP_PUT, path, **body)
47
+ v2_request(HTTP_PUT, path, api_key: api_key, **body)
44
48
  end
45
49
 
46
50
  # Deletes a list
47
51
  # @param list_id [String] the id of the list
52
+ # @kwarg :api_key [String] private API key for this request
48
53
  # @return will return with HTTP OK on success
49
- def self.delete_list(list_id)
54
+ def self.delete_list(list_id, api_key: nil)
50
55
  path = "#{LIST}/#{list_id}"
51
- v2_request(HTTP_DELETE, path)
56
+ v2_request(HTTP_DELETE, path, api_key: api_key)
52
57
  end
53
58
 
54
59
  # Check if profiles are in a list and not supressed
@@ -56,57 +61,61 @@ module Klaviyo
56
61
  # @param :emails [List] the emails of the profiles to check
57
62
  # @param :phone_numbers [List] the phone numbers of the profiles to check
58
63
  # @param :push_tokens [List] push tokens of the profiles to check
64
+ # @kwarg :api_key [String] private API key for this request
59
65
  # @return A list of JSON objects of the profiles. Profiles that are
60
66
  # supressed or not found are not included.
61
- def self.check_list_subscriptions(list_id, emails: [], phone_numbers: [], push_tokens: [])
67
+ def self.check_list_subscriptions(list_id, api_key: nil, emails: [], phone_numbers: [], push_tokens: [])
62
68
  path = "#{LIST}/#{list_id}/#{SUBSCRIBE}"
63
69
  params = {
64
70
  :emails => emails,
65
71
  :phone_numbers => phone_numbers,
66
72
  :push_tokens => push_tokens
67
73
  }
68
- v2_request(HTTP_GET, path, **params)
74
+ v2_request(HTTP_GET, path, api_key: api_key, **params)
69
75
  end
70
76
 
71
77
  # Subscribe profiles to a list.
72
78
  # @param list_id [String] the id of the list
73
79
  # @param profiles [List] a list of JSON objects. Each object requires either
74
80
  # an email or phone number key.
81
+ # @kwarg :api_key [String] private API key for this request
75
82
  # @return will retun HTTP OK on success. If the list is single opt-in then a
76
83
  # list of records containing the email address, phone number, push token,
77
84
  # and the corresponding profile ID will also be included.
78
- def self.add_subscribers_to_list(list_id, profiles: [])
85
+ def self.add_subscribers_to_list(list_id, api_key: nil, profiles: [])
79
86
  path = "#{LIST}/#{list_id}/#{SUBSCRIBE}"
80
87
  params = {
81
88
  :profiles => profiles
82
89
  }
83
- v2_request(HTTP_POST, path, **params)
90
+ v2_request(HTTP_POST, path, api_key: api_key, **params)
84
91
  end
85
92
 
86
93
  # Unsubscribe and remove profiles from a list
87
94
  # @param list_id [String] the id of the list
88
95
  # @param :emails [List] the emails of the profiles to check
96
+ # @kwarg :api_key [String] private API key for this request
89
97
  # @return will return with HTTP OK on success
90
- def self.unsubscribe_from_list(list_id, emails: [])
98
+ def self.unsubscribe_from_list(list_id, api_key: nil, emails: [])
91
99
  path = "#{LIST}/#{list_id}/#{SUBSCRIBE}"
92
100
  params = {
93
101
  :emails => emails
94
102
  }
95
- v2_request(HTTP_DELETE, path, **params)
103
+ v2_request(HTTP_DELETE, path, api_key: api_key, **params)
96
104
  end
97
105
 
98
106
  # Add profiles to a list
99
107
  # @param list_id [String] the id of the list
100
108
  # @param :profiles [List] A list of JSON objects. Each object is a profile
101
109
  # that will be added to the list
110
+ # @kwarg :api_key [String] private API key for this request
102
111
  # @return will return with HTTP OK on success and a list of records of the
103
112
  # corresponding profile id
104
- def self.add_to_list(list_id, profiles: [])
113
+ def self.add_to_list(list_id, api_key: nil, profiles: [])
105
114
  path = "#{LIST}/#{list_id}/#{MEMBERS}"
106
115
  params = {
107
116
  :profiles => profiles
108
117
  }
109
- v2_request(HTTP_POST, path, **params)
118
+ v2_request(HTTP_POST, path, api_key: api_key, **params)
110
119
  end
111
120
 
112
121
  # Check if profiles are on a list
@@ -114,16 +123,17 @@ module Klaviyo
114
123
  # @param :emails [List] the emails of the profiles to check
115
124
  # @param :phone_numbers [List] the phone numbers of the profiles to check
116
125
  # @param :push_tokens [List] push tokens of the profiles to check
126
+ # @kwarg :api_key [String] private API key for this request
117
127
  # @return A list of JSON objects of the profiles. Profiles that are
118
128
  # supressed or not found are not included.
119
- def self.check_list_memberships(list_id, emails: [], phone_numbers: [], push_tokens: [])
129
+ def self.check_list_memberships(list_id, api_key: nil, emails: [], phone_numbers: [], push_tokens: [])
120
130
  path = "#{LIST}/#{list_id}/#{MEMBERS}"
121
131
  params = {
122
132
  :emails => emails,
123
133
  :phone_numbers => phone_numbers,
124
134
  :push_tokens => push_tokens
125
135
  }
126
- v2_request(HTTP_GET, path, **params)
136
+ v2_request(HTTP_GET, path, api_key: api_key, **params)
127
137
  end
128
138
 
129
139
  # Remove profiles from a list
@@ -131,36 +141,39 @@ module Klaviyo
131
141
  # @param :emails [List] the emails of the profiles to check
132
142
  # @param :phone_numbers [List] the phone numbers of the profiles to check
133
143
  # @param :push_tokens [List] push tokens of the profiles to check
144
+ # @kwarg :api_key [String] private API key for this request
134
145
  # @return will return with HTTP OK on success
135
- def self.remove_from_list(list_id, emails: [], phone_numbers: [], push_tokens: [])
146
+ def self.remove_from_list(list_id, api_key: nil, emails: [], phone_numbers: [], push_tokens: [])
136
147
  path = "#{LIST}/#{list_id}/#{MEMBERS}"
137
148
  params = {
138
149
  :emails => emails,
139
150
  :phone_numbers => phone_numbers,
140
151
  :push_tokens => push_tokens
141
152
  }
142
- v2_request(HTTP_DELETE, path, **params)
153
+ v2_request(HTTP_DELETE, path, api_key: api_key, **params)
143
154
  end
144
155
 
145
156
  # Get all emails, phone numbers, along with reasons for list exclusion
146
157
  # @param list_id [String] the id of the list
147
158
  # @param marker [Integer] a marker from a previous call to get the next batch
159
+ # @kwarg :api_key [String] private API key for this request
148
160
  # @return [List] A list of JSON object for each profile with the reason for exclusion
149
- def self.get_list_exclusions(list_id, marker: nil)
161
+ def self.get_list_exclusions(list_id, api_key: nil, marker: nil)
150
162
  path = "#{LIST}/#{list_id}/#{EXCLUSIONS}/#{ALL}"
151
163
  params = {
152
164
  :marker => marker
153
165
  }
154
- v2_request(HTTP_GET, path, **params)
166
+ v2_request(HTTP_GET, path, api_key: api_key, **params)
155
167
  end
156
168
 
157
169
  # Get all of the emails, phone numbers, and push tokens for profiles in a given list or segment
158
170
  # @param list_id [String] the id of the list
159
171
  # @param marker [Integer] a marker from a previous call to get the next batch
172
+ # @kwarg :api_key [String] private API key for this request
160
173
  # @return [List] A list of JSON objects for each profile with the id, email, phone number, and push token
161
- def self.get_group_members(list_id)
174
+ def self.get_group_members(list_id, api_key: nil)
162
175
  path = "#{GROUP}/#{list_id}/#{MEMBERS}/#{ALL}"
163
- v2_request(HTTP_GET, path)
176
+ v2_request(HTTP_GET, path, api_key: api_key)
164
177
  end
165
178
  end
166
179
  end
@@ -5,28 +5,30 @@ module Klaviyo
5
5
  # Returns a list of all metrics in Klaviyo
6
6
  # @param page [Integer] which page to return, default 0
7
7
  # @param count [Integer] number of results to return, default 100
8
+ # @kwarg :api_key [String] private API key for this request
8
9
  # @return a dictionary with a data property that contains an array of all the metrics
9
- def self.get_metrics(page: DEFAULT_PAGE, count: DEFAULT_COUNT)
10
+ def self.get_metrics(page: DEFAULT_PAGE, count: DEFAULT_COUNT, api_key: nil)
10
11
  params = {
11
12
  :page => page,
12
13
  :count => count
13
14
  }
14
- v1_request(HTTP_GET, METRICS, **params)
15
+ v1_request(HTTP_GET, METRICS, api_key: api_key, **params)
15
16
  end
16
17
 
17
18
  # Returns a batched timeline of all events in your Klaviyo account.
18
19
  # @param since [Integer or String] either a Unix timestamp or the UUID from a previous request. Default is the current time.
19
20
  # @param count [Integer] number of results to return, default 100
20
21
  # @param sort [String] 'asc' or 'desc', sort order to apply to the timeline. Default is 'desc'.
22
+ # @kwarg :api_key [String] private API key for this request
21
23
  # @return a dictionary with a data property that contains an array of the metrics
22
- def self.get_metrics_timeline(since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC)
24
+ def self.get_metrics_timeline(since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC, api_key: nil)
23
25
  path = "#{METRICS}/#{TIMELINE}"
24
26
  params = {
25
27
  :since => since,
26
28
  :count => count,
27
29
  :sort => sort
28
30
  }
29
- v1_request(HTTP_GET, path, **params)
31
+ v1_request(HTTP_GET, path, api_key: api_key, **params)
30
32
  end
31
33
 
32
34
  # Returns a batched timeline for one specific type of metric.
@@ -34,15 +36,16 @@ module Klaviyo
34
36
  # @param since [Integer or String] either a Unix timestamp or the UUID from a previous request. Default is the current time.
35
37
  # @param count [Integer] number of results to return, default 100
36
38
  # @param sort [String] 'asc' or 'desc', sort order to apply to the timeline. Default is 'desc'.
39
+ # @kwarg :api_key [String] private API key for this request
37
40
  # @return a dictionary with a data property that contains information about what metric the event tracks
38
- def self.get_metric_timeline(metric_id, since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC)
41
+ def self.get_metric_timeline(metric_id, since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC, api_key: nil)
39
42
  path = "#{METRIC}/#{metric_id}/#{TIMELINE}"
40
43
  params = {
41
44
  :since => since,
42
45
  :count => count,
43
46
  :sort => sort
44
47
  }
45
- v1_request(HTTP_GET, path, **params)
48
+ v1_request(HTTP_GET, path, api_key: api_key, **params)
46
49
  end
47
50
 
48
51
  # Export event data, optionally filtering and segmented on available event properties
@@ -54,6 +57,7 @@ module Klaviyo
54
57
  # @param where [JSON-encoded list] Conditions to use to filter the set of events. A max of 1 condition can be given.
55
58
  # @param by [String] The name of a property to segment the event data on. Where and by parameters cannot be specified at the same time.
56
59
  # @param count [Integer] Maximum number of segments to return. The default value is 25.
60
+ # @kwarg :api_key [String] private API key for this request
57
61
  # @return A dictionary relecting the input request parameters as well as a results property
58
62
  def self.get_metric_export(metric_id,
59
63
  start_date: nil,
@@ -62,7 +66,8 @@ module Klaviyo
62
66
  measurement: nil,
63
67
  where: nil,
64
68
  by: nil,
65
- count: nil
69
+ count: nil,
70
+ api_key: nil
66
71
  )
67
72
  path = "#{METRIC}/#{metric_id}/#{EXPORT}"
68
73
  params = {
@@ -74,7 +79,7 @@ module Klaviyo
74
79
  :by => by,
75
80
  :count => count
76
81
  }
77
- v1_request(HTTP_GET, path, **params)
82
+ v1_request(HTTP_GET, path, api_key: api_key, **params)
78
83
  end
79
84
  end
80
85
  end
@@ -6,21 +6,23 @@ module Klaviyo
6
6
 
7
7
  # Retrieves the id of the profile given email
8
8
  # @param email [String] the email of the profile
9
+ # @kwarg :api_key [String] private API key for this request
9
10
  # @return [JSON] a JSON object containing id of the profile
10
- def self.get_profile_id_by_email(email)
11
+ def self.get_profile_id_by_email(email, api_key: nil)
11
12
  path = "#{PEOPLE}/#{SEARCH}"
12
13
  params = {
13
14
  :email => email
14
15
  }
15
- v2_request(HTTP_GET, path, **params)
16
+ v2_request(HTTP_GET, path, api_key: api_key, **params)
16
17
  end
17
18
 
18
19
  # Retrieve all the data attributes for a Klaviyo Person ID.
19
20
  # @param person_id [String] the id of the profile
21
+ # @kwarg :api_key [String] private API key for this request
20
22
  # @return returns a person object
21
- def self.get_person_attributes(person_id)
23
+ def self.get_person_attributes(person_id, api_key: nil)
22
24
  path = "#{PERSON}/#{person_id}"
23
- v1_request(HTTP_GET, path)
25
+ v1_request(HTTP_GET, path, api_key: api_key)
24
26
  end
25
27
 
26
28
  # Add or update one more more attributes for a Person
@@ -37,15 +39,16 @@ module Klaviyo
37
39
  # @param since [Integer or String] either a Unix timestamp or the UUID from a previous request. Default is the current time.
38
40
  # @param count [Integer] number of results to return, default 100
39
41
  # @param sort [String] 'asc' or 'desc', sort order to apply to the timeline. Default is 'desc'.
42
+ # @kwarg :api_key [String] private API key for this request
40
43
  # @return returns a dictionary containing a list of metric event objects
41
- def self.get_person_metrics_timeline(person_id, since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC)
44
+ def self.get_person_metrics_timeline(person_id, since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC, api_key: nil)
42
45
  path = "#{PERSON}/#{person_id}/#{METRICS}/#{TIMELINE}"
43
46
  params = {
44
47
  :since => since,
45
48
  :count => count,
46
49
  :sort => sort
47
50
  }
48
- v1_request(HTTP_GET, path, **params)
51
+ v1_request(HTTP_GET, path, api_key: api_key, **params)
49
52
  end
50
53
 
51
54
  # Listing a person's event timeline for a particular metric
@@ -54,15 +57,16 @@ module Klaviyo
54
57
  # @param since [Integer or String] either a Unix timestamp or the UUID from a previous request. Default is the current time.
55
58
  # @param count [Integer] number of results to return, default 100
56
59
  # @param sort [String] 'asc' or 'desc', sort order to apply to the timeline. Default is 'desc'.
60
+ # @kwarg :api_key [String] private API key for this request
57
61
  # @return returns a dictionary containing a list of metric event objects
58
- def self.get_person_metric_timeline(person_id, metric_id, since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC)
62
+ def self.get_person_metric_timeline(person_id, metric_id, since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC, api_key: nil)
59
63
  path = "#{PERSON}/#{person_id}/#{METRIC}/#{metric_id}/#{TIMELINE}"
60
64
  params = {
61
65
  :since => since,
62
66
  :count => count,
63
67
  :sort => sort
64
68
  }
65
- v1_request(HTTP_GET, path, **params)
69
+ v1_request(HTTP_GET, path, api_key: api_key, **params)
66
70
  end
67
71
  end
68
72
  end
@@ -6,13 +6,15 @@ module Klaviyo
6
6
  # @kwarg :email [String] the customer or profile email
7
7
  # @kwarg :phone_number [String] the customer or profile phone number
8
8
  # @kwarg :properties [Hash] properties of the profile to add or update
9
+ # @kwarg :token [String] public API token for this request
9
10
  # @kwargs :method [String] the HTTP method to use for the request. Accepts 'get' or 'post'. Defaults to 'get'.
10
11
  def self.identify(kwargs = {})
11
12
  defaults = {:id => nil,
12
13
  :email => nil,
13
14
  :phone_number => nil,
14
15
  :properties => {},
15
- :method => HTTP_GET
16
+ :method => HTTP_GET,
17
+ :token => nil
16
18
  }
17
19
  kwargs = defaults.merge(kwargs)
18
20
 
@@ -25,8 +27,10 @@ module Klaviyo
25
27
  properties[:$phone_number] = kwargs[:phone_number] unless kwargs[:phone_number].to_s.empty?
26
28
  properties[:id] = kwargs[:id] unless kwargs[:id].to_s.empty?
27
29
 
30
+ token = kwargs[:token] || Klaviyo.public_api_key || nil
31
+
28
32
  params = {
29
- :token => Klaviyo.public_api_key,
33
+ :token => token,
30
34
  :properties => properties
31
35
  }
32
36
 
@@ -42,6 +46,7 @@ module Klaviyo
42
46
  # @kwarg :properties [Hash] properties of the event
43
47
  # @kwargs :customer_properties [Hash] properties of the customer or profile
44
48
  # @kwargs :time [Integer] timestamp of the event
49
+ # @kwarg :token [String] public API token for this request
45
50
  # @kwargs :method [String] the HTTP method to use for the request. Accepts 'get' or 'post'. Defaults to 'get'.
46
51
  def self.track(event, kwargs = {})
47
52
  defaults = {
@@ -51,7 +56,8 @@ module Klaviyo
51
56
  :properties => {},
52
57
  :customer_properties => {},
53
58
  :time => nil,
54
- :method => HTTP_GET
59
+ :method => HTTP_GET,
60
+ :token => nil
55
61
  }
56
62
 
57
63
  kwargs = defaults.merge(kwargs)
@@ -65,8 +71,10 @@ module Klaviyo
65
71
  customer_properties[:$phone_number] = kwargs[:phone_number] unless kwargs[:phone_number].to_s.empty?
66
72
  customer_properties[:id] = kwargs[:id] unless kwargs[:id].to_s.empty?
67
73
 
74
+ token = kwargs[:token] || Klaviyo.public_api_key || nil
75
+
68
76
  params = {
69
- :token => Klaviyo.public_api_key,
77
+ :token => token,
70
78
  :event => event,
71
79
  :properties => kwargs[:properties],
72
80
  :customer_properties => customer_properties
@@ -4,7 +4,7 @@ module Klaviyo
4
4
  V1_API = 'v1'
5
5
  V2_API = 'v2'
6
6
 
7
- KL_VERSION = '2.1.0'
7
+ KL_VERSION = '2.2.0'
8
8
  KL_USER_AGENT = "Ruby_Klaviyo/#{KL_VERSION}"
9
9
 
10
10
  HTTP_DELETE = 'delete'
@@ -44,7 +44,7 @@ module Klaviyo
44
44
  end
45
45
 
46
46
  def self.public_request(method, path, **kwargs)
47
- check_public_api_key_is_valid()
47
+ check_public_api_key_is_valid(kwargs[:token])
48
48
  if method == HTTP_GET
49
49
  params = build_params(kwargs)
50
50
  url = "#{BASE_API_URL}/#{path}?#{params}"
@@ -64,9 +64,10 @@ module Klaviyo
64
64
 
65
65
  def self.v1_request(method, path, content_type: CONTENT_JSON, **kwargs)
66
66
  if content_type == CONTENT_URL_FORM
67
+ priv_api_key = kwargs[:api_key] || Klaviyo.private_api_key || nil
67
68
  data = {
68
69
  :body => {
69
- :api_key => Klaviyo.private_api_key
70
+ :api_key => priv_api_key
70
71
  }
71
72
  }
72
73
  data[:body] = data[:body].merge(kwargs[:params])
@@ -79,18 +80,20 @@ module Klaviyo
79
80
  :sort => nil}
80
81
  params = defaults.merge(kwargs)
81
82
  query_params = encode_params(params)
82
- full_url = "#{V1_API}/#{path}?api_key=#{Klaviyo.private_api_key}#{query_params}"
83
+ priv_api_key = kwargs[:api_key] || Klaviyo.private_api_key || nil
84
+ full_url = "#{V1_API}/#{path}?api_key=#{priv_api_key}#{query_params}"
83
85
  request(method, full_url, content_type)
84
86
  end
85
87
  end
86
88
 
87
89
  def self.v2_request(method, path, **kwargs)
88
90
  path = "#{V2_API}/#{path}"
91
+ priv_api_key = kwargs[:api_key] || Klaviyo.private_api_key || nil
89
92
  key = {
90
- "api_key": "#{Klaviyo.private_api_key}"
93
+ "api_key": "#{priv_api_key}"
91
94
  }
92
95
  data = {}
93
- data[:body] = key.merge(kwargs)
96
+ data[:body] = kwargs.merge(key)
94
97
  request(method, path, CONTENT_JSON, **data)
95
98
  end
96
99
 
@@ -112,13 +115,13 @@ module Klaviyo
112
115
  end
113
116
  end
114
117
 
115
- def self.check_public_api_key_is_valid()
116
- if !Klaviyo.public_api_key
118
+ def self.check_public_api_key_is_valid(token)
119
+ if !token
117
120
  raise KlaviyoError.new(NO_PUBLIC_API_KEY_ERROR)
118
121
  end
119
- if ( Klaviyo.public_api_key =~ /pk_\w{34}$/ ) == 0
122
+ if ( token =~ /pk_\w{34}$/ ) == 0
120
123
  warn(PRIVATE_KEY_AS_PUBLIC)
121
- elsif ( Klaviyo.public_api_key =~ /\w{6}$/ ) != 0
124
+ elsif ( token =~ /\w{6}$/ ) != 0
122
125
  raise KlaviyoError.new(INCORRECT_PUBLIC_API_KEY_LENGTH)
123
126
  end
124
127
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klaviyo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Klaviyo Team
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-01 00:00:00.000000000 Z
11
+ date: 2021-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -87,7 +87,7 @@ files:
87
87
  homepage: https://www.klaviyo.com/
88
88
  licenses: []
89
89
  metadata: {}
90
- post_install_message:
90
+ post_install_message:
91
91
  rdoc_options: []
92
92
  require_paths:
93
93
  - lib
@@ -102,8 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  requirements: []
105
- rubygems_version: 3.0.3
106
- signing_key:
105
+ rubygems_version: 3.2.3
106
+ signing_key:
107
107
  specification_version: 4
108
108
  summary: You heard us, a Ruby wrapper for the Klaviyo API
109
109
  test_files: []