klaviyo 2.0.1 → 2.0.6

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
- SHA1:
3
- metadata.gz: a6eaa11bdc826d37a755546d9cb771a469ff9039
4
- data.tar.gz: 9f599d1c47bc281311a8adeb5f0c90e8467e4b65
2
+ SHA256:
3
+ metadata.gz: c50780aa07007d13ffcbfd5e88692dc46122641627511be21f4988012d48d20e
4
+ data.tar.gz: 453757a911094db7394000a2aa59d4a0f31a3cb6aa9196e50a749dac3e51b336
5
5
  SHA512:
6
- metadata.gz: 9a9ddabcb4a51a640a3f6b265d329a77934a5146803a4e4c67482fe4fbb7589e8dc722c193c55a7ae547ef917da6a61ad44e666442f7107f0963affdb8af7166
7
- data.tar.gz: a04c3224f53c41f732e99f423b0e0e895772b3edd87b2824b179aec24f14c258f12f541631d035550fab32241f43558d04dd92b159fb9145e83d76737ee36d5b
6
+ metadata.gz: 62278b5560afe418dc8658e71646f36905cdb10c2b607b923377ea08c8ea247630fc782c6dbceda1d4fe2a88f5f332995abce6dcf90484cfbc1791516d4b6cd3
7
+ data.tar.gz: 1358f2f45b1e92144f8a43f0f52a291f3ee859af3e416924e003a1b7c7a02553965d6240fecf8289eb41167c422565c8909b57344b8baed9091c6f022801183b
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.1'
6
- s.date = '2020-11-23'
5
+ s.version = '2.0.6'
6
+ s.date = '2021-04-08'
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']
@@ -0,0 +1,41 @@
1
+ module Klaviyo
2
+ class Campaigns < Client
3
+ CANCEL = 'cancel'
4
+ CAMPAIGN = 'campaign'
5
+ CAMPAIGNS = 'campaigns'
6
+ SEND = 'send'
7
+
8
+ # Retrieves all the campaigns from Klaviyo account
9
+ # @return [List] of JSON formatted campaing objects
10
+ def self.get_campaigns()
11
+ v1_request(HTTP_GET, CAMPAIGNS)
12
+ end
13
+
14
+ # Retrieves the details of the list
15
+ # @param campaign_id the if of campaign
16
+ # @return [JSON] a JSON object containing information about the campaign
17
+ def self.get_campaign_details(campaign_id)
18
+ path = "#{CAMPAIGN}/#{campaign_id}"
19
+
20
+ v1_request(HTTP_GET, path)
21
+ end
22
+
23
+ # Sends the campaign immediately
24
+ # @param campaign_id [String] the id of campaign
25
+ # @return will return with HTTP ok in case of success
26
+ def self.send_campaign(campaign_id)
27
+ path = "#{CAMPAIGN}/#{campaign_id}/#{SEND}"
28
+
29
+ v1_request(HTTP_POST, path)
30
+ end
31
+
32
+ # Cancels the campaign with specified campaign_id
33
+ # @param campaign_id [String] the id of campaign
34
+ # @return [JSON] a JSON object containing the campaign details
35
+ def self.cancel_campaign(campaign_id)
36
+ path = "#{CAMPAIGN}/#{campaign_id}/#{CANCEL}"
37
+
38
+ v1_request(HTTP_POST, path)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ module Klaviyo
2
+ class DataPrivacy < Client
3
+ DATA_PRIVACY = 'data-privacy'
4
+ DELETION_REQUEST = 'deletion-request'
5
+
6
+ # Submits a data privacy-related deletion request
7
+ # @param id_type [String] 'email' or 'phone_number' or 'person_id
8
+ # @param identifier [String] value for the identifier specified
9
+ # @return a dictionary with a confirmation that deletion task submitted for the customer
10
+ def self.request_profile_deletion(id_type, identifier)
11
+ unless ['email', 'phone_number', 'person_id'].include? id_type
12
+ raise Klaviyo::KlaviyoError.new(INVALID_ID_TYPE_ERROR)
13
+ end
14
+ identifier = { id_type => identifier }
15
+ path = "#{DATA_PRIVACY}/#{DELETION_REQUEST}"
16
+ v2_request(HTTP_POST, path, identifier)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,97 @@
1
+ module Klaviyo
2
+ class EmailTemplates < Client
3
+ EMAIL_TEMPLATES = 'email-templates'
4
+ EMAIL_TEMPLATE = 'email-template'
5
+ CLONE = 'clone'
6
+ RENDER = 'render'
7
+ SEND = 'send'
8
+
9
+ # Returns a list of all the email templates you've created.
10
+ # The templates are returned in sorted order by name.
11
+ # @return [List] of JSON formatted email template objects
12
+ def self.get_templates()
13
+ v1_request(HTTP_GET, EMAIL_TEMPLATES)
14
+ end
15
+
16
+ # Creates a new email template
17
+ # @param :name [String] The name of the email template
18
+ # @param :html [String] The HTML content for this template
19
+ # @return [JSON] a JSON object containing information about the email template
20
+ def self.create_template(name: nil, html: nil)
21
+ params = {
22
+ name: name,
23
+ html: html
24
+ }
25
+ v1_request(HTTP_POST, EMAIL_TEMPLATES, content_type: CONTENT_URL_FORM, params: params)
26
+ end
27
+
28
+ # Updates the name and/or HTML content of a template. Only updates imported
29
+ # HTML templates; does not currently update drag & drop templates
30
+ # @param template_id [String] The id of the email template
31
+ # @param :name [String] The name of the email template
32
+ # @param :html [String] The HTML content for this template
33
+ # @return [JSON] a JSON object containing information about the email template
34
+ def self.update_template(template_id, name:, html:)
35
+ path = "#{EMAIL_TEMPLATE}/#{template_id}"
36
+ params = {
37
+ name: name,
38
+ html: html
39
+ }
40
+ v1_request(HTTP_PUT, path, params)
41
+ end
42
+
43
+ # Deletes a given template.
44
+ # @param template_id [String] The id of the email template
45
+ # @return [JSON] a JSON object containing information about the email template
46
+ def self.delete_template(template_id)
47
+ path = "#{EMAIL_TEMPLATE}/#{template_id}"
48
+ v1_request(HTTP_DELETE, path)
49
+ end
50
+
51
+ # Creates a copy of a given template with a new name
52
+ # @param template_id [String] The id of the email template to copy
53
+ # @param :name [String] The name of the newly cloned email template
54
+ # @return [JSON] a JSON object containing information about the email template
55
+ def self.clone_template(template_id, name:)
56
+ path = "#{EMAIL_TEMPLATE}/#{template_id}/#{CLONE}"
57
+ params = {
58
+ name: name
59
+ }
60
+ v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
61
+ end
62
+
63
+ # Renders the specified template with the provided data and return HTML
64
+ # and text versions of the email
65
+ # @param template_id [String] The id of the email template to copy
66
+ # @param :context [Hash] The context the email template will be rendered with
67
+ # @return [JSON] a JSON object containing information about the email template
68
+ def self.render_template(template_id, context: {})
69
+ path = "#{EMAIL_TEMPLATE}/#{template_id}/#{RENDER}"
70
+ params = {
71
+ context: context
72
+ }
73
+ v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
74
+ end
75
+
76
+ # Renders the specified template with the provided data and then send the
77
+ # contents in an email via the service specified
78
+ # @param template_id [String] The id of the email template to copy
79
+ # @param :from_email [String] The from email address; used in the reply-to header
80
+ # @param :from_name [String] The name the email is sent from
81
+ # @param :subject [String] The subject of the email template
82
+ # @param :to [Mixed] The email this template is being sent to
83
+ # @param :context [Hash] The context the email template will be rendered with
84
+ # @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: {})
86
+ path = "#{EMAIL_TEMPLATE}/#{template_id}/#{SEND}"
87
+ params = {
88
+ from_email: from_email,
89
+ from_name: from_name,
90
+ subject: subject,
91
+ to: to,
92
+ context: context
93
+ }
94
+ v1_request(HTTP_POST, path, content_type: CONTENT_URL_FORM, params: params)
95
+ end
96
+ end
97
+ end
@@ -1,8 +1,6 @@
1
1
  module Klaviyo
