tco_client_core 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1fba66273191b0a4c9ce439c6ce0a12b64a05152
4
- data.tar.gz: ad1536d4cd5639e70d4f138a289f4fc59250c9d0
3
+ metadata.gz: bb7e85db0f524ca8d6aea9f9501d3b6a3c8d3d5e
4
+ data.tar.gz: bb730570d3131439cc7ca55ad6fa25e5121affc3
5
5
  SHA512:
6
- metadata.gz: 125cd1fa26f50f6e6e8c1c162f6357177015a4fe3c340d6e95e1dc1295ef27f9dea83c280d8e52fb865214243427bdf2ec8479bd6f53edbcb54f1ced2b525b2a
7
- data.tar.gz: ad32aa74c0b5005f039ea864ae8db43316e43a4e58c86bab7ba05baf509e63e121eb36924e96e74548b8704a44fa7fe057dc128b3331b542075e1cb58cfbb207
6
+ metadata.gz: d32fa588db99fb5745e8116e2aa5509708636a93f52341bea4757e53f4cf849dbf2459cea66913a195119734066791d87bf1749eca1324e8fb8771bd7cb7031a
7
+ data.tar.gz: 641a5f4879038e5f005cd51ca9016ad39495c65c97e057f2934217bb10a79ceeb17654d5ba3018542d8493b335e24fa1c309b1ec3a3189c4eab7a3e405694484
@@ -3,7 +3,19 @@ require "json"
3
3
 
4
4
  module TcoClientCore
5
5
  class Response
6
- attr_reader :raw
6
+ def self.to_struct(object)
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
17
+
18
+ attr_reader :raw, :to_struct
7
19
 
8
20
  def initialize(response)
9
21
  @raw = response
@@ -12,6 +24,7 @@ module TcoClientCore
12
24
  raise TcoClientCore::RecordNotFound, value.first
13
25
  end
14
26
  end
27
+ @to_struct = self.class.to_struct(as_json)
15
28
  end
16
29
 
17
30
  def to_json
@@ -26,19 +39,6 @@ module TcoClientCore
26
39
  raw.to_s
27
40
  end
28
41
 
29
- def to_struct(object=nil)
30
- object ||= as_json
31
- if object.is_a? Array
32
- object.map { |v| v.nil? ? v : to_struct(v) }
33
- elsif object.is_a? Hash
34
- object.each_with_object(OpenStruct.new) do |(key, val), st|
35
- st[key] = val.nil? ? val : to_struct(val)
36
- end
37
- else
38
- object
39
- end
40
- end
41
-
42
42
  def errors?
43
43
  json = as_json
44
44
  !!(json.is_a?(Hash) && (json.fetch("errors", nil) || json.fetch(:errors, nil)))
@@ -1,3 +1,3 @@
1
1
  module TcoClientCore
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -34,19 +34,22 @@ describe TcoClientCore::Response do
34
34
  end
35
35
 
36
36
  it "creates a open struct for nested hashes" do
37
- ostruct = subject.to_struct({
37
+ subject = TcoClientCore::Response.new({
38
38
  one: 1,
39
39
  two: { three: { four: 4 } }
40
- })
40
+ }.to_json)
41
+
42
+ ostruct = subject.to_struct
41
43
  expect(ostruct[:two]).to be_kind_of(OpenStruct)
42
44
  expect(ostruct.two.three.four).to eq(4)
43
45
  end
44
46
 
45
47
  it "creates a open struct for array values with hashes" do
46
- ostruct = subject.to_struct({
48
+ subject = TcoClientCore::Response.new({
47
49
  one: 1,
48
50
  two: { three: [{ four: 4 }] }
49
- })
51
+ }.to_json)
52
+ ostruct = subject.to_struct
50
53
  expect(ostruct.two.three.first).to be_kind_of(OpenStruct)
51
54
  end
52
55
 
@@ -61,16 +64,20 @@ describe TcoClientCore::Response do
61
64
 
62
65
  context "with an array" do
63
66
  it "creates a open struct for every hash property" do
64
- array = subject.to_struct([{ one: 1 }, { two: 2 }]);
67
+ subject = TcoClientCore::Response.new([{ one: 1 }, { two: 2 }].to_json)
68
+
69
+ array = subject.to_struct;
65
70
  expect(array.first).to be_kind_of(OpenStruct)
66
71
  expect(array.last).to be_kind_of(OpenStruct)
67
72
  end
68
73
 
69
74
  it "creates a open struct for nested arrays with hashes" do
70
- array = subject.to_struct([
75
+ subject = TcoClientCore::Response.new([
71
76
  { one: 1 },
72
77
  [{ two: 2 }]
73
- ])
78
+ ].to_json)
79
+
80
+ array = subject.to_struct
74
81
  expect(array.last.first).to be_kind_of(OpenStruct)
75
82
  end
76
83
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tco_client_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - 2CO Inc