lhs 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/docs/services.md +20 -0
- data/lib/lhs/concerns/service/endpoints.rb +1 -0
- data/lib/lhs/concerns/service/request.rb +1 -1
- data/lib/lhs/data.rb +8 -1
- data/lib/lhs/version.rb +1 -1
- data/spec/service/includes_spec.rb +23 -1
- data/spec/service/mapping_spec.rb +17 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edf3d097348d9c26b700bfe35df2d41b43052109
|
4
|
+
data.tar.gz: 6b2130406f136714241764b4e9c8c6a418896d3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41c472f8a71fb9f3c7a9f79ed32363754b87cbeb0f7903d3f9e3bd54ed5011056f07b208b233bf8927308e0c5d79422f59eb9c785bb8b3b8af1f91db673d5664
|
7
|
+
data.tar.gz: b875a7965529984e842668f6a92cf6b96b54929db5c8de38f6ceb5a6aa04eba94491046f0add8acb5750d06c97022fe440d5b9e07d8c24bfbfed1c0376fdf960
|
data/docs/services.md
CHANGED
@@ -221,3 +221,23 @@ class LocalEntry < LHS::Service
|
|
221
221
|
|
222
222
|
end
|
223
223
|
```
|
224
|
+
|
225
|
+
### Known services when accessing mapped data from nested data
|
226
|
+
|
227
|
+
As LHS detects services from available service definitions as soon as a link is present, mappings will also be applied on nested data:
|
228
|
+
|
229
|
+
```
|
230
|
+
class Place < LHS::Service
|
231
|
+
endpoint ':datastore/v2/places'
|
232
|
+
|
233
|
+
map :name, ->{ addresses.first.business.identities.first.name }
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
class Favorite < LHS::Service
|
238
|
+
endpoint ':datastore/v2/favorites'
|
239
|
+
end
|
240
|
+
|
241
|
+
favorite = Favorite.includes(:place).find(1)
|
242
|
+
favorite.place.name # local.ch AG
|
243
|
+
```
|
@@ -17,7 +17,7 @@ class LHS::Service
|
|
17
17
|
|
18
18
|
# Convert URLs in options to endpoint templates
|
19
19
|
def convert_options_to_endpoints(options)
|
20
|
-
if options.
|
20
|
+
if options.is_a?(Array)
|
21
21
|
options.map { |option| convert_option_to_endpoints(option) }
|
22
22
|
else
|
23
23
|
convert_option_to_endpoints(options)
|
data/lib/lhs/data.rb
CHANGED
@@ -38,7 +38,7 @@ class LHS::Data
|
|
38
38
|
# Use existing mapping to provide data
|
39
39
|
# or forward to proxy
|
40
40
|
def method_missing(name, *args, &block)
|
41
|
-
if
|
41
|
+
if mapping = mapping_for(name)
|
42
42
|
self.instance_exec(&mapping)
|
43
43
|
else
|
44
44
|
_proxy.send(name, *args, &block)
|
@@ -57,6 +57,13 @@ class LHS::Data
|
|
57
57
|
input.is_a?(Array) || _raw.is_a?(Array)
|
58
58
|
end
|
59
59
|
|
60
|
+
def mapping_for(name)
|
61
|
+
service_instance = LHS::Service.for_url(_raw['href']) if _raw.is_a?(Hash)
|
62
|
+
service_instance ||= _root._service.instance if root_item?
|
63
|
+
return unless service_instance
|
64
|
+
service_instance.mapping[name]
|
65
|
+
end
|
66
|
+
|
60
67
|
def root_item
|
61
68
|
return if self._proxy.class != LHS::Item
|
62
69
|
root = root_item = self
|
data/lib/lhs/version.rb
CHANGED
@@ -18,7 +18,29 @@ describe LHS::Service do
|
|
18
18
|
.to_return(body: { 'name' => 'Casa Ferlin' }.to_json)
|
19
19
|
end
|
20
20
|
|
21
|
-
context 'includes' do
|
21
|
+
context 'singlelevel includes' do
|
22
|
+
before(:each) do
|
23
|
+
class LocalEntry < LHS::Service
|
24
|
+
endpoint ':datastore/local-entries'
|
25
|
+
endpoint ':datastore/local-entries/:id'
|
26
|
+
end
|
27
|
+
class Favorite < LHS::Service
|
28
|
+
endpoint ':datastore/favorites'
|
29
|
+
endpoint ':datastore/favorites/:id'
|
30
|
+
end
|
31
|
+
stub_request(:get, "#{datastore}/local-entries/1")
|
32
|
+
.to_return(body: {company_name: 'local.ch'}.to_json)
|
33
|
+
stub_request(:get, "#{datastore}/favorites/1")
|
34
|
+
.to_return(body: {local_entry: {href: "#{datastore}/local-entries/1"}}.to_json)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'does single level includes' do
|
38
|
+
favorite = Favorite.includes(:local_entry).find(1)
|
39
|
+
expect(favorite.local_entry.company_name).to eq 'local.ch'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'multilevel includes' do
|
22
44
|
|
23
45
|
before(:each) do
|
24
46
|
class Feedback < LHS::Service
|
@@ -69,5 +69,22 @@ describe LHS::Service do
|
|
69
69
|
agb = Agb.includes(:preceding_agb).first!
|
70
70
|
expect(agb.pdf_url).to be == 'de'
|
71
71
|
end
|
72
|
+
|
73
|
+
it 'makes mappings available even for nested data' do
|
74
|
+
class LocalEntry < LHS::Service
|
75
|
+
map :name, ->{ company_name }
|
76
|
+
end
|
77
|
+
class Favorite < LHS::Service
|
78
|
+
endpoint ':datastore/favorites'
|
79
|
+
endpoint ':datastore/favorites/:id'
|
80
|
+
end
|
81
|
+
stub_request(:get, "#{datastore}/local-entries/1")
|
82
|
+
.to_return(body: {company_name: 'local.ch'}.to_json)
|
83
|
+
stub_request(:get, "#{datastore}/favorites/1")
|
84
|
+
.to_return(body: {local_entry: {href: "#{datastore}/local-entries/1"}}.to_json)
|
85
|
+
|
86
|
+
favorite = Favorite.includes(:local_entry).find(1)
|
87
|
+
expect(favorite.local_entry.name).to eq 'local.ch'
|
88
|
+
end
|
72
89
|
end
|
73
90
|
end
|