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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 988aa36ca352a04d8301597ea64143f36ce78971
4
- data.tar.gz: 7dd34b6afb2fd308003a9a0322ac4a2e44219f17
3
+ metadata.gz: c13303b1d63c373df6e28f468fb5a844b39bd997
4
+ data.tar.gz: 9d5d5f9107c31f0794e264c3c4f58fc6b0f8fb42
5
5
  SHA512:
6
- metadata.gz: a03da0c76691340140a2dd11d774c83a91f89c1f415f88369b2b6faf453324f46e978ff55ec4776d04aab7d2321fa29ca1030db06b2429fdc8b3bcad834e78f6
7
- data.tar.gz: 2605a74f0b59bb4e3de9236f24c697aac30098fdb954a64a5c29c8bc72c505f7b95e0da399848e2f0dbee830eeb578df5cb85b23b2bfd872a0c8018c485028d9
6
+ metadata.gz: 3c76fedbae3fae6f654de6382ca6b91ad80cd3b60766ac181e914a7a22333ad5f1b4e4c3cf234b49d3967c2707b222923b6298f6c95991d84d7e66019ecf7948
7
+ data.tar.gz: 1384d6962243cdadfc84b777b9caa7e20321d0f7961f9770e13482b5f0d5a4b7a7fa19e54a7ee047df639ef6b958f199b2cd49e8b95d1cc55ce6f846c2859b88
@@ -1,7 +1,8 @@
1
1
  require "ostruct"
2
2
  require "tco_client_core/version"
3
- require "tco_client_core/base_request"
3
+ require "tco_client_core/request"
4
4
  require "tco_client_core/response"
5
+ require "tco_client_core/model"
5
6
 
6
7
  module TcoClientCore
7
8
  class Error < StandardError; end
@@ -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 BaseRequest
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: response hash required" unless @response.is_a? Hash
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
- @response.key?("errors")
20
+ errors.any?
21
21
  end
22
22
 
23
23
  def errors
@@ -1,3 +1,3 @@
1
1
  module TcoClientCore
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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::BaseRequest do
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::BaseRequest.authorization).to eq("Basic #{Base64::encode64("#{TcoClientCore.config.vendor_id}:#{TcoClientCore.config.api_key}")}")
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::BaseRequest.authorization
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::BaseRequest.authorization
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::BaseRequest.request("test", :get)
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::BaseRequest).to receive(:request).with("test", :get, {})
65
- TcoClientCore::BaseRequest.get("test")
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::BaseRequest).to receive(:request).with("test", :post, {})
72
- TcoClientCore::BaseRequest.post("test")
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::BaseRequest).to receive(:request).with("test", :patch, {})
79
- TcoClientCore::BaseRequest.patch("test")
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::BaseRequest).to receive(:request).with("test", :delete, {})
86
- TcoClientCore::BaseRequest.delete("test")
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.2.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-20 00:00:00.000000000 Z
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/base_request.rb
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/base_request_spec.rb
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/base_request_spec.rb
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