lhs 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40403e2ae293ace8b6504d10b3f25ed4696b3fc4
4
- data.tar.gz: 5cb53fbd0fb4742d5b4a0ddf70313b56f9c67de9
3
+ metadata.gz: edf3d097348d9c26b700bfe35df2d41b43052109
4
+ data.tar.gz: 6b2130406f136714241764b4e9c8c6a418896d3b
5
5
  SHA512:
6
- metadata.gz: 5ad6899b426c29d006b48f3fa7d3cada987a55a7311c8c7d7ab8a2dc38ebeb661b2f359d01a6b9c92dddfb438568cb35cbdce6bbef5aa537b9d7a4d8ce298b08
7
- data.tar.gz: 3e540dccae57014ded112e0237a330ede9fcd4e91e9f702eef735edf3f9fde93d7fa1ddb735941b08af84f7cd80041db25e48262eea4fc8a6cc580c746dc1c95
6
+ metadata.gz: 41c472f8a71fb9f3c7a9f79ed32363754b87cbeb0f7903d3f9e3bd54ed5011056f07b208b233bf8927308e0c5d79422f59eb9c785bb8b3b8af1f91db673d5664
7
+ data.tar.gz: b875a7965529984e842668f6a92cf6b96b54929db5c8de38f6ceb5a6aa04eba94491046f0add8acb5750d06c97022fe440d5b9e07d8c24bfbfed1c0376fdf960
@@ -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
+ ```
@@ -24,6 +24,7 @@ class LHS::Service
24
24
  end
25
25
 
26
26
  def for_url(url)
27
+ return unless url
27
28
  template, service = LHS::Service::Endpoints.all.detect do |template, _service|
28
29
  LHC::Endpoint.match?(url, template)
29
30
  end
@@ -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.respond_to?(:map)
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)
@@ -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 root_item? && mapping = _root._service.instance.mapping[name]
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
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - local.ch