klaviyo 2.0.8 → 2.3.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: b02aaf0e72a06b6d2bf999e9279a8b6c20c0f4f7652275fea1981ea0ab0683c5
4
- data.tar.gz: 4b2ac021105853944c5bdbf47efa268addda7b1006f2683be04cadefb4db7ecc
3
+ metadata.gz: 48ce149f6493a79a418c85585a43079cf3735102c7f3090439a18f5f1c253425
4
+ data.tar.gz: 4633715d8397a09f1c8b70c0cf90e75eebfd2ced0cdf60d20c7b8a90aa0623f1
5
5
  SHA512:
6
- metadata.gz: 561d598fa79624b0a2e271efb64676d55f39f09ca4ee787097061006f865f08f14d12faa49da7eea2a28523c3c0d96ed727feff2b40924b3965b1b1b234fb727
7
- data.tar.gz: 934fe573a195fa8e307fc1c4383f810965d0b2636e81d8a442ece9c5b613ddef4d3eb3a1d5a159a82328edbb12cd0c87ab071a27ce5d7d57f2e8d7060a29433b
6
+ metadata.gz: f0295d3b07f3aa56ba3285ade79c35eb2bd9498b8931461b10cad486be12cdf0e507e64456f0c383255b8972a491212505df94a3a508c4ea9dfbd0f5973ab98a
7
+ data.tar.gz: cf4f1f10ebad10c3b5c19c0eb6f95862ffabf012f17451610061c6d140f188c752686a10db87c5a52465d346476d61463cee68a4f80c8f17c5e1dac6e34edde1
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.0.8'
6
- s.date = '2021-04-28'
5
+ s.version = '2.3.0'
6
+ s.date = '2021-10-28'
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
@@ -4,12 +4,17 @@ module Klaviyo
4
4
  #
5
5
  # @kwarg :id [String] the customer or profile id
6
6
  # @kwarg :email [String] the customer or profile email
7
+ # @kwarg :phone_number [String] the customer or profile phone number
7
8
  # @kwarg :properties [Hash] properties of the profile to add or update
9
+ # @kwarg :token [String] public API token for this request
10
+ # @kwargs :method [String] the HTTP method to use for the request. Accepts 'get' or 'post'. Defaults to 'get'.
8
11
  def self.identify(kwargs = {})
9
12
  defaults = {:id => nil,
10
13
  :email => nil,
11
14
  :phone_number => nil,
12
- :properties => {}
15
+ :properties => {},
16
+ :method => HTTP_GET,
17
+ :token => nil
13
18
  }
14
19
  kwargs = defaults.merge(kwargs)
15
20
 
@@ -22,12 +27,14 @@ module Klaviyo
22
27
  properties[:$phone_number] = kwargs[:phone_number] unless kwargs[:phone_number].to_s.empty?
23
28
  properties[:id] = kwargs[:id] unless kwargs[:id].to_s.empty?
24
29
 
30
+ token = kwargs[:token] || Klaviyo.public_api_key || nil
31
+
25
32
  params = {
26
- :token => Klaviyo.public_api_key,
33
+ :token => token,
27
34
  :properties => properties
28
35
  }
29
36
 
30
- public_request(HTTP_GET, 'identify', **params)
37
+ public_request(kwargs[:method], 'identify', **params)
31
38
  end
32
39
 
33
40
  # Used for tracking events and customer behaviors
@@ -39,6 +46,8 @@ module Klaviyo
39
46
  # @kwarg :properties [Hash] properties of the event
40
47
  # @kwargs :customer_properties [Hash] properties of the customer or profile
41
48
  # @kwargs :time [Integer] timestamp of the event
49
+ # @kwarg :token [String] public API token for this request
50
+ # @kwargs :method [String] the HTTP method to use for the request. Accepts 'get' or 'post'. Defaults to 'get'.
42
51
  def self.track(event, kwargs = {})
43
52
  defaults = {
44
53
  :id => nil,
@@ -46,7 +55,9 @@ module Klaviyo
46
55
  :phone_number => nil,
47
56
  :properties => {},
48
57
  :customer_properties => {},
49
- :time => nil
58
+ :time => nil,
59
+ :method => HTTP_GET,
60
+ :token => nil
50
61
  }
51
62
 
52
63
  kwargs = defaults.merge(kwargs)
@@ -60,15 +71,17 @@ module Klaviyo
60
71
  customer_properties[:$phone_number] = kwargs[:phone_number] unless kwargs[:phone_number].to_s.empty?
61
72
  customer_properties[:id] = kwargs[:id] unless kwargs[:id].to_s.empty?
62
73
 
74
+ token = kwargs[:token] || Klaviyo.public_api_key || nil
75
+
63
76
  params = {
64
- :token => Klaviyo.public_api_key,
77
+ :token => token,
65
78
  :event => event,
66
79
  :properties => kwargs[:properties],
67
80
  :customer_properties => customer_properties
68
81
  }
69
82
  params[:time] = kwargs[:time] if kwargs[:time]
70
83
 
71
- public_request(HTTP_GET, 'track', **params)
84
+ public_request(kwargs[:method], 'track', **params)
72
85
  end
73
86
 
74
87
  def self.track_once(event, kwargs = {})
@@ -4,6 +4,9 @@ module Klaviyo
4
4
  V1_API = 'v1'
5
5
  V2_API = 'v2'
6
6
 
7
+ KL_VERSION = '2.3.0'
8
+ KL_USER_AGENT = "Ruby_Klaviyo/#{KL_VERSION}"
9
+
7
10
  HTTP_DELETE = 'delete'
8
11
  HTTP_GET = 'get'
9
12
  HTTP_POST = 'post'
@@ -24,12 +27,13 @@ module Klaviyo
24
27
  private
25
28
 
26
29
  def self.request(method, path, content_type, **kwargs)
27
- check_private_api_key_exists()
30
+ check_private_api_key_exists(kwargs)
28
31
  url = "#{BASE_API_URL}/#{path}"
29
32
  connection = Faraday.new(
30
33
  url: url,
31
34
  headers: {
32
- 'Content-Type' => content_type
35
+ 'Content-Type' => content_type,
36
+ 'User-Agent' => KL_USER_AGENT
33
37
  })
34
38
  if content_type == CONTENT_JSON
35
39
  kwargs[:body] = kwargs[:body].to_json
@@ -40,17 +44,30 @@ module Klaviyo
40
44
  end
41
45
 
42
46
  def self.public_request(method, path, **kwargs)
43
- check_public_api_key_exists()
44
- params = build_params(kwargs)
45
- url = "#{BASE_API_URL}/#{path}?#{params}"
46
- res = Faraday.get(url).body
47
+ check_public_api_key_is_valid(kwargs[:token])
48
+ if method == HTTP_GET
49
+ params = build_params(kwargs)
50
+ url = "#{BASE_API_URL}/#{path}?#{params}"
51
+ res = Faraday.get(url, {}, { 'User-Agent' => KL_USER_AGENT }).body
52
+ elsif method == HTTP_POST
53
+ url = URI("#{BASE_API_URL}/#{path}")
54
+ response = Faraday.post(url) do |req|
55
+ req.headers['Content-Type'] = CONTENT_URL_FORM
56
+ req.headers['Accept'] = 'text/html'
57
+ req.headers['User-Agent'] = KL_USER_AGENT
58
+ req.body = {data: "#{kwargs.to_json}"}
59
+ end
60
+ else
61
+ raise KlaviyoError.new(INVALID_HTTP_METHOD)
62
+ end
47
63
  end
48
64
 
49
65
  def self.v1_request(method, path, content_type: CONTENT_JSON, **kwargs)
50
66
  if content_type == CONTENT_URL_FORM
67
+ priv_api_key = kwargs[:api_key] || Klaviyo.private_api_key || nil
51
68
  data = {
52
69
  :body => {
53
- :api_key => Klaviyo.private_api_key
70
+ :api_key => priv_api_key
54
71
  }
55
72
  }
56
73
  data[:body] = data[:body].merge(kwargs[:params])
@@ -63,18 +80,20 @@ module Klaviyo
63
80
  :sort => nil}
64
81
  params = defaults.merge(kwargs)
65
82
  query_params = encode_params(params)
66
- full_url = "#{V1_API}/#{path}?api_key=#{Klaviyo.private_api_key}#{query_params}"
67
- request(method, full_url, content_type)
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}"
85
+ request(method, full_url, content_type, body: {api_key: priv_api_key})
68
86
  end
69
87
  end
70
88
 
71
89
  def self.v2_request(method, path, **kwargs)
72
90
  path = "#{V2_API}/#{path}"
91
+ priv_api_key = kwargs[:api_key] || Klaviyo.private_api_key || nil
73
92
  key = {
74
- "api_key": "#{Klaviyo.private_api_key}"
93
+ :api_key => "#{priv_api_key}"
75
94
  }
76
95
  data = {}
77
- data[:body] = key.merge(kwargs)
96
+ data[:body] = kwargs.merge(key)
78
97
  request(method, path, CONTENT_JSON, **data)
79
98
  end
80
99
 
@@ -90,16 +109,21 @@ module Klaviyo
90
109
  end
91
110
  end
92
111
 
93
- def self.check_private_api_key_exists()
94
- if !Klaviyo.private_api_key
112
+ def self.check_private_api_key_exists(kwargs)
113
+ if !Klaviyo.private_api_key && kwargs[:body][:api_key].nil?
95
114
  raise KlaviyoError.new(NO_PRIVATE_API_KEY_ERROR)
96
115
  end
97
116
  end
98
117
 
99
- def self.check_public_api_key_exists()
100
- if !Klaviyo.public_api_key
118
+ def self.check_public_api_key_is_valid(token)
119
+ if !token
101
120
  raise KlaviyoError.new(NO_PUBLIC_API_KEY_ERROR)
102
121
  end
122
+ if ( token =~ /pk_\w{34}$/ ) == 0
123
+ warn(PRIVATE_KEY_AS_PUBLIC)
124
+ elsif ( token =~ /\w{6}$/ ) != 0
125
+ raise KlaviyoError.new(INCORRECT_PUBLIC_API_KEY_LENGTH)
126
+ end
103
127
  end
104
128
 
105
129
  def self.encode_params(kwargs)
@@ -24,4 +24,7 @@ module Klaviyo
24
24
  NO_PUBLIC_API_KEY_ERROR = 'Please provide your Public API key for this request'
25
25
  REQUIRED_ARG_ERROR = 'You must identify a user by email, ID or phone_number'
26
26
  INVALID_ID_TYPE_ERROR = 'Invalid id_type provided, must be one of: email, phone_number, person_id'
27
+ PRIVATE_KEY_AS_PUBLIC = 'Private API key added in place of Public API key'
28
+ INCORRECT_PUBLIC_API_KEY_LENGTH = 'Public API Key must be 6 characters'
29
+ INVALID_HTTP_METHOD = 'Invalid HTTP method present, please use "get" or "post"'
27
30
  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.0.8
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Klaviyo Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-28 00:00:00.000000000 Z
11
+ date: 2021-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -102,7 +102,7 @@ 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.8
105
+ rubygems_version: 3.2.3
106
106
  signing_key:
107
107
  specification_version: 4
108
108
  summary: You heard us, a Ruby wrapper for the Klaviyo API