lhc 6.4.0 → 6.5.0

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: 80f4055f440bc7f36343e387c27cbe4526e2969e
4
- data.tar.gz: 50b0cb3ae16bc7cc9098a2e9069d03d91314304b
3
+ metadata.gz: a1870e7f765bd88ab8d601bf98828dec1dad3578
4
+ data.tar.gz: 2ee4018796f6db877e144888e3211c7cae3cd72c
5
5
  SHA512:
6
- metadata.gz: e71ceba6f6f43b0112aecf6d8b92470628cbdf29e855c237a8ec26a2cf22f13ed24add47230268daf4e74a16574fb4e5b23f72744e7ef36bece5b1945d953e7a
7
- data.tar.gz: 6f1d15f6cb8b7aff8cfc64553d8366a951f4d8bd579e57c182003be2011955bf8da967de2fb1e9fc0af8c4648e7c285928f2d8a5a7a6ba4fc19b76245bbd019a
6
+ metadata.gz: 8868211d499b67b58d67abbeadfd4fd12aabbf61d2a99f18a7f4e06ff5f51856b0203997e914e43143503c02315448913253b914b87c51c830ecbc1955b95aec
7
+ data.tar.gz: 8f558506283ca904cda4c0db565f24f1728dd9976778523dcd7b74ed155aa82dce6eb4a28a5156dd4bcaea5f9b301dd2d4ee720beae1fca6a8aeee2ad615251c
@@ -1,36 +1,26 @@
1
- require 'ostruct'
2
- # Response data is data provided through the response body
3
- # but made accssible in the ruby world
4
- class LHC::Response::Data < OpenStruct
1
+ class LHC::Response::Data
2
+ autoload :Base, 'lhc/response/data/base'
3
+ autoload :Item, 'lhc/response/data/item'
4
+ autoload :Collection, 'lhc/response/data/collection'
5
5
 
6
- def initialize(response)
7
- @response = response
8
- set_dynamic_accessor_methods
9
- super(as_json)
10
- end
6
+ include LHC::Response::Data::Base
11
7
 
12
- def as_json
13
- response.format.as_json(response)
14
- end
8
+ def initialize(response, data: nil)
9
+ @response = response
10
+ @data = data
15
11
 
16
- def as_open_struct
17
- response.format.as_open_struct(response)
12
+ if as_json.is_a?(Hash)
13
+ @base = LHC::Response::Data::Item.new(response, data: data)
14
+ elsif as_json.is_a?(Array)
15
+ @base = LHC::Response::Data::Collection.new(response, data: data)
16
+ end
18
17
  end
19
18
 
20
- def [](key)
21
- @hash ||= as_json.with_indifferent_access
22
- @hash[key]
19
+ def method_missing(method, *args, &block)
20
+ @base.send(method, *args, &block)
23
21
  end
24
22
 
25
- private
26
-
27
- attr_reader :response
28
-
29
- def set_dynamic_accessor_methods
30
- as_json.keys.each do |key|
31
- define_singleton_method key do |*args|
32
- as_open_struct.send key, *args
33
- end
34
- end
23
+ def respond_to_missing?(method_name, include_private = false)
24
+ @base.respond_to?(method_name, include_private) || super
35
25
  end
36
26
  end
@@ -0,0 +1,20 @@
1
+ # Response data is data provided through the response body
2
+ # but made accssible in the ruby world
3
+ module LHC::Response::Data::Base
4
+ def as_json
5
+ @json ||= (@data || response.format.as_json(response))
6
+ end
7
+
8
+ def as_open_struct
9
+ @open_struct ||=
10
+ if @data
11
+ JSON.parse(@data.to_json, object_class: OpenStruct)
12
+ else
13
+ response.format.as_open_struct(response)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :response
20
+ end
@@ -0,0 +1,14 @@
1
+ class LHC::Response::Data::Collection < Array
2
+ include LHC::Response::Data::Base
3
+
4
+ def initialize(response, data: nil)
5
+ @response = response
6
+ @data = data
7
+
8
+ super(
9
+ as_json.map do |i|
10
+ LHC::Response::Data.new(response, data: i)
11
+ end
12
+ )
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ class LHC::Response::Data::Item < OpenStruct
2
+ include LHC::Response::Data::Base
3
+
4
+ def initialize(response, data: nil)
5
+ @response = response
6
+ @data = data
7
+
8
+ set_dynamic_accessor_methods
9
+
10
+ super(as_json)
11
+ end
12
+
13
+ def [](key)
14
+ @hash ||= as_json.with_indifferent_access
15
+ @hash[key]
16
+ end
17
+
18
+ private
19
+
20
+ def set_dynamic_accessor_methods
21
+ as_json.keys.each do |key|
22
+ define_singleton_method key do |*args|
23
+ as_open_struct.send key, *args
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= "6.4.0"
2
+ VERSION ||= "6.5.0"
3
3
  end
@@ -4,30 +4,56 @@ describe LHC::Response do
4
4
  context 'data' do
5
5
  let(:value) { 'some_value' }
6
6
 
7
- let(:body) { { some_key: { nested: value } } }
8
-
9
7
  let(:raw_response) { OpenStruct.new(body: body.to_json) }
10
8
 
11
9
  let(:response) { LHC::Response.new(raw_response, nil) }
12
10
 
13
- it 'makes data from response body available' do
14
- expect(response.data.some_key.nested).to eq value
15
- end
11
+ context 'for item' do
12
+ let(:body) { { some_key: { nested: value } } }
16
13
 
17
- it 'makes data from response body available with hash bracket notation' do
18
- expect(response.data[:some_key][:nested]).to eq value
19
- end
14
+ it 'makes data from response body available' do
15
+ expect(response.data.some_key.nested).to eq value
16
+ end
20
17
 
21
- it 'can be converted to json with the as_json method' do
22
- expect(response.data.as_json).to eq body.as_json
23
- end
18
+ it 'makes data from response body available with hash bracket notation' do
19
+ expect(response.data[:some_key][:nested]).to eq value
20
+ end
21
+
22
+ it 'can be converted to json with the as_json method' do
23
+ expect(response.data.as_json).to eq body.as_json
24
+ end
25
+
26
+ it 'can be converted to an open struct with the as_open_struct method' do
27
+ expect(response.data.as_open_struct).to eq JSON.parse(response.body, object_class: OpenStruct)
28
+ end
24
29
 
25
- it 'can be converted to an open struct with the as_open_struct method' do
26
- expect(response.data.as_open_struct).to eq JSON.parse(response.body, object_class: OpenStruct)
30
+ it 'returns nil when data is not available' do
31
+ expect(response.data.something).to be_nil
32
+ end
27
33
  end
28
34
 
29
- it 'returns nil when data is not available' do
30
- expect(response.data.something).to be_nil
35
+ context 'for collection' do
36
+ let(:body) { [{ some_key: { nested: value } }] }
37
+
38
+ it 'can be converted to json with the as_json method' do
39
+ expect(response.data.as_json).to eq body.as_json
40
+ end
41
+
42
+ it 'can be converted to an open struct with the as_open_struct method' do
43
+ expect(response.data.as_open_struct).to eq JSON.parse(response.body, object_class: OpenStruct)
44
+ end
45
+
46
+ it 'is a collection of items' do
47
+ expect(response.data.size).to eq(1)
48
+ end
49
+
50
+ it 'makes item data from response body available' do
51
+ expect(response.data.first.some_key.nested).to eq value
52
+ end
53
+
54
+ it 'makes item data from response body available with hash bracket notation' do
55
+ expect(response.data.first[:some_key][:nested]).to eq value
56
+ end
31
57
  end
32
58
  end
33
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.4.0
4
+ version: 6.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-05 00:00:00.000000000 Z
11
+ date: 2017-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -195,6 +195,9 @@ files:
195
195
  - lib/lhc/request.rb
196
196
  - lib/lhc/response.rb
197
197
  - lib/lhc/response/data.rb
198
+ - lib/lhc/response/data/base.rb
199
+ - lib/lhc/response/data/collection.rb
200
+ - lib/lhc/response/data/item.rb
198
201
  - lib/lhc/test/cache_helper.rb
199
202
  - lib/lhc/version.rb
200
203
  - script/ci/build.sh
@@ -321,7 +324,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
321
324
  requirements:
322
325
  - Ruby >= 2.0.0
323
326
  rubyforge_project:
324
- rubygems_version: 2.6.8
327
+ rubygems_version: 2.6.12
325
328
  signing_key:
326
329
  specification_version: 4
327
330
  summary: LocalHttpClient