2
2
  class Metrics < Client
3
3
  EXPORT = 'export'
4
- METRIC = 'metric'
5
- METRICS = 'metrics'
6
4
 
7
5
  # Returns a list of all metrics in Klaviyo
8
6
  # @param page [Integer] which page to return, default 0
@@ -1,6 +1,19 @@
1
1
  module Klaviyo
2
2
  class Profiles < Client
3
3
  PERSON = 'person'
4
+ PEOPLE = 'people'
5
+ SEARCH = 'search'
6
+
7
+ # Retrieves the id of the profile given email
8
+ # @param email [String] the email of the profile
9
+ # @return [JSON] a JSON object containing id of the profile
10
+ def self.get_profile_id_by_email(email)
11
+ path = "#{PEOPLE}/#{SEARCH}"
12
+ params = {
13
+ :email => email
14
+ }
15
+ v2_request(HTTP_GET, path, params)
16
+ end
4
17
 
5
18
  # Retrieve all the data attributes for a Klaviyo Person ID.
6
19
  # @param person_id [String] the id of the profile
@@ -8,16 +8,18 @@ module Klaviyo
8
8
  def self.identify(kwargs = {})
9
9
  defaults = {:id => nil,
10
10
  :email => nil,
11
+ :phone_number => nil,
11
12
  :properties => {}
12
13
  }
