tco_client_core 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/tco_client_core.rb +2 -1
- data/lib/tco_client_core/model.rb +19 -0
- data/lib/tco_client_core/{base_request.rb → request.rb} +2 -2
- data/lib/tco_client_core/response.rb +2 -2
- data/lib/tco_client_core/version.rb +1 -1
- data/spec/model_spec.rb +22 -0
- data/spec/{base_request_spec.rb → request_spec.rb} +14 -14
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c13303b1d63c373df6e28f468fb5a844b39bd997
|
4
|
+
data.tar.gz: 9d5d5f9107c31f0794e264c3c4f58fc6b0f8fb42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c76fedbae3fae6f654de6382ca6b91ad80cd3b60766ac181e914a7a22333ad5f1b4e4c3cf234b49d3967c2707b222923b6298f6c95991d84d7e66019ecf7948
|
7
|
+
data.tar.gz: 1384d6962243cdadfc84b777b9caa7e20321d0f7961f9770e13482b5f0d5a4b7a7fa19e54a7ee047df639ef6b958f199b2cd49e8b95d1cc55ce6f846c2859b88
|
data/lib/tco_client_core.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "active_model"
|
2
|
+
|
3
|
+
class TcoClientCore::Model
|
4
|
+
include ActiveModel::Model
|
5
|
+
|
6
|
+
def self.new_with_errors attributes, errors
|
7
|
+
model = new attributes
|
8
|
+
errors.each do |key, messages|
|
9
|
+
messages.each do |message|
|
10
|
+
model.errors.add(key.to_sym, message)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
model
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
!errors.any?
|
18
|
+
end
|
19
|
+
end
|
@@ -3,7 +3,7 @@ require "rest_client"
|
|
3
3
|
require "base64"
|
4
4
|
|
5
5
|
module TcoClientCore
|
6
|
-
class
|
6
|
+
class Request
|
7
7
|
def self.base_uri
|
8
8
|
TcoClientCore.config.base_uri
|
9
9
|
end
|
@@ -14,7 +14,7 @@ module TcoClientCore
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.request(endpoint, method, payload={})
|
17
|
-
raise "Base URI not set" if base_uri.nil?
|
17
|
+
raise "TcoClientCore::Request Base URI not set" if base_uri.nil?
|
18
18
|
params = {
|
19
19
|
method: method,
|
20
20
|
url: "#{base_uri}/#{endpoint}",
|
@@ -9,7 +9,7 @@ module TcoClientCore
|
|
9
9
|
def initialize(raw)
|
10
10
|
@raw = raw
|
11
11
|
@response = JSON.parse(raw)
|
12
|
-
raise ArgumentError, "TcoClientCore::Response
|
12
|
+
raise ArgumentError, "TcoClientCore::Response response hash required" unless @response.is_a? Hash
|
13
13
|
@response.keys.each do |key|
|
14
14
|
next if key == "errors"
|
15
15
|
define_singleton_method(key) { @response[key] }
|
@@ -17,7 +17,7 @@ module TcoClientCore
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def errors?
|
20
|
-
|
20
|
+
errors.any?
|
21
21
|
end
|
22
22
|
|
23
23
|
def errors
|
data/spec/model_spec.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe TcoClientCore::Model do
|
4
|
+
class TestModel < TcoClientCore::Model
|
5
|
+
attr_accessor :property
|
6
|
+
end
|
7
|
+
|
8
|
+
describe ".new_with_errors" do
|
9
|
+
it "adds the errors to the model" do
|
10
|
+
subject = TestModel.new_with_errors({ property: "value" }, { property: ["can't be value"] })
|
11
|
+
expect(subject.errors).not_to be_empty
|
12
|
+
expect(subject).not_to be_valid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#valid?" do
|
17
|
+
it "returns true when no errors are present" do
|
18
|
+
subject = TestModel.new(property: "value")
|
19
|
+
expect(subject).to be_valid
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe TcoClientCore::
|
3
|
+
describe TcoClientCore::Request do
|
4
4
|
before do
|
5
5
|
allow(TcoClientCore.config).to receive(:base_uri) { "base_uri" }
|
6
6
|
allow(TcoClientCore.config).to receive(:vendor_id) { "vendor_id" }
|
@@ -9,7 +9,7 @@ describe TcoClientCore::BaseRequest do
|
|
9
9
|
|
10
10
|
describe '.authorization' do
|
11
11
|
it 'Base64 encodes the vendor id and api key and prepends "Basic "' do
|
12
|
-
expect(TcoClientCore::
|
12
|
+
expect(TcoClientCore::Request.authorization).to eq("Basic #{Base64::encode64("#{TcoClientCore.config.vendor_id}:#{TcoClientCore.config.api_key}")}")
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -18,7 +18,7 @@ describe TcoClientCore::BaseRequest do
|
|
18
18
|
allow(TcoClientCore.config).to receive(:base_uri) { nil }
|
19
19
|
expect {
|
20
20
|
subject.request("test", :get)
|
21
|
-
}.to raise_error("Base URI not set")
|
21
|
+
}.to raise_error("TcoClientCore::Request Base URI not set")
|
22
22
|
end
|
23
23
|
|
24
24
|
it "makes a rest request with the given endpoint, including the authorization" do
|
@@ -30,7 +30,7 @@ describe TcoClientCore::BaseRequest do
|
|
30
30
|
headers: {
|
31
31
|
accept: :json,
|
32
32
|
content_type: 'application/json',
|
33
|
-
authorization: TcoClientCore::
|
33
|
+
authorization: TcoClientCore::Request.authorization
|
34
34
|
}
|
35
35
|
}).and_return double(execute: "{}")
|
36
36
|
subject.request("test", :get)
|
@@ -45,7 +45,7 @@ describe TcoClientCore::BaseRequest do
|
|
45
45
|
headers: {
|
46
46
|
accept: :json,
|
47
47
|
content_type: "application/json",
|
48
|
-
authorization: TcoClientCore::
|
48
|
+
authorization: TcoClientCore::Request.authorization
|
49
49
|
},
|
50
50
|
payload: payload_data.to_json
|
51
51
|
}).and_return double(execute: "{}")
|
@@ -54,36 +54,36 @@ describe TcoClientCore::BaseRequest do
|
|
54
54
|
|
55
55
|
it "returns a response" do
|
56
56
|
allow_any_instance_of(RestClient::Request).to receive(:execute).and_return({ "test" => "result" }.to_json)
|
57
|
-
response = TcoClientCore::
|
57
|
+
response = TcoClientCore::Request.request("test", :get)
|
58
58
|
expect(response).to be_kind_of(TcoClientCore::Response)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
describe ".get" do
|
63
63
|
it "makes a get request" do
|
64
|
-
expect(TcoClientCore::
|
65
|
-
TcoClientCore::
|
64
|
+
expect(TcoClientCore::Request).to receive(:request).with("test", :get, {})
|
65
|
+
TcoClientCore::Request.get("test")
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
69
|
describe ".post" do
|
70
70
|
it "makes a post request" do
|
71
|
-
expect(TcoClientCore::
|
72
|
-
TcoClientCore::
|
71
|
+
expect(TcoClientCore::Request).to receive(:request).with("test", :post, {})
|
72
|
+
TcoClientCore::Request.post("test")
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
76
|
describe ".patch" do
|
77
77
|
it "makes a patch request" do
|
78
|
-
expect(TcoClientCore::
|
79
|
-
TcoClientCore::
|
78
|
+
expect(TcoClientCore::Request).to receive(:request).with("test", :patch, {})
|
79
|
+
TcoClientCore::Request.patch("test")
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
83
|
describe ".delete" do
|
84
84
|
it "makes a delete request" do
|
85
|
-
expect(TcoClientCore::
|
86
|
-
TcoClientCore::
|
85
|
+
expect(TcoClientCore::Request).to receive(:request).with("test", :delete, {})
|
86
|
+
TcoClientCore::Request.delete("test")
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tco_client_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 2CO Inc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -228,10 +228,12 @@ extensions: []
|
|
228
228
|
extra_rdoc_files: []
|
229
229
|
files:
|
230
230
|
- lib/tco_client_core.rb
|
231
|
-
- lib/tco_client_core/
|
231
|
+
- lib/tco_client_core/model.rb
|
232
|
+
- lib/tco_client_core/request.rb
|
232
233
|
- lib/tco_client_core/response.rb
|
233
234
|
- lib/tco_client_core/version.rb
|
234
|
-
- spec/
|
235
|
+
- spec/model_spec.rb
|
236
|
+
- spec/request_spec.rb
|
235
237
|
- spec/response_spec.rb
|
236
238
|
- spec/spec_helper.rb
|
237
239
|
- spec/tco_base_api_spec.rb
|
@@ -260,7 +262,8 @@ signing_key:
|
|
260
262
|
specification_version: 4
|
261
263
|
summary: Gem to handle communication with the 2CO API
|
262
264
|
test_files:
|
263
|
-
- spec/
|
265
|
+
- spec/model_spec.rb
|
266
|
+
- spec/request_spec.rb
|
264
267
|
- spec/response_spec.rb
|
265
268
|
- spec/spec_helper.rb
|
266
269
|
- spec/tco_base_api_spec.rb
|