lhc 3.6.1 → 3.7.0.pre1
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/README.md +12 -0
- data/lib/lhc/formats/json.rb +14 -4
- data/lib/lhc/response.rb +5 -8
- data/lib/lhc/response/data.rb +34 -0
- data/lib/lhc/version.rb +1 -1
- data/spec/response/data_spec.rb +14 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ba7b312631cdd807ba15789447528102d6c6c65
|
4
|
+
data.tar.gz: 7ac5531c6deecaa7a9ec73c9422443acffdcffd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ccb2e358ec64e1a396e6e05b1e024a0af8d846bf579e78ff01bc42d69f1877de713594386cd33cfda97ad2b17d7ffa9a936d5903f67c585a1d97cd4e04e48e6c
|
7
|
+
data.tar.gz: 524a3e8fc64058a97594ff4b5ebd3bf80e4e2853a3bd24288c3a7970f345f08c6d0f80863722c62457875608b6ea048daa05f8acaffaf4caa38c64284148bc32
|
data/README.md
CHANGED
@@ -45,6 +45,18 @@ Currently supported formats: `json`
|
|
45
45
|
|
46
46
|
→ [Read more about the response object](docs/response.md)
|
47
47
|
|
48
|
+
## Accessing data
|
49
|
+
|
50
|
+
The response data can be access with dot-notation and square-bracket notation. You can convert response data to open structs or json (if the response format is json).
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
response = LHC.request(url: 'http://datastore/entry/1')
|
54
|
+
response.data.as_open_struct #<OpenStruct name='local.ch'>
|
55
|
+
response.data.as_json # { name: 'local.ch' }
|
56
|
+
response.data.name # 'local.ch'
|
57
|
+
response.data[:name] # 'local.ch'
|
58
|
+
```
|
59
|
+
|
48
60
|
## Parallel requests
|
49
61
|
|
50
62
|
If you pass an array of requests to `LHC.request`, it will perform those requests in parallel.
|
data/lib/lhc/formats/json.rb
CHANGED
@@ -9,10 +9,12 @@ class JsonFormat
|
|
9
9
|
super(options)
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
def as_json(response)
|
13
|
+
parse(response, Hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
def as_open_struct(response)
|
17
|
+
parse(response, OpenStruct)
|
16
18
|
end
|
17
19
|
|
18
20
|
def to_s
|
@@ -22,4 +24,12 @@ class JsonFormat
|
|
22
24
|
def to_sym
|
23
25
|
to_s.to_sym
|
24
26
|
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def parse(response, object_class)
|
31
|
+
JSON.parse(response.body, object_class: object_class)
|
32
|
+
rescue JSON::ParserError => e
|
33
|
+
raise LHC::ParserError.new(e.message, response)
|
34
|
+
end
|
25
35
|
end
|
data/lib/lhc/response.rb
CHANGED
@@ -13,11 +13,8 @@ class LHC::Response
|
|
13
13
|
self.raw = raw
|
14
14
|
end
|
15
15
|
|
16
|
-
# Access response data.
|
17
|
-
# Cache parsing.
|
18
16
|
def data
|
19
|
-
@data ||=
|
20
|
-
@data
|
17
|
+
@data ||= LHC::Response::Data.new(self)
|
21
18
|
end
|
22
19
|
|
23
20
|
def effective_url
|
@@ -53,13 +50,13 @@ class LHC::Response
|
|
53
50
|
raw.success?
|
54
51
|
end
|
55
52
|
|
56
|
-
private
|
57
|
-
|
58
|
-
attr_accessor :raw
|
59
|
-
|
60
53
|
def format
|
61
54
|
return JsonFormat.new if request.nil?
|
62
55
|
request.format
|
63
56
|
end
|
64
57
|
|
58
|
+
private
|
59
|
+
|
60
|
+
attr_accessor :raw
|
61
|
+
|
65
62
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Response data is data provided through the response body
|
2
|
+
# but made accssible in the ruby world
|
3
|
+
class LHC::Response::Data
|
4
|
+
|
5
|
+
def initialize(response)
|
6
|
+
@response = response
|
7
|
+
set_dynamic_accessor_methods
|
8
|
+
end
|
9
|
+
|
10
|
+
def as_json
|
11
|
+
response.format.as_json(response)
|
12
|
+
end
|
13
|
+
|
14
|
+
def as_open_struct
|
15
|
+
response.format.as_open_struct(response)
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](key)
|
19
|
+
@hash ||= as_json.with_indifferent_access
|
20
|
+
@hash[key]
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_reader :response
|
26
|
+
|
27
|
+
def set_dynamic_accessor_methods
|
28
|
+
as_json.keys.each do |key|
|
29
|
+
define_singleton_method key do |*args|
|
30
|
+
as_open_struct.send key, *args
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/lhc/version.rb
CHANGED
data/spec/response/data_spec.rb
CHANGED
@@ -8,9 +8,22 @@ describe LHC::Response do
|
|
8
8
|
|
9
9
|
let(:raw_response) { OpenStruct.new(body: body.to_json) }
|
10
10
|
|
11
|
+
let(:response) { LHC::Response.new(raw_response, nil) }
|
12
|
+
|
11
13
|
it 'makes data from response body available' do
|
12
|
-
response = LHC::Response.new(raw_response, nil)
|
13
14
|
expect(response.data.some_key.nested).to eq value
|
14
15
|
end
|
16
|
+
|
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
|
20
|
+
|
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
|
24
|
+
|
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)
|
27
|
+
end
|
15
28
|
end
|
16
29
|
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: 3.
|
4
|
+
version: 3.7.0.pre1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- local.ch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typhoeus
|
@@ -167,6 +167,7 @@ files:
|
|
167
167
|
- lib/lhc/interceptor_processor.rb
|
168
168
|
- lib/lhc/request.rb
|
169
169
|
- lib/lhc/response.rb
|
170
|
+
- lib/lhc/response/data.rb
|
170
171
|
- lib/lhc/version.rb
|
171
172
|
- non_rails_spec/request/request_spec.rb
|
172
173
|
- script/ci/build.sh
|
@@ -271,9 +272,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
271
272
|
version: 1.9.2
|
272
273
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
273
274
|
requirements:
|
274
|
-
- - "
|
275
|
+
- - ">"
|
275
276
|
- !ruby/object:Gem::Version
|
276
|
-
version:
|
277
|
+
version: 1.3.1
|
277
278
|
requirements:
|
278
279
|
- Ruby >= 1.9.2
|
279
280
|
rubyforge_project:
|