13
14
  kwargs = defaults.merge(kwargs)
14
15
 
15
- if !check_email_or_id_exists(kwargs)
16
+ unless check_required_args(kwargs)
16
17
  return
17
18
  end
18
19
 
19
20
  properties = kwargs[:properties]
20
21
  properties[:email] = kwargs[:email] unless kwargs[:email].to_s.empty?
22
+ properties[:$phone_number] = kwargs[:phone_number] unless kwargs[:phone_number].to_s.empty?
21
23
  properties[:id] = kwargs[:id] unless kwargs[:id].to_s.empty?
22
24
 
23
25
  params = {
@@ -33,6 +35,7 @@ module Klaviyo
33
35
  # @param event [String] the event to track
34
36
  # @kwarg :id [String] the customer or profile id
35
37
  # @kwarg :email [String] the customer or profile email
38
+ # @kwarg :phone_number [String] the customer or profile phone number
36
39
  # @kwarg :properties [Hash] properties of the event
37
40
  # @kwargs :customer_properties [Hash] properties of the customer or profile
38
41
  # @kwargs :time [Integer] timestamp of the event
@@ -40,6 +43,7 @@ module Klaviyo
40
43
  defaults = {
41
44
  :id => nil,
42
45
  :email => nil,
46
+ :phone_number => nil,
43
47
  :properties => {},
44
48
  :customer_properties => {},
45
49
  :time => nil
@@ -47,12 +51,13 @@ module Klaviyo
47
51
 
48
52
  kwargs = defaults.merge(kwargs)
49
53
 
50
- if !check_email_or_id_exists(kwargs)
54
+ unless check_required_args(kwargs)
51
55
  return
52
56
  end
53
57
 
54
58
  customer_properties = kwargs[:customer_properties]
55
59
  customer_properties[:email] = kwargs[:email] unless kwargs[:email].to_s.empty?
60
+ customer_properties[:$phone_number] = kwargs[:phone_number] unless kwargs[:phone_number].to_s.empty?
56
61
  customer_properties[:id] = kwargs[:id] unless kwargs[:id].to_s.empty?
57
62
 
58
63
  params = {
@@ -61,9 +66,9 @@ module Klaviyo
61
66
  :properties => kwargs[:properties],
62
67
  :customer_properties => customer_properties
63
68
  }
64
- params[:time] = kwargs[:time].to_time.to_i if kwargs[:time]
69
+ params[:time] = kwargs[:time] if kwargs[:time]
65
70
 
66
- public_request(HTTP_GET, 'track', params)
71
+ public_request(HTTP_GET, 'track', **params)
67
72
  end
68
73
 
69
74
  def self.track_once(event, kwargs = {})
@@ -10,62 +10,81 @@ module Klaviyo
10
10
  HTTP_PUT = 'put'
11
11
 
12
12
  ALL = 'all'
13
+ METRIC = 'metric'
14
+ METRICS = 'metrics'
13
15
  TIMELINE = 'timeline'
14
16
 
15
17
  DEFAULT_COUNT = 100
16
18
  DEFAULT_PAGE = 0
17
19
  DEFAULT_SORT_DESC = 'desc'
18
20
 
21
+ CONTENT_JSON = 'application/json'
22
+ CONTENT_URL_FORM = 'application/x-www-form-urlencoded'
23
+
19
24
  private
20
25
 
21
- def self.request(method, path, kwargs = {})
26
+ def self.request(method, path, content_type, **kwargs)
22
27
  check_private_api_key_exists()
23
28
  url = "#{BASE_API_URL}/#{path}"
24
29
  connection = Faraday.new(
25
30
  url: url,
26
31
  headers: {
27
- 'Content-Type' => 'application/json'
32
+ 'Content-Type' => content_type
28
33
  })
34
+ if content_type == CONTENT_JSON
35
+ kwargs[:body] = kwargs[:body].to_json
36
+ end
29
37
  response = connection.send(method) do |req|
30
- req.body = kwargs[:body].to_json || nil
38
+ req.body = kwargs[:body] || nil
31
39
  end
32
40
  end
33
41
 
34
- def self.public_request(method, path, kwargs = {})
42
+ def self.public_request(method, path, **kwargs)
35
43
  check_public_api_key_exists()
36
44
  params = build_params(kwargs)
37
45
  url = "#{BASE_API_URL}/#{path}?#{params}"
38
46
  res = Faraday.get(url).body
39
47
  end
40
48
 
41
- def self.v1_request(method, path, kwargs = {})
42
- defaults = {:page => nil,
43
- :count => nil,
44
- :since => nil,
45
- :sort => nil}
46
- params = defaults.merge(kwargs)
47
- query_params = encode_params(params)
48
- full_url = "#{V1_API}/#{path}?api_key=#{Klaviyo.private_api_key}#{query_params}"
49
- request(method, full_url)
49
+ def self.v1_request(method, path, content_type: CONTENT_JSON, **kwargs)
50
+ if content_type == CONTENT_URL_FORM
51
+ data = {
52
+ :body => {
53
+ :api_key => Klaviyo.private_api_key
54
+ }
55
+ }
56
+ data[:body] = data[:body].merge(kwargs[:params])
57
+ full_url = "#{V1_API}/#{path}"
58
+ request(method, full_url, content_type, data)
59
+ else
60
+ defaults = {:page => nil,
61
+ :count => nil,
62
+ :since => nil,
63
+ :sort => nil}
64
+ params = defaults.merge(kwargs)
65
+ 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)
68
+ end
50
69
  end
51
70
 
52
- def self.v2_request(method, path, kwargs = {})
71
+ def self.v2_request(method, path, **kwargs)
53
72
  path = "#{V2_API}/#{path}"
54
73
  key = {
55
74
  "api_key": "#{Klaviyo.private_api_key}"
56
75
  }
57
76
  data = {}
58
77
  data[:body] = key.merge(kwargs)
59
- request(method, path, data)
78
+ request(method, path, CONTENT_JSON, data)
60
79
  end
61
80
 
62
81
  def self.build_params(params)
63
- "data=#{Base64.encode64(JSON.generate(params)).gsub(/\n/,'')}"
82
+ "data=#{CGI.escape(Base64.encode64(JSON.generate(params)).gsub(/\n/, ''))}"
64
83
  end
65
84
 
66
- def self.check_email_or_id_exists(kwargs)
67
- if kwargs[:email].to_s.empty? and kwargs[:id].to_s.empty?
68
- raise Klaviyo::KlaviyoError.new(NO_ID_OR_EMAIL_ERROR)
85
+ def self.check_required_args(kwargs)
86
+ if kwargs[:email].to_s.empty? and kwargs[:phone_number].to_s.empty? and kwargs[:id].to_s.empty?
87
+ raise Klaviyo::KlaviyoError.new(REQUIRED_ARG_ERROR)
69
88
  else
70
89
  return true
71
90
  end
@@ -8,6 +8,9 @@ require_relative 'apis/public'
8
8
  require_relative 'apis/lists'
9
9
  require_relative 'apis/metrics'
10
10
  require_relative 'apis/profiles'
11
+ require_relative 'apis/campaigns'
12
+ require_relative 'apis/email_templates'
13
+ require_relative 'apis/data_privacy'
11
14
 
12
15
  module Klaviyo
13
16
  class << self
@@ -19,5 +22,6 @@ module Klaviyo
19
22
 
20
23
  NO_PRIVATE_API_KEY_ERROR = 'Please provide your Private API key for this request'
21
24
  NO_PUBLIC_API_KEY_ERROR = 'Please provide your Public API key for this request'
22
- NO_ID_OR_EMAIL_ERROR = 'You must identify a user by email or ID'
25
+ REQUIRED_ARG_ERROR = 'You must identify a user by email, ID or phone_number'
26
+ INVALID_ID_TYPE_ERROR = 'Invalid id_type provided, must be one of: email, phone_number, person_id'
23
27
  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.1
4
+ version: 2.0.6
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: 2020-11-23 00:00:00.000000000 Z
11
+ date: 2021-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -74,6 +74,9 @@ extra_rdoc_files: []
74
74
  files:
75
75
  - klaviyo.gemspec
76
76
  - lib/klaviyo.rb
77
+ - lib/klaviyo/apis/campaigns.rb
78
+ - lib/klaviyo/apis/data_privacy.rb
79
+ - lib/klaviyo/apis/email_templates.rb
77
80
  - lib/klaviyo/apis/lists.rb
78
81
  - lib/klaviyo/apis/metrics.rb
79
82
  - lib/klaviyo/apis/profiles.rb
@@ -84,7 +87,7 @@ files:
84
87
  homepage: https://www.klaviyo.com/
85
88
  licenses: []
86
89
  metadata: {}
87
- post_install_message:
90
+ post_install_message:
88
91
  rdoc_options: []
89
92
  require_paths:
90
93
  - lib
@@ -99,9 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
102
  - !ruby/object:Gem::Version
100
103
  version: '0'
101
104
  requirements: []
102
- rubyforge_project:
103
- rubygems_version: 2.5.1
104
- signing_key:
105
+ rubygems_version: 3.0.8
106
+ signing_key:
105
107
  specification_version: 4
106
108
  summary: You heard us, a Ruby wrapper for the Klaviyo API
107
109
  test_files: []