klaviyo_api 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f8a948d59877f23eb97d2f6c53a8438892694862f7428d298151da0d1f26bfa3
4
+ data.tar.gz: e97ef135e895594a65bcc6f75c919f4ae89ad106220936eda08b925209fba615
5
+ SHA512:
6
+ metadata.gz: 33760c4bd9d18fa3f0f582a78558fb3d6bc84548ea5889e9c3fd81cde4a6257c8c122809020b1080e3258c92d7d6b109b418db8a0d45fcc4121952005c0fcc94
7
+ data.tar.gz: 33372e47db2bfa551fc166ed859ca8dbb42f51b419a0a76ee1722eca79188e7dae991f4f64668df95a65e7d82fb23738256a5eafc4fa8e6c520b842c59d6ec7c
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # CHANGELOG
2
+
3
+ ## [1.0.0]
4
+
5
+ ### Added
6
+
7
+ - Created
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Third Blink Software Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # KlaviyoAPI
2
+
3
+ [![Build Status](https://travis-ci.org/rewindio/klaviyo_api.svg?branch=master)](https://travis-ci.org/rewindio/klaviyo_api)
4
+
5
+ The best way to consume Klaviyo's v1 & v2 APIs!
6
+
7
+ ## Installation
8
+
9
+ ### Add this line to your application's Gemfile
10
+
11
+ ```ruby
12
+ gem 'klaviyo_api'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Basic Account object
18
+
19
+ ```ruby
20
+ # frozen_string_literal: true
21
+
22
+ require 'klaviyo_api'
23
+
24
+ class Account
25
+ attr_accessor :api_key
26
+
27
+ def initialize(api_key)
28
+ @api_key = api_key
29
+ end
30
+
31
+ def with_klaviyo_session(&block)
32
+ KlaviyoAPI::Session.temp api_key, &block
33
+ end
34
+ end
35
+
36
+ account = Account.new 'pk_xxxyyyzzz'
37
+ ```
38
+
39
+ ### GET `/lists`
40
+
41
+ ```ruby
42
+ account.with_klaviyo_session { KlaviyoAPI::List.all }
43
+ ```
44
+
45
+ ### GET `/list/:list_id`
46
+
47
+ ```ruby
48
+ account.with_klaviyo_session { KlaviyoAPI::List.find 'AbC123xYz' }
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repository, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
54
+
55
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment. You can create a file in the root of the project called `dev-config.yml` and add your API key to it:
56
+
57
+ ```yaml
58
+ api_key: <your-api-key>
59
+ ```
60
+
61
+ This will tell the console to pre-authenticate the Klaviyo session, making it easier to test.
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/rewindio/klaviyo_api>.
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'klaviyo_api/connection'
4
+
5
+ module ActiveResource
6
+ class Connection
7
+ attr_reader :response
8
+
9
+ prepend KlaviyoAPI::Connection::ResponseCapture
10
+ prepend KlaviyoAPI::Connection::RequestNotification
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
4
+
5
+ # gem requires
6
+ require 'activeresource'
7
+ require 'caching_enumerator'
8
+
9
+ # gem extensions
10
+ require 'active_resource/connection_ext'
11
+
12
+ require 'klaviyo_api/version'
13
+ require 'klaviyo_api/configuration'
14
+
15
+ module KlaviyoAPI
16
+ end
17
+
18
+ require 'klaviyo_api/json_formatter'
19
+ require 'klaviyo_api/connection'
20
+ require 'klaviyo_api/detailed_log_subscriber'
21
+ require 'klaviyo_api/exceptions'
22
+ require 'klaviyo_api/session'
23
+
24
+ require 'klaviyo_api/resources'
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KlaviyoAPI
4
+ class Configuration
5
+ attr_accessor :url
6
+
7
+ def initialize
8
+ # base url
9
+ @url = 'https://a.klaviyo.com/api/'
10
+ end
11
+ end
12
+
13
+ class << self
14
+ def configure
15
+ yield configuration
16
+
17
+ KlaviyoAPI::Base.site = configuration.url
18
+
19
+ configuration
20
+ end
21
+
22
+ def configuration
23
+ @configuration ||= Configuration.new
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KlaviyoAPI
4
+ class Connection < ActiveResource::Connection
5
+ attr_reader :response
6
+
7
+ module ResponseCapture
8
+ def handle_response(response)
9
+ @response = super
10
+ end
11
+ end
12
+
13
+ include ResponseCapture
14
+
15
+ module RequestNotification
16
+ def request(method, path, *arguments)
17
+ super.tap do |response|
18
+ notify_about_request response, arguments
19
+ end
20
+ rescue => e
21
+ notify_about_request e.response, arguments if e.respond_to? :response
22
+
23
+ raise
24
+ end
25
+
26
+ def notify_about_request(response, arguments)
27
+ ActiveSupport::Notifications.instrument('request.active_resource_detailed') do |payload|
28
+ payload[:response] = response
29
+ payload[:data] = arguments
30
+ end
31
+ end
32
+ end
33
+
34
+ include RequestNotification
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveResource
4
+ class DetailedLogSubscriber < ActiveSupport::LogSubscriber
5
+ def request(event)
6
+ data = event.payload[:data]
7
+ request_body = data.first
8
+
9
+ log_level_method = event.payload[:response].code.to_i < 400 ? :info : :error
10
+
11
+ send log_level_method, "Request: #{request_body}" if request_body
12
+ send log_level_method, "Response: #{event.payload[:response].body}" unless event.payload[:response].header['content-type'] == 'text/html'
13
+ end
14
+
15
+ def logger
16
+ ActiveResource::Base.logger
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveResource::DetailedLogSubscriber.attach_to :active_resource_detailed
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KlaviyoAPI
4
+ class MissingParameter < StandardError
5
+ def initialize(msg = 'Missing Parameter(s)')
6
+ super
7
+ end
8
+ end
9
+
10
+ class InvalidOperation < StandardError
11
+ def initialize(msg = 'The requested operation is not valid')
12
+ super
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KlaviyoAPI
4
+ class JsonFormatter
5
+ include ActiveResource::Formats::JsonFormat
6
+
7
+ attr_reader :collection_name
8
+
9
+ def initialize(collection_name)
10
+ @collection_name = collection_name.to_s
11
+ end
12
+
13
+ def decode(json)
14
+ ActiveSupport::JSON.decode json.presence
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'klaviyo_api/resources/base'
4
+
5
+ Dir.glob("#{File.dirname(__FILE__)}/resources/support/*").each { |file| require file if file.end_with? '.rb' }
6
+ Dir.glob("#{File.dirname(__FILE__)}/resources/*").each { |file| require file if file.end_with? '.rb' }
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KlaviyoAPI
4
+ class Base < ActiveResource::Base
5
+ headers['User-Agent'] = "KlaviyoAPI/#{KlaviyoAPI::VERSION}"
6
+ headers['Accept'] = 'application/json'
7
+
8
+ self.site = KlaviyoAPI.configuration.url
9
+
10
+ self.include_format_in_path = false
11
+
12
+ self.format = KlaviyoAPI::JsonFormatter.new :result
13
+
14
+ class << self
15
+ def activate_session(session)
16
+ self.headers['api-key'] = session.api_key.to_s # rubocop:disable Style/RedundantSelf
17
+
18
+ KlaviyoAPI::Base.site = KlaviyoAPI.configuration.url
19
+ end
20
+
21
+ def reset_session
22
+ self.headers['api-key'] = nil # rubocop:disable Style/RedundantSelf
23
+
24
+ KlaviyoAPI::Base.site = KlaviyoAPI.configuration.url
25
+ end
26
+
27
+ def headers
28
+ return _headers if _headers_defined?
29
+ return superclass.headers if superclass != Object && superclass.headers
30
+
31
+ _headers || {}
32
+ end
33
+
34
+ def element_path(id, prefix_options = {}, query_options = nil)
35
+ check_prefix_options(prefix_options)
36
+
37
+ prefix_options, query_options = split_options(prefix_options) if query_options.nil?
38
+ "#{prefix(prefix_options)}#{element_name}/#{URI.parser.escape id.to_s}#{format_extension}#{query_string(query_options)}"
39
+ end
40
+ end
41
+
42
+ def to_h
43
+ JSON.parse(attributes.to_json).symbolize_keys
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KlaviyoAPI
4
+ class List < Base
5
+ self.prefix += 'v2/'
6
+ self.primary_key = :list_id
7
+
8
+ class << self
9
+ # Override this from ActiveResource#base in order to inject the id into the response
10
+ # because its not returned
11
+ def find_single(scope, options)
12
+ super.tap { |record| record.id = scope }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KlaviyoAPI
4
+ class Session
5
+ attr_accessor :api_key
6
+
7
+ def initialize(api_key)
8
+ self.api_key = api_key
9
+ end
10
+
11
+ class << self
12
+ def temp(api_key)
13
+ session = new api_key
14
+
15
+ KlaviyoAPI::Base.activate_session session
16
+
17
+ yield
18
+ ensure
19
+ KlaviyoAPI::Base.reset_session
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KlaviyoAPI
4
+ VERSION = '1.1.4'
5
+ end
metadata ADDED
@@ -0,0 +1,240 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: klaviyo_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.4
5
+ platform: ruby
6
+ authors:
7
+ - rewind.io
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeresource
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: caching_enumerator
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 12.3.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 12.3.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: http_logger
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.5.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.5.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.6.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.6.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: factory_bot
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 4.11.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 4.11.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 2.15.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 2.15.0
139
+ - !ruby/object:Gem::Dependency
140
+ name: guard-minitest
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 2.4.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 2.4.0
153
+ - !ruby/object:Gem::Dependency
154
+ name: minitest
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 5.11.0
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 5.11.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: minitest-reporters
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 1.3.6
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 1.3.6
181
+ - !ruby/object:Gem::Dependency
182
+ name: webmock
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 3.5.1
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 3.5.1
195
+ description: Consume Klaviyo's API using ActiveResource
196
+ email:
197
+ - team@rewind.io
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - CHANGELOG.md
203
+ - LICENSE
204
+ - README.md
205
+ - lib/active_resource/connection_ext.rb
206
+ - lib/klaviyo_api.rb
207
+ - lib/klaviyo_api/configuration.rb
208
+ - lib/klaviyo_api/connection.rb
209
+ - lib/klaviyo_api/detailed_log_subscriber.rb
210
+ - lib/klaviyo_api/exceptions.rb
211
+ - lib/klaviyo_api/json_formatter.rb
212
+ - lib/klaviyo_api/resources.rb
213
+ - lib/klaviyo_api/resources/base.rb
214
+ - lib/klaviyo_api/resources/list.rb
215
+ - lib/klaviyo_api/session.rb
216
+ - lib/klaviyo_api/version.rb
217
+ homepage: https://github.com/rewindio/klaviyo_api.git
218
+ licenses:
219
+ - MIT
220
+ metadata: {}
221
+ post_install_message:
222
+ rdoc_options: []
223
+ require_paths:
224
+ - lib
225
+ required_ruby_version: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ required_rubygems_version: !ruby/object:Gem::Requirement
231
+ requirements:
232
+ - - ">="
233
+ - !ruby/object:Gem::Version
234
+ version: '0'
235
+ requirements: []
236
+ rubygems_version: 3.0.3
237
+ signing_key:
238
+ specification_version: 4
239
+ summary: Consume Klaviyo's API using ActiveResource
240
+ test_files: []