lhs 14.0.3 → 14.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: d2cb43c14345dbda2239e8714ea2c43ff9c41c56
4
- data.tar.gz: 4d076f553a644860357e8adf22e9fdf8598a0afa
3
+ metadata.gz: 504f8883ad45f75b004de4cbf3dbf5aa1e9e12bc
4
+ data.tar.gz: 1076be76ce857bc401a2222e7c1459cf9f300901
5
5
  SHA512:
6
- metadata.gz: fe3a0437e13e0b82952da892e845625fb2c2039acbbf06f1c842fb8c726a957c9c0b7309ebb08d4d182bae14d605383f0108a67fb198b895b92da06c08b6fce8
7
- data.tar.gz: 92686aae195ca9d03479f50971b3e3e3cd466f7dd615a8b1d7ab6cd9c4f43f115389b4aaef74877377baded2cfa878245fbc64676303a0dd0475328ab8ac88e2
6
+ metadata.gz: 41286eb7d5bcf95c24c39d67ac6c5d35460d66514ead66bebcbcda7256c8322ab7706058c1920ffceb00e1fd9e887baa05285d8eb43fd40e03c438b4b8f2f648
7
+ data.tar.gz: b1401bde9eb82be62b2d90750adfbaae6b23349054e088b9142db8b8ddfabdde92841c7a61701a24a53bb183d49e2b3b5f9301b4cdefd4c6bcdf1a8ef39b13ee
data/README.md CHANGED
@@ -914,6 +914,8 @@ class Search < LHS::Record
914
914
  end
915
915
  ```
916
916
 
917
+ `item_key` key used to unwrap the actuall object from within the response body.
918
+
917
919
  `items_key` key used to determine items of the current page (e.g. `docs`, `items`, etc.).
918
920
 
919
921
  `item_created_key` key used to merge record data thats nested in the creation response body.
@@ -926,6 +928,27 @@ end
926
928
 
927
929
  `total_key` key used to determine the total amount of items (e.g. `total`, `totalResults`, etc.).
928
930
 
931
+ ### Unwrap nested items
932
+
933
+ ```json
934
+ {
935
+ "response": {
936
+ "location": {
937
+ "id": 123
938
+ }
939
+ }
940
+ }
941
+ ```
942
+
943
+ ```ruby
944
+ class Location < LHS::Record
945
+ configuration item_key: [:response, :location]
946
+ end
947
+
948
+ location = Location.find(123)
949
+ location.id # 123
950
+ ```
951
+
929
952
  ### Configure complex accessors for nested data (EXPERIMENTAL)
930
953
 
931
954
  If items, limit, pagination, total etc. is nested in the responding objects, use complex data structures for configuring a record.
@@ -14,6 +14,12 @@ class LHS::Record
14
14
  @configuration = args.freeze || {}
15
15
  end
16
16
 
17
+ def item_key
18
+ symbolize_unless_complex(
19
+ @configuration.try(:[], :item_key) || :item
20
+ )
21
+ end
22
+
17
23
  def items_key
18
24
  symbolize_unless_complex(
19
25
  @configuration.try(:[], :items_key) || :items
@@ -17,8 +17,8 @@ class LHS::Record
17
17
  else
18
18
  find_by_id(args, options)
19
19
  end
20
- return data if data && (data.is_a?(Array) || !data._record)
21
- data ? data._record.new(data) : nil
20
+ return data if data && (data.is_a?(Array) || !data._record || data.collection?)
21
+ data ? data._record.new(data.unwrap_nested_item) : nil
22
22
  end
23
23
 
24
24
  private
@@ -27,7 +27,7 @@ class LHS::Record
27
27
  if data && data._proxy.is_a?(LHS::Collection)
28
28
  data.first || raise(LHC::NotFound.new('No item was found.', data._request.response))
29
29
  elsif data
30
- data._record.new(data)
30
+ data._record.new(data.unwrap_nested_item)
31
31
  end
32
32
  end
33
33
  end
@@ -41,4 +41,11 @@ class LHS::Item < LHS::Proxy
41
41
  # We accept every message that does not belong to set of keywords
42
42
  BLACKLISTED_KEYWORDS.exclude?(name.to_s)
43
43
  end
44
+
45
+ def unwrap_nested_item
46
+ return _data unless _record.item_key
47
+ nested_data = _data.dig(*_record.item_key)
48
+ return _data unless nested_data
49
+ nested_data
50
+ end
44
51
  end
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = '14.0.3'
2
+ VERSION = '14.1.0'
3
3
  end
@@ -0,0 +1,43 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHS::Record do
4
+ before(:each) do
5
+ class Location < LHS::Record
6
+ configuration item_key: [:response, :location]
7
+ endpoint 'http://uberall/location'
8
+ endpoint 'http://uberall/location/:id'
9
+ end
10
+ end
11
+
12
+ let(:json_body) do
13
+ {
14
+ response: {
15
+ location: {
16
+ id: 1
17
+ }
18
+ }
19
+ }.to_json
20
+ end
21
+
22
+ let(:stub_request_by_id) do
23
+ stub_request(:get, "http://uberall/location/1")
24
+ .to_return(body: json_body)
25
+ end
26
+
27
+ let(:stub_request_by_get_parameters) do
28
+ stub_request(:get, "http://uberall/location?identifier=1&limit=1")
29
+ .to_return(body: json_body)
30
+ end
31
+
32
+ it 'uses configured item_key to unwrap response data for find' do
33
+ stub_request_by_id
34
+ location = Location.find(1)
35
+ expect(location.id).to eq 1
36
+ end
37
+
38
+ it 'uses configured item_key to unwrap response data for find_by' do
39
+ stub_request_by_get_parameters
40
+ location = Location.find_by(identifier: 1)
41
+ expect(location.id).to eq 1
42
+ end
43
+ 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: 14.0.3
4
+ version: 14.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-09-06 00:00:00.000000000 Z
11
+ date: 2017-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc
@@ -365,6 +365,7 @@ files:
365
365
  - spec/record/includes_all_spec.rb
366
366
  - spec/record/includes_spec.rb
367
367
  - spec/record/includes_warning_spec.rb
368
+ - spec/record/item_key_spec.rb
368
369
  - spec/record/items_created_key_configuration_spec.rb
369
370
  - spec/record/loading_twice_spec.rb
370
371
  - spec/record/mapping_spec.rb
@@ -418,7 +419,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
418
419
  requirements:
419
420
  - Ruby >= 2.3.0
420
421
  rubyforge_project:
421
- rubygems_version: 2.6.13
422
+ rubygems_version: 2.6.8
422
423
  signing_key:
423
424
  specification_version: 4
424
425
  summary: Rails gem providing an easy, active-record-like interface for http json services
@@ -540,6 +541,7 @@ test_files:
540
541
  - spec/record/includes_all_spec.rb
541
542
  - spec/record/includes_spec.rb
542
543
  - spec/record/includes_warning_spec.rb
544
+ - spec/record/item_key_spec.rb
543
545
  - spec/record/items_created_key_configuration_spec.rb
544
546
  - spec/record/loading_twice_spec.rb
545
547
  - spec/record/mapping_spec.rb