tco_client_core 0.1.3 → 0.2.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/base_request.rb +2 -2
- data/lib/tco_client_core/response.rb +11 -39
- data/lib/tco_client_core/version.rb +1 -1
- data/spec/response_spec.rb +21 -101
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 988aa36ca352a04d8301597ea64143f36ce78971
|
|
4
|
+
data.tar.gz: 7dd34b6afb2fd308003a9a0322ac4a2e44219f17
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a03da0c76691340140a2dd11d774c83a91f89c1f415f88369b2b6faf453324f46e978ff55ec4776d04aab7d2321fa29ca1030db06b2429fdc8b3bcad834e78f6
|
|
7
|
+
data.tar.gz: 2605a74f0b59bb4e3de9236f24c697aac30098fdb954a64a5c29c8bc72c505f7b95e0da399848e2f0dbee830eeb578df5cb85b23b2bfd872a0c8018c485028d9
|
|
@@ -21,12 +21,12 @@ module TcoClientCore
|
|
|
21
21
|
payload: payload.to_json,
|
|
22
22
|
headers: {
|
|
23
23
|
accept: :json,
|
|
24
|
-
content_type:
|
|
24
|
+
content_type: "application/json",
|
|
25
25
|
authorization: authorization
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
TcoClientCore::Response.new(RestClient::Request.new(params).execute)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
def self.get(endpoint, options={})
|
|
@@ -1,55 +1,27 @@
|
|
|
1
|
-
require "ostruct"
|
|
2
1
|
require "json"
|
|
3
2
|
|
|
4
3
|
module TcoClientCore
|
|
5
4
|
class Response
|
|
6
|
-
|
|
7
|
-
if object.is_a? Array
|
|
8
|
-
object.map { |v| to_struct(v) }
|
|
9
|
-
elsif object.is_a? Hash
|
|
10
|
-
object.each_with_object(OpenStruct.new) do |(key, val), st|
|
|
11
|
-
st[key] = to_struct(val)
|
|
12
|
-
end
|
|
13
|
-
else
|
|
14
|
-
object
|
|
15
|
-
end
|
|
16
|
-
end
|
|
5
|
+
extend Forwardable
|
|
17
6
|
|
|
18
|
-
|
|
7
|
+
def_delegators :@response, :to_h, :to_hash
|
|
19
8
|
|
|
20
|
-
def initialize(
|
|
21
|
-
@raw =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
9
|
+
def initialize(raw)
|
|
10
|
+
@raw = raw
|
|
11
|
+
@response = JSON.parse(raw)
|
|
12
|
+
raise ArgumentError, "TcoClientCore::Response: response hash required" unless @response.is_a? Hash
|
|
13
|
+
@response.keys.each do |key|
|
|
14
|
+
next if key == "errors"
|
|
15
|
+
define_singleton_method(key) { @response[key] }
|
|
26
16
|
end
|
|
27
|
-
@to_struct = self.class.to_struct(as_json)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def to_json
|
|
31
|
-
to_s
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def as_json
|
|
35
|
-
JSON.parse(raw)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def to_s
|
|
39
|
-
raw.to_s
|
|
40
17
|
end
|
|
41
18
|
|
|
42
19
|
def errors?
|
|
43
|
-
|
|
44
|
-
!!(json.is_a?(Hash) && (json.fetch("errors", nil) || json.fetch(:errors, nil)))
|
|
20
|
+
@response.key?("errors")
|
|
45
21
|
end
|
|
46
22
|
|
|
47
23
|
def errors
|
|
48
|
-
|
|
49
|
-
as_json["errors"] || as_json[:errors]
|
|
50
|
-
else
|
|
51
|
-
{}
|
|
52
|
-
end
|
|
24
|
+
@response.fetch("errors", {})
|
|
53
25
|
end
|
|
54
26
|
end
|
|
55
27
|
end
|
data/spec/response_spec.rb
CHANGED
|
@@ -1,124 +1,44 @@
|
|
|
1
1
|
require "spec_helper"
|
|
2
2
|
|
|
3
3
|
describe TcoClientCore::Response do
|
|
4
|
-
let(:
|
|
5
|
-
subject { TcoClientCore::Response.new(
|
|
4
|
+
let(:hash) { { "a" => "A", "b" => "B" } }
|
|
5
|
+
subject { TcoClientCore::Response.new(hash.to_json) }
|
|
6
6
|
|
|
7
|
-
it "
|
|
8
|
-
expect
|
|
7
|
+
it "raises an error when given a json array" do
|
|
8
|
+
expect {
|
|
9
|
+
TcoClientCore::Response.new("[]")
|
|
10
|
+
}.to raise_error ArgumentError
|
|
9
11
|
end
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
describe "#to_json" do
|
|
18
|
-
it "returns the raw json" do
|
|
19
|
-
expect(subject.to_json).to eq(raw_response)
|
|
20
|
-
end
|
|
13
|
+
it "defines methods for each key in the response hash" do
|
|
14
|
+
expect(subject.a).to eq("A")
|
|
15
|
+
expect(subject.b).to eq("B")
|
|
21
16
|
end
|
|
22
17
|
|
|
23
|
-
describe "#
|
|
24
|
-
it "returns the response
|
|
25
|
-
expect(subject.
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
describe "#to_struct" do
|
|
30
|
-
context "with a hash" do
|
|
31
|
-
it "creates an open struct" do
|
|
32
|
-
ostruct = subject.to_struct
|
|
33
|
-
expect(ostruct).to be_kind_of(OpenStruct)
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
it "creates a open struct for nested hashes" do
|
|
37
|
-
subject = TcoClientCore::Response.new({
|
|
38
|
-
one: 1,
|
|
39
|
-
two: { three: { four: 4 } }
|
|
40
|
-
}.to_json)
|
|
41
|
-
|
|
42
|
-
ostruct = subject.to_struct
|
|
43
|
-
expect(ostruct[:two]).to be_kind_of(OpenStruct)
|
|
44
|
-
expect(ostruct.two.three.four).to eq(4)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
it "creates a open struct for array values with hashes" do
|
|
48
|
-
subject = TcoClientCore::Response.new({
|
|
49
|
-
one: 1,
|
|
50
|
-
two: { three: [{ four: 4 }] }
|
|
51
|
-
}.to_json)
|
|
52
|
-
ostruct = subject.to_struct
|
|
53
|
-
expect(ostruct.two.three.first).to be_kind_of(OpenStruct)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
context "with nil values" do
|
|
57
|
-
subject { TcoClientCore::Response.new({ test: nil }.to_json) }
|
|
58
|
-
|
|
59
|
-
it "does not create an infinite loop" do
|
|
60
|
-
expect { subject.to_struct }.not_to raise_error
|
|
61
|
-
end
|
|
62
|
-
end
|
|
18
|
+
describe "#to_h" do
|
|
19
|
+
it "returns the response json hash" do
|
|
20
|
+
expect(subject.to_h).to include(hash)
|
|
63
21
|
end
|
|
64
22
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
subject = TcoClientCore::Response.new([{ one: 1 }, { two: 2 }].to_json)
|
|
68
|
-
|
|
69
|
-
array = subject.to_struct;
|
|
70
|
-
expect(array.first).to be_kind_of(OpenStruct)
|
|
71
|
-
expect(array.last).to be_kind_of(OpenStruct)
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
it "creates a open struct for nested arrays with hashes" do
|
|
75
|
-
subject = TcoClientCore::Response.new([
|
|
76
|
-
{ one: 1 },
|
|
77
|
-
[{ two: 2 }]
|
|
78
|
-
].to_json)
|
|
79
|
-
|
|
80
|
-
array = subject.to_struct
|
|
81
|
-
expect(array.last.first).to be_kind_of(OpenStruct)
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
context "with nil values" do
|
|
85
|
-
subject { TcoClientCore::Response.new([nil].to_json) }
|
|
86
|
-
|
|
87
|
-
it "does not create an infinite loop" do
|
|
88
|
-
expect { subject.to_struct }.not_to raise_error
|
|
89
|
-
end
|
|
90
|
-
end
|
|
23
|
+
it "is aliased as to_hash" do
|
|
24
|
+
expect(subject.to_hash).to include(hash)
|
|
91
25
|
end
|
|
92
26
|
end
|
|
93
27
|
|
|
94
28
|
describe "#errors?" do
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
expect(subject.errors?).to eq(false)
|
|
98
|
-
end
|
|
29
|
+
it "returns false when an errors key is missing" do
|
|
30
|
+
expect(subject.errors?).to eq(false)
|
|
99
31
|
end
|
|
100
32
|
|
|
101
|
-
|
|
102
|
-
subject
|
|
103
|
-
|
|
104
|
-
it "return true" do
|
|
105
|
-
expect(subject.errors?).to eq(true)
|
|
106
|
-
end
|
|
33
|
+
it "returns true when an errors key is present" do
|
|
34
|
+
subject = TcoClientCore::Response.new({ errors: { foo: ["message"] } }.to_json)
|
|
35
|
+
expect(subject.errors?).to eq(true)
|
|
107
36
|
end
|
|
108
37
|
end
|
|
109
38
|
|
|
110
39
|
describe "#errors" do
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
it "returns the errors key" do
|
|
114
|
-
expect(subject.errors).to have_key("field")
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
describe "with exceptions" do
|
|
119
|
-
it "raises a TcoClientCore::RecordNotFound exception if the record was not found" do
|
|
120
|
-
allow_any_instance_of(RestClient::Request).to receive(:execute).and_return({ "errors" => { "ActiveRecord::RecordNotFound" => ["not found error"] } }.to_json)
|
|
121
|
-
expect { TcoClientCore::BaseRequest.request("test", :get) }.to raise_error(TcoClientCore::RecordNotFound)
|
|
40
|
+
it "returns an empty hash when there are no errors" do
|
|
41
|
+
expect(subject.errors).to eq({})
|
|
122
42
|
end
|
|
123
43
|
end
|
|
124
44
|
end
|
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.2.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-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activemodel
|
|
@@ -255,7 +255,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
255
255
|
version: '0'
|
|
256
256
|
requirements: []
|
|
257
257
|
rubyforge_project:
|
|
258
|
-
rubygems_version: 2.
|
|
258
|
+
rubygems_version: 2.2.2
|
|
259
259
|
signing_key:
|
|
260
260
|
specification_version: 4
|
|
261
261
|
summary: Gem to handle communication with the 2CO API
|
|
@@ -264,3 +264,4 @@ test_files:
|
|
|
264
264
|
- spec/response_spec.rb
|
|
265
265
|
- spec/spec_helper.rb
|
|
266
266
|
- spec/tco_base_api_spec.rb
|
|
267
|
+
has_rdoc:
|