klaviyo 2.0.8 → 2.0.9

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: f20097f9b9538503cc8f541dd7e6d71c6c70904e51fdd59f636670f56d0b53c5
4
+ data.tar.gz: 98032c0e8a4ac60b16501495b71d35ce09bd28bd62fce4c49392448929555ffa
5
5
  SHA512:
6
- metadata.gz: 561d598fa79624b0a2e271efb64676d55f39f09ca4ee787097061006f865f08f14d12faa49da7eea2a28523c3c0d96ed727feff2b40924b3965b1b1b234fb727
7
- data.tar.gz: 934fe573a195fa8e307fc1c4383f810965d0b2636e81d8a442ece9c5b613ddef4d3eb3a1d5a159a82328edbb12cd0c87ab071a27ce5d7d57f2e8d7060a29433b
6
+ metadata.gz: e4f99398385906828f37a4e1f25f05ca76493a905d526177da2cc189301fba84adc76551711213c85003337b0278d0cade9c8ba52e0c0726176465fd2c8132cd
7
+ data.tar.gz: e109724a853cbf44a3546298fdfc04037a9cfb748b72a3edd96fab1475e209a3ea67c697ce940ff54d6aac57120f37aa853076398c016995b1b4b11f70279694
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.0.9'
6
+ s.date = '2021-07-30'
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']
@@ -4,12 +4,15 @@ 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
+ # @kwargs :method [String] the HTTP method to use for the request. Accepts 'get' or 'post'. Defaults to 'get'.
8
10
  def self.identify(kwargs = {})
9
11
  defaults = {:id => nil,
10
12
  :email => nil,
11
13
  :phone_number => nil,
12
- :properties => {}
14
+ :properties => {},
15
+ :method => HTTP_GET
13
16
  }
14
17
  kwargs = defaults.merge(kwargs)
15
18
 
@@ -27,7 +30,7 @@ module Klaviyo
27
30
  :properties => properties
28
31
  }
29
32
 
30
- public_request(HTTP_GET, 'identify', **params)
33
+ public_request(kwargs[:method], 'identify', **params)
31
34
  end
32
35
 
33
36
  # Used for tracking events and customer behaviors
@@ -39,6 +42,7 @@ module Klaviyo
39
42
  # @kwarg :properties [Hash] properties of the event
40
43
  # @kwargs :customer_properties [Hash] properties of the customer or profile
41
44
  # @kwargs :time [Integer] timestamp of the event
45
+ # @kwargs :method [String] the HTTP method to use for the request. Accepts 'get' or 'post'. Defaults to 'get'.
42
46
  def self.track(event, kwargs = {})
43
47
  defaults = {
44
48
  :id => nil,
@@ -46,7 +50,8 @@ module Klaviyo
46
50
  :phone_number => nil,
47
51
  :properties => {},
48
52
  :customer_properties => {},
49
- :time => nil
53
+ :time => nil,
54
+ :method => HTTP_GET
50
55
  }
51
56
 
52
57
  kwargs = defaults.merge(kwargs)
@@ -68,7 +73,7 @@ module Klaviyo
68
73
  }
69
74
  params[:time] = kwargs[:time] if kwargs[:time]
70
75
 
71
- public_request(HTTP_GET, 'track', **params)
76
+ public_request(kwargs[:method], 'track', **params)
72
77
  end
73
78
 
74
79
  def self.track_once(event, kwargs = {})
@@ -40,10 +40,21 @@ module Klaviyo
40
40
  end
41
41
 
42
42
  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
43
+ check_public_api_key_is_valid()
44
+ if method == HTTP_GET
45
+ params = build_params(kwargs)
46
+ url = "#{BASE_API_URL}/#{path}?#{params}"
47
+ res = Faraday.get(url).body
48
+ elsif method == HTTP_POST
49
+ url = URI("#{BASE_API_URL}/#{path}")
50
+ response = Faraday.post(url) do |req|
51
+ req.headers['Content-Type'] = CONTENT_URL_FORM
52
+ req.headers['Accept'] = 'text/html'
53
+ req.body = {data: "#{kwargs.to_json}"}
54
+ end
55
+ else
56
+ raise KlaviyoError.new(INVALID_HTTP_METHOD)
57
+ end
47
58
  end
48
59
 
49
60
  def self.v1_request(method, path, content_type: CONTENT_JSON, **kwargs)
@@ -96,10 +107,16 @@ module Klaviyo
96
107
  end
97
108
  end
98
109
 
99
- def self.check_public_api_key_exists()
110
+ def self.check_public_api_key_is_valid()
100
111
  if !Klaviyo.public_api_key
101
112
  raise KlaviyoError.new(NO_PUBLIC_API_KEY_ERROR)
102
113
  end
114
+ if ( Klaviyo.public_api_key =~ /pk_\w{34}$/ ) == 0
115
+ raise KlaviyoError.new(PRIVATE_KEY_AS_PUBLIC)
116
+ end
117
+ if ( Klaviyo.public_api_key =~ /\w{6}$/ ) != 0
118
+ raise KlaviyoError.new(INCORRECT_PUBLIC_API_KEY_LENGTH)
119
+ end
103
120
  end
104
121
 
105
122
  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.0.9
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-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json