lhs 2.0.3 → 2.0.4

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: 1b80422557549f4c6187df6ec6068be2d4186317
4
- data.tar.gz: ed4841db53bf4da2636c33167eea5171f6064199
3
+ metadata.gz: 583ea8a5785e4d78e732150986bcb5a05f5f8397
4
+ data.tar.gz: a488798e7c906e0806d8d0d78ea3e4b5be8c87e1
5
5
  SHA512:
6
- metadata.gz: 475b1f2ba9d9082f086e3e2777b28cd19eea593ca2678484c72121e5b128667f1f324c85b1b869ba032084076a4c994565f60c0c403e6b9737fc55133a8ef17b
7
- data.tar.gz: ee009de8735ccd8ae997b6c97530a6dee5243a34c2498fc54c7c35b6421f0831845007ae8b32ededc02a41b2db7737c6bd1ee5eb1a224d2f0f52e4ca6259e3c5
6
+ metadata.gz: a0251caac24d2fd88395ac78af1a0e044b6f887f3750ff84fe1ccc9aa43ca42bf4dd80cbc11c450a793b9ee902bfdc621b7e3dba84fc938de38495c24de37282
7
+ data.tar.gz: d7ca3bcc747f89051c5ad59294dc8046b5a2553ad50a6243110ecebf44d9e65ddb551a5a782da5025cda4cd16fec520a0258c0b70da82d07261e81b65361546c
@@ -183,6 +183,19 @@ and you should read it to understand this feature in all its glory.
183
183
  feedbacks.first.campaign.entry.name # 'Casa Ferlin'
184
184
  ```
185
185
 
186
+ ### Multiple `includes`
187
+
188
+ ```ruby
189
+ # list of includes
190
+ claims = Claims.includes(:localch_account, :entry).where(place_id: 'huU90mB_6vAfUdVz_uDoyA')
191
+
192
+ # array of includes
193
+ claims = Claims.includes([:localch_account, :entry]).where(place_id: 'huU90mB_6vAfUdVz_uDoyA')
194
+
195
+ # Two-level with array of includes
196
+ feedbacks = Feedback.includes(campaign: [:entry, :user]).where(has_reviews: true)
197
+ ```
198
+
186
199
  ### Known services are used to request linked resources
187
200
 
188
201
  When including linked resources with `includes`, known/defined services and endpoints are used to make those requests.
@@ -244,4 +257,4 @@ favorite.place.name # local.ch AG
244
257
 
245
258
  ## Validation
246
259
 
247
- See: [Item Validation](../item.md).
260
+ See: [Item Validation](../item.md).
@@ -9,11 +9,11 @@ class LHS::Service
9
9
 
10
10
  module ClassMethods
11
11
 
12
- def includes(args)
12
+ def includes(*args)
13
13
  class_clone = clone
14
14
  class_clone.instance.endpoints = instance.endpoints
15
15
  class_clone.instance.mapping = instance.mapping
16
- class_clone.instance.includes = args
16
+ class_clone.instance.includes = args.size == 1 ? args[0] : args
17
17
  class_clone
18
18
  end
19
19
  end
@@ -48,6 +48,8 @@ class LHS::Service
48
48
  def handle_includes(data)
49
49
  if includes.is_a? Hash
50
50
  includes.keys.each { |key| handle_include(data, key) }
51
+ elsif includes.is_a? Array
52
+ includes.each { |key| handle_include(data, key) }
51
53
  else
52
54
  handle_include(data, includes)
53
55
  end
@@ -60,12 +62,12 @@ class LHS::Service
60
62
  else
61
63
  url_option_for(data, key)
62
64
  end
63
- addition = load_includes(includes, options, key, data)
65
+ addition = load_include(includes, options, key, data)
64
66
  extend(data, addition, key)
65
67
  end
66
68
 
67
69
  # Load additional resources that are requested with include
68
- def load_includes(includes, options, key, data)
70
+ def load_include(includes, options, key, data)
69
71
  service = service_for_options(options) || self
70
72
  options = convert_options_to_endpoints(options) if service_for_options(options)
71
73
  further_keys = includes.fetch(key, nil) if includes.is_a? Hash
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "2.0.3"
2
+ VERSION = "2.0.4"
3
3
  end
@@ -9,7 +9,8 @@ describe LHS::Service do
9
9
  stub_request(:get, "#{datastore}/content-ads/51dfc5690cf271c375c5a12d")
10
10
  .to_return(body: {
11
11
  'href' => "#{datastore}/content-ads/51dfc5690cf271c375c5a12d",
12
- 'entry' => { 'href' => "#{datastore}/local-entries/lakj35asdflkj1203va" }
12
+ 'entry' => { 'href' => "#{datastore}/local-entries/lakj35asdflkj1203va" },
13
+ 'user' => { 'href' => "#{datastore}/users/lakj35asdflkj1203va" }
13
14
  }.to_json)
14
15
  end
15
16
 
@@ -18,26 +19,52 @@ describe LHS::Service do
18
19
  .to_return(body: { 'name' => 'Casa Ferlin' }.to_json)
19
20
  end
20
21
 
22
+ let(:stub_user_request) do
23
+ stub_request(:get, "#{datastore}/users/lakj35asdflkj1203va")
24
+ .to_return(body: { 'name' => 'Mario' }.to_json)
25
+ end
26
+
21
27
  context 'singlelevel includes' do
22
28
  before(:each) do
23
- class LocalEntry < LHS::Service
29
+ class LocalEntry < LHS::Service
24
30
  endpoint ':datastore/local-entries'
25
31
  endpoint ':datastore/local-entries/:id'
26
32
  end
33
+ class User < LHS::Service
34
+ endpoint ':datastore/users'
35
+ endpoint ':datastore/users/:id'
36
+ end
27
37
  class Favorite < LHS::Service
28
38
  endpoint ':datastore/favorites'
29
39
  endpoint ':datastore/favorites/:id'
30
40
  end
31
41
  stub_request(:get, "#{datastore}/local-entries/1")
32
42
  .to_return(body: {company_name: 'local.ch'}.to_json)
43
+ stub_request(:get, "#{datastore}/users/1")
44
+ .to_return(body: {name: 'Mario'}.to_json)
33
45
  stub_request(:get, "#{datastore}/favorites/1")
34
- .to_return(body: {local_entry: {href: "#{datastore}/local-entries/1"}}.to_json)
46
+ .to_return(body: {
47
+ local_entry: {href: "#{datastore}/local-entries/1"},
48
+ user: {href: "#{datastore}/users/1"}
49
+ }.to_json)
35
50
  end
36
51
 
37
- it 'does single level includes' do
52
+ it 'includes a resource' do
38
53
  favorite = Favorite.includes(:local_entry).find(1)
39
54
  expect(favorite.local_entry.company_name).to eq 'local.ch'
40
55
  end
56
+
57
+ it 'includes a list of resources' do
58
+ favorite = Favorite.includes(:local_entry, :user).find(1)
59
+ expect(favorite.local_entry.company_name).to eq 'local.ch'
60
+ expect(favorite.user.name).to eq 'Mario'
61
+ end
62
+
63
+ it 'includes an array of resources' do
64
+ favorite = Favorite.includes([:local_entry, :user]).find(1)
65
+ expect(favorite.local_entry.company_name).to eq 'local.ch'
66
+ expect(favorite.user.name).to eq 'Mario'
67
+ end
41
68
  end
42
69
 
43
70
  context 'multilevel includes' do
@@ -49,6 +76,7 @@ describe LHS::Service do
49
76
  end
50
77
  stub_campaign_request
51
78
  stub_entry_request
79
+ stub_user_request
52
80
  end
53
81
 
54
82
  it 'includes linked resources while fetching multiple resources from one service' do
@@ -79,6 +107,19 @@ describe LHS::Service do
79
107
  expect(feedbacks.campaign.entry.name).to eq 'Casa Ferlin'
80
108
  end
81
109
 
110
+ it 'includes linked resources with array while fetching a single resource from one service' do
111
+
112
+ stub_request(:get, "#{datastore}/feedbacks/123")
113
+ .to_return(status: 200, body: {
114
+ 'href' => "#{datastore}/feedbacks/-Sc4_pYNpqfsudzhtivfkA",
115
+ 'campaign' => { 'href' => "#{datastore}/content-ads/51dfc5690cf271c375c5a12d" }
116
+ }.to_json)
117
+
118
+ feedbacks = Feedback.includes(campaign: [:entry, :user]).find(123)
119
+ expect(feedbacks.campaign.entry.name).to eq 'Casa Ferlin'
120
+ expect(feedbacks.campaign.user.name).to eq 'Mario'
121
+ end
122
+
82
123
  context 'include objects from known services' do
83
124
 
84
125
  let(:stub_feedback_request) do
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: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - local.ch