lhs 9.0.4 → 9.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: eb534b7591faab4cd810b3cadc57187834180736
4
- data.tar.gz: 10ebca98180dab9bf47532bba6b82ed9011dff59
3
+ metadata.gz: 6332862581250354312558fba4a8aa584b4a0611
4
+ data.tar.gz: 61d791ccc6b3153ad1928c9d743abfaac4005eff
5
5
  SHA512:
6
- metadata.gz: ac2ab9727befddaabc2684dd021d906e8c0c58f41dda4e9695b1847ed8716a0148604a89c4f830883bd8bc62a5958017f2a927927eca77cd81ed06299896dda1
7
- data.tar.gz: a71dfe943c674098b6195df7e9a26addfe2cc35cc6bef4ce8093ddd284a0ea7a114f0bc27f2553a006325728c574ea2dcba2fdced82c8269ae7376c96362d697
6
+ metadata.gz: e47a127a76131cb1cf82f65c525fdc55b29e32b24b9d57366a24aa7ce2d034e3fc1681e9b239527e8d98bd1628f56a69e8b03f30fce5d5ed544b3d22f4c00412
7
+ data.tar.gz: 3961c7b47fca9e438cc426cef6ea3daa873be38210ff46684726371019981397576c78235d08f50e893675abe0cddcdec83aa62e16d416b6e4905fd84e369bd4
data/README.md CHANGED
@@ -71,6 +71,32 @@ This uses the `:service/v2/records` endpoint, cause `:association_id` was not pr
71
71
 
72
72
  Uses the `:service/v2/association/:association_id/records` endpoint.
73
73
 
74
+ ### Expand plain collection of links
75
+
76
+ Some endpoints could respond a plain list of links without any expanded data. Like search endpoints.
77
+ If you want to have LHS expand those items, use `expanded` as part of a Query-Chain:
78
+
79
+ ```json
80
+ {
81
+ "items" : [
82
+ {"href": "http://local.ch/customer/1/accounts/1"},
83
+ {"href": "http://local.ch/customer/1/accounts/2"},
84
+ {"href": "http://local.ch/customer/1/accounts/3"}
85
+ ]
86
+ }
87
+ end
88
+ ```
89
+
90
+ ```ruby
91
+ Account.where(customer_id: 123).expanded
92
+ ```
93
+
94
+ You can also apply options to `expanded` in order to apply anything on the requests made to expand the links:
95
+
96
+ ```ruby
97
+ Account.where(customer_id: 123).expanded(auth: { bearer: access_token })
98
+ ```
99
+
74
100
  ## Chaining where statements
75
101
 
76
102
  LHS supports chaining where statements.
@@ -21,6 +21,10 @@ class LHS::Record
21
21
  chain
22
22
  end
23
23
 
24
+ def expanded(options = nil)
25
+ Chain.new(self, Option.new(expanded: options || true))
26
+ end
27
+
24
28
  def options(hash = nil)
25
29
  Chain.new(self, Option.new(hash))
26
30
  end
@@ -181,6 +185,10 @@ class LHS::Record
181
185
  push([Parameter.new(hash), Option.new(all: true)])
182
186
  end
183
187
 
188
+ def expanded(options = nil)
189
+ push(Option.new(expanded: options || true))
190
+ end
191
+
184
192
  def options(hash = nil)
185
193
  push(Option.new(hash))
186
194
  end
@@ -422,10 +422,22 @@ class LHS::Record
422
422
  response = LHC.request(process_options(options, endpoint))
423
423
  data = LHS::Data.new(response.body, nil, self, response.request, endpoint)
424
424
  load_and_merge_remaining_objects!(data, process_options(options, endpoint)) if paginated?(data._raw) && options[:all]
425
+ expand_items(data, options[:expanded]) if data.collection? && options[:expanded]
425
426
  handle_includes(including, data, referencing) if including.present? && data.present?
426
427
  data
427
428
  end
428
429
 
430
+ def expand_items(data, expand_options)
431
+ expand_options = {} unless expand_options.is_a?(Hash)
432
+ options = data.map do |item|
433
+ expand_options.merge(url: item.href)
434
+ end
435
+ expanded_data = request(options)
436
+ data.each_with_index do |item, index|
437
+ item.merge_raw!(expanded_data[index])
438
+ end
439
+ end
440
+
429
441
  def url_option_for(item, key = nil)
430
442
  link = key ? item[key] : item
431
443
  return if link.blank?
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "9.0.4"
2
+ VERSION = "9.1.0"
3
3
  end
@@ -0,0 +1,67 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHS::Record do
4
+ before(:each) do
5
+ class Record < LHS::Record
6
+ endpoint 'http://local.ch/v2/records'
7
+ end
8
+ end
9
+
10
+ let!(:request_collection) do
11
+ stub_request(:get, "http://local.ch/v2/records?color=blue")
12
+ .to_return(body: {
13
+ items: [
14
+ { href: 'http://local.ch/v2/records/1' },
15
+ { href: 'http://local.ch/v2/records/2' }
16
+ ]
17
+ }.to_json)
18
+ end
19
+
20
+ let!(:request_item_1) do
21
+ stub_request(:get, "http://local.ch/v2/records/1?via=collection")
22
+ .to_return(body: {
23
+ name: 'Steve'
24
+ }.to_json)
25
+ end
26
+
27
+ let!(:request_item_2) do
28
+ stub_request(:get, "http://local.ch/v2/records/2?via=collection")
29
+ .to_return(body: {
30
+ name: 'John'
31
+ }.to_json)
32
+ end
33
+
34
+ it 'expands collections that just contains links' do
35
+ records = Record.where(color: 'blue').expanded(params: { via: 'collection' })
36
+ expect(records[0].name).to eq 'Steve'
37
+ expect(records[1].name).to eq 'John'
38
+ assert_requested request_collection
39
+ assert_requested request_item_1
40
+ assert_requested request_item_2
41
+ end
42
+
43
+ context 'without options' do
44
+ let!(:request_item_1) do
45
+ stub_request(:get, "http://local.ch/v2/records/1")
46
+ .to_return(body: {
47
+ name: 'Steve'
48
+ }.to_json)
49
+ end
50
+
51
+ let!(:request_item_2) do
52
+ stub_request(:get, "http://local.ch/v2/records/2")
53
+ .to_return(body: {
54
+ name: 'John'
55
+ }.to_json)
56
+ end
57
+
58
+ it 'works also without options' do
59
+ records = Record.where(color: 'blue').expanded
60
+ expect(records[0].name).to eq 'Steve'
61
+ expect(records[1].name).to eq 'John'
62
+ assert_requested request_collection
63
+ assert_requested request_item_1
64
+ assert_requested request_item_2
65
+ end
66
+ end
67
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.0.4
4
+ version: 9.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-27 00:00:00.000000000 Z
11
+ date: 2017-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc
@@ -324,6 +324,7 @@ files:
324
324
  - spec/record/endpoint_options_spec.rb
325
325
  - spec/record/endpoints_spec.rb
326
326
  - spec/record/equality_spec.rb
327
+ - spec/record/expanded_spec.rb
327
328
  - spec/record/find_by_chains_spec.rb
328
329
  - spec/record/find_by_spec.rb
329
330
  - spec/record/find_each_spec.rb
@@ -481,6 +482,7 @@ test_files:
481
482
  - spec/record/endpoint_options_spec.rb
482
483
  - spec/record/endpoints_spec.rb
483
484
  - spec/record/equality_spec.rb
485
+ - spec/record/expanded_spec.rb
484
486
  - spec/record/find_by_chains_spec.rb
485
487
  - spec/record/find_by_spec.rb
486
488
  - spec/record/find_each_spec.rb