tco_client_core 0.1.0

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
+ SHA1:
3
+ metadata.gz: d7c5bf9d3fa6eee0deb165199bb1e61debdbf199
4
+ data.tar.gz: 2663ecd6223c1db03fdc3cd01ce2462f4de2c6fe
5
+ SHA512:
6
+ metadata.gz: e6bd1a0af6e74885e5e1843c1a55dc6238d7e54d515452e519e7d059c932384fc32e0820dc3005443f1c4330b86ea0a387d6c6d39cc4f333e2d43898079678ac
7
+ data.tar.gz: ce36e38bbb5bb1059131aa72faacf6fa6d8fab5e348cbb9195546a0218f17a1cbb539f1a2c32059e3d38edb7ddf0619c7715eb0004c14020ba0ba7b7848e49a3
@@ -0,0 +1,67 @@
1
+ require File.join(File.expand_path('..', File.dirname(__FILE__)), 'tco_client_core', 'response')
2
+ require "rest_client"
3
+
4
+ module TcoClientCore
5
+ class BaseRequest
6
+ def self.base_uri
7
+ TcoClientCore.config.base_uri
8
+ end
9
+
10
+ def self.authorization
11
+ encoded = Base64::encode64("#{TcoClientCore.config.vendor_id}:#{TcoClientCore.config.api_key}")
12
+ "Basic #{encoded}"
13
+ end
14
+
15
+ def self.request(endpoint, method, payload={})
16
+ raise "Base URI not set" if base_uri.nil?
17
+ params = {
18
+ method: method,
19
+ url: "#{base_uri}/#{endpoint}",
20
+ payload: payload.to_json,
21
+ headers: {
22
+ accept: :json,
23
+ content_type: 'application/json',
24
+ authorization: authorization
25
+ }
26
+ }
27
+
28
+ response = TcoClientCore::Response.new(RestClient::Request.new(params).execute)
29
+ end
30
+
31
+ def self.get(endpoint, options={})
32
+ request(endpoint, :get, options)
33
+ end
34
+
35
+ def self.post(endpoint, options={})
36
+ request(endpoint, :post, options)
37
+ end
38
+
39
+ def self.patch(endpoint, options={})
40
+ request(endpoint, :patch, options)
41
+ end
42
+
43
+ def self.delete(endpoint, options={})
44
+ request(endpoint, :delete, options)
45
+ end
46
+
47
+ def request(endpoint, method, options={})
48
+ self.class.request(endpoint, method, options)
49
+ end
50
+
51
+ def get(endpoint, options={})
52
+ request(endpoint, :get, options)
53
+ end
54
+
55
+ def post(endpoint, options={})
56
+ request(endpoint, :post, options)
57
+ end
58
+
59
+ def patch(endpoint, options={})
60
+ request(endpoint, :patch, options)
61
+ end
62
+
63
+ def delete(endpoint, options={})
64
+ request(endpoint, :delete, options)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,51 @@
1
+ require "ostruct"
2
+ require "json"
3
+
4
+ module TcoClientCore
5
+ class Response
6
+ attr_reader :raw
7
+
8
+ def initialize(response)
9
+ @raw = response
10
+ errors.each do |key, value|
11
+ if key == "ActiveRecord::RecordNotFound"
12
+ raise TcoClientCore::RecordNotFound, value.first
13
+ end
14
+ end
15
+ end
16
+
17
+ def as_json
18
+ JSON.parse(raw)
19
+ end
20
+
21
+ def to_s
22
+ raw
23
+ end
24
+
25
+ def to_struct(object=nil)
26
+ object ||= as_json
27
+ if object.is_a? Array
28
+ object.map { |v| to_struct(v) }
29
+ elsif object.is_a? Hash
30
+ object.each_with_object(OpenStruct.new) do |(key, val), st|
31
+ st[key] = to_struct(val)
32
+ end
33
+ else
34
+ object
35
+ end
36
+ end
37
+
38
+ def errors?
39
+ json = as_json
40
+ !!(json.is_a?(Hash) && (json.fetch("errors", nil) || json.fetch(:errors, nil)))
41
+ end
42
+
43
+ def errors
44
+ if errors?
45
+ as_json["errors"] || as_json[:errors]
46
+ else
47
+ {}
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module TcoClientCore
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+ require "tco_client_core/version"
3
+ require "tco_client_core/base_request"
4
+ require "tco_client_core/response"
5
+
6
+ module TcoClientCore
7
+ class Error < StandardError; end
8
+ class RecordNotFound < TcoClientCore::Error; end
9
+
10
+ def configure
11
+ yield config
12
+ end
13
+
14
+ def config
15
+ @@config ||= OpenStruct.new
16
+ end
17
+
18
+ module_function :configure, :config
19
+ end
@@ -0,0 +1,117 @@
1
+ require "spec_helper"
2
+
3
+ describe TcoClientCore::BaseRequest do
4
+ before do
5
+ allow(TcoClientCore.config).to receive(:base_uri) { "base_uri" }
6
+ allow(TcoClientCore.config).to receive(:vendor_id) { "vendor_id" }
7
+ allow(TcoClientCore.config).to receive(:api_key) { "api_key" }
8
+ end
9
+
10
+ describe '.authorization' do
11
+ it 'Base64 encodes the vendor id and api key and prepends "Basic "' do
12
+ expect(TcoClientCore::BaseRequest.authorization).to eq("Basic #{Base64::encode64("#{TcoClientCore.config.vendor_id}:#{TcoClientCore.config.api_key}")}")
13
+ end
14
+ end
15
+
16
+ describe ".request" do
17
+ it "raises an error if no base_uri is set" do
18
+ allow(TcoClientCore.config).to receive(:base_uri) { nil }
19
+ expect {
20
+ subject.request("test", :get)
21
+ }.to raise_error("Base URI not set")
22
+ end
23
+
24
+ it "makes a rest request with the given endpoint, including the authorization" do
25
+ allow(TcoClientCore.config).to receive(:base_uri) { "base_uri" }
26
+ expect(RestClient::Request).to receive(:new).with({
27
+ method: :get,
28
+ url: "base_uri/test",
29
+ payload: "{}",
30
+ headers: {
31
+ accept: :json,
32
+ content_type: 'application/json',
33
+ authorization: TcoClientCore::BaseRequest.authorization
34
+ }
35
+ }).and_return double(execute: "{}")
36
+ subject.request("test", :get)
37
+ end
38
+
39
+ it "stringifies the json payload" do
40
+ allow(TcoClientCore.config).to receive(:base_uri) { "base_uri" }
41
+ payload_data = { foo: "bar" }
42
+ expect(RestClient::Request).to receive(:new).with({
43
+ method: :post,
44
+ url: "base_uri/test",
45
+ headers: {
46
+ accept: :json,
47
+ content_type: "application/json",
48
+ authorization: TcoClientCore::BaseRequest.authorization
49
+ },
50
+ payload: payload_data.to_json
51
+ }).and_return double(execute: "{}")
52
+ subject.request("test", :post, payload_data)
53
+ end
54
+
55
+ it "returns a response" do
56
+ allow_any_instance_of(RestClient::Request).to receive(:execute).and_return({ "test" => "result" }.to_json)
57
+ response = TcoClientCore::BaseRequest.request("test", :get)
58
+ expect(response).to be_kind_of(TcoClientCore::Response)
59
+ end
60
+ end
61
+
62
+ describe ".get" do
63
+ it "makes a get request" do
64
+ expect(TcoClientCore::BaseRequest).to receive(:request).with("test", :get, {})
65
+ TcoClientCore::BaseRequest.get("test")
66
+ end
67
+ end
68
+
69
+ describe ".post" do
70
+ it "makes a post request" do
71
+ expect(TcoClientCore::BaseRequest).to receive(:request).with("test", :post, {})
72
+ TcoClientCore::BaseRequest.post("test")
73
+ end
74
+ end
75
+
76
+ describe ".patch" do
77
+ it "makes a patch request" do
78
+ expect(TcoClientCore::BaseRequest).to receive(:request).with("test", :patch, {})
79
+ TcoClientCore::BaseRequest.patch("test")
80
+ end
81
+ end
82
+
83
+ describe ".delete" do
84
+ it "makes a delete request" do
85
+ expect(TcoClientCore::BaseRequest).to receive(:request).with("test", :delete, {})
86
+ TcoClientCore::BaseRequest.delete("test")
87
+ end
88
+ end
89
+
90
+ describe "#get" do
91
+ it "makes a get request" do
92
+ expect(subject).to receive(:request).with("test", :get, {})
93
+ subject.get("test")
94
+ end
95
+ end
96
+
97
+ describe "#post" do
98
+ it "makes a post request" do
99
+ expect(subject).to receive(:request).with("test", :post, {})
100
+ subject.post("test")
101
+ end
102
+ end
103
+
104
+ describe "#patch" do
105
+ it "makes a patch request" do
106
+ expect(subject).to receive(:request).with("test", :patch, {})
107
+ subject.patch("test")
108
+ end
109
+ end
110
+
111
+ describe "#delete" do
112
+ it "makes a delete request" do
113
+ expect(subject).to receive(:request).with("test", :delete, {})
114
+ subject.delete("test")
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,95 @@
1
+ require "spec_helper"
2
+
3
+ describe TcoClientCore::Response do
4
+ let(:raw_response) { { test: "response" }.to_json }
5
+ subject { TcoClientCore::Response.new(raw_response) }
6
+
7
+ it "saves a reference to the raw response" do
8
+ expect(subject.raw).to eq(raw_response)
9
+ end
10
+
11
+ describe "#to_s" do
12
+ it "returns the raw response string" do
13
+ expect(subject.to_s).to eq(raw_response)
14
+ end
15
+ end
16
+
17
+ describe "#to_json" do
18
+ it "returns the response as json" do
19
+ expect(subject.as_json).to have_key("test")
20
+ end
21
+ end
22
+
23
+ describe "#to_struct" do
24
+ context "with a hash" do
25
+ it "creates an open struct" do
26
+ ostruct = subject.to_struct
27
+ expect(ostruct).to be_kind_of(OpenStruct)
28
+ end
29
+
30
+ it "creates a open struct for nested hashes" do
31
+ ostruct = subject.to_struct({
32
+ one: 1,
33
+ two: { three: { four: 4 } }
34
+ })
35
+ expect(ostruct[:two]).to be_kind_of(OpenStruct)
36
+ expect(ostruct.two.three.four).to eq(4)
37
+ end
38
+
39
+ it "creates a open struct for array values with hashes" do
40
+ ostruct = subject.to_struct({
41
+ one: 1,
42
+ two: { three: [{ four: 4 }] }
43
+ })
44
+ expect(ostruct.two.three.first).to be_kind_of(OpenStruct)
45
+ end
46
+ end
47
+
48
+ context "with an array" do
49
+ it "creates a open struct for every hash property" do
50
+ array = subject.to_struct([{ one: 1 }, { two: 2 }]);
51
+ expect(array.first).to be_kind_of(OpenStruct)
52
+ expect(array.last).to be_kind_of(OpenStruct)
53
+ end
54
+
55
+ it "creates a open struct for nested arrays with hashes" do
56
+ array = subject.to_struct([
57
+ { one: 1 },
58
+ [{ two: 2 }]
59
+ ])
60
+ expect(array.last.first).to be_kind_of(OpenStruct)
61
+ end
62
+ end
63
+ end
64
+
65
+ describe "#errors?" do
66
+ context "without errors" do
67
+ it "returns false" do
68
+ expect(subject.errors?).to eq(false)
69
+ end
70
+ end
71
+
72
+ context "with errors" do
73
+ subject { TcoClientCore::Response.new({ errors: { field: ["error message"] } }.to_json) }
74
+
75
+ it "return true" do
76
+ expect(subject.errors?).to eq(true)
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#errors" do
82
+ subject { TcoClientCore::Response.new({ errors: { field: ["error message"] } }.to_json) }
83
+
84
+ it "returns the errors key" do
85
+ expect(subject.errors).to have_key("field")
86
+ end
87
+ end
88
+
89
+ describe "with exceptions" do
90
+ it "raises a TcoClientCore::RecordNotFound exception if the record was not found" do
91
+ allow_any_instance_of(RestClient::Request).to receive(:execute).and_return({ "errors" => { "ActiveRecord::RecordNotFound" => ["not found error"] } }.to_json)
92
+ expect { TcoClientCore::BaseRequest.request("test", :get) }.to raise_error(TcoClientCore::RecordNotFound)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,22 @@
1
+ require "rubygems"
2
+ require "awesome_print"
3
+ require "bundler/setup"
4
+ require "tco_client_core"
5
+ require "vcr"
6
+ require "json"
7
+
8
+ RSpec.configure do |config|
9
+ config.order = :rand
10
+ config.color = true
11
+ end
12
+
13
+ TcoClientCore.configure do |config|
14
+ config.base_uri = "localhost:4000/internal"
15
+ end
16
+
17
+ VCR.configure do |config|
18
+ config.configure_rspec_metadata!
19
+ config.allow_http_connections_when_no_cassette = true
20
+ config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
21
+ config.hook_into :webmock
22
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ describe TcoClientCore do
4
+ describe "#config" do
5
+ it "is a symbol table" do
6
+ expect(TcoClientCore.config).to be_an(OpenStruct)
7
+ end
8
+ end
9
+
10
+ describe "#configure" do
11
+ it "yields the config to the block" do
12
+ TcoClientCore.configure do |config|
13
+ config.test = "test"
14
+ end
15
+ expect(TcoClientCore.config.test).to eq("test")
16
+ end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,294 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tco_client_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 2CO Inc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: dotenv
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: fuubar
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.10'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.10'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-awesome_print
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '9.6'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '9.6'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry-coolline
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.2'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.2'
181
+ - !ruby/object:Gem::Dependency
182
+ name: pry-theme
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '1.1'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '1.1'
195
+ - !ruby/object:Gem::Dependency
196
+ name: rake
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '10.0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '10.0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: rspec
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '3.2'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '3.2'
223
+ - !ruby/object:Gem::Dependency
224
+ name: vcr
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '2.9'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '2.9'
237
+ - !ruby/object:Gem::Dependency
238
+ name: webmock
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - "~>"
242
+ - !ruby/object:Gem::Version
243
+ version: '1.20'
244
+ type: :development
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - "~>"
249
+ - !ruby/object:Gem::Version
250
+ version: '1.20'
251
+ description: Handles communication with 2checkout.com's API
252
+ email:
253
+ - support@2co.com
254
+ executables: []
255
+ extensions: []
256
+ extra_rdoc_files: []
257
+ files:
258
+ - lib/tco_client_core.rb
259
+ - lib/tco_client_core/base_request.rb
260
+ - lib/tco_client_core/response.rb
261
+ - lib/tco_client_core/version.rb
262
+ - spec/base_request_spec.rb
263
+ - spec/response_spec.rb
264
+ - spec/spec_helper.rb
265
+ - spec/tco_base_api_spec.rb
266
+ homepage: http://2checkout.com
267
+ licenses:
268
+ - All Rights Reserved
269
+ metadata: {}
270
+ post_install_message:
271
+ rdoc_options: []
272
+ require_paths:
273
+ - lib
274
+ required_ruby_version: !ruby/object:Gem::Requirement
275
+ requirements:
276
+ - - ">="
277
+ - !ruby/object:Gem::Version
278
+ version: '0'
279
+ required_rubygems_version: !ruby/object:Gem::Requirement
280
+ requirements:
281
+ - - ">="
282
+ - !ruby/object:Gem::Version
283
+ version: '0'
284
+ requirements: []
285
+ rubyforge_project:
286
+ rubygems_version: 2.4.3
287
+ signing_key:
288
+ specification_version: 4
289
+ summary: Gem to handle communication with the 2CO API
290
+ test_files:
291
+ - spec/base_request_spec.rb
292
+ - spec/response_spec.rb
293
+ - spec/spec_helper.rb
294
+ - spec/tco_base_api_spec.rb