lhs 24.1.0 → 24.1.1
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 +4 -4
- data/lib/lhs/concerns/collection/internal_collection.rb +12 -4
- data/lib/lhs/version.rb +1 -1
- data/spec/record/compact_spec.rb +29 -6
- data/spec/record/references_spec.rb +43 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00243f3b3ea3dd2e97864c126dc7b8177c3f6e38cd217aba7c1a2843d9215d4c
|
4
|
+
data.tar.gz: 2c63934cbc1752513d1336997d2c9743c40b518d31b177d30f18a6ffe00ea99a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 492a78447b6b7985bed8a82025a95551f94943f6e712d09863ca99d3952bb16a26da46a88d6fc1edc5cb744dae230eab22319a038c3a0e67e76747e406afd2aa
|
7
|
+
data.tar.gz: 274eacd3f4815d81514b7273d6613851be0eb6427cd7c9fca1c0a8105cd5a28e62e1f5b740ad31331446a10d2cfe6fdc13c64808fb957511cbcf6b4e7790dc99
|
@@ -47,7 +47,7 @@ class LHS::Collection < LHS::Proxy
|
|
47
47
|
if item.is_a?(LHS::Data) && item._request && !item._request.response.success?
|
48
48
|
nil
|
49
49
|
else
|
50
|
-
item
|
50
|
+
cast_item(item)
|
51
51
|
end
|
52
52
|
end.compact
|
53
53
|
end
|
@@ -55,10 +55,18 @@ class LHS::Collection < LHS::Proxy
|
|
55
55
|
private
|
56
56
|
|
57
57
|
def cast_item(item)
|
58
|
-
record_by_href = LHS::Record.for_url(item[:href]) if item[:href]
|
59
58
|
data = LHS::Data.new(item, @parent, @record)
|
60
|
-
|
61
|
-
|
59
|
+
(record_by_href(item) || @record)&.new(data) || data
|
60
|
+
end
|
61
|
+
|
62
|
+
def record_by_href(item)
|
63
|
+
return if plain_value?(item) || item[:href].blank?
|
64
|
+
|
65
|
+
LHS::Record.for_url(item[:href])
|
66
|
+
end
|
67
|
+
|
68
|
+
def plain_value?(item)
|
69
|
+
item.is_a?(String) || item.is_a?(Numeric) || item.is_a?(TrueClass) || item.is_a?(FalseClass)
|
62
70
|
end
|
63
71
|
end
|
64
72
|
end
|
data/lib/lhs/version.rb
CHANGED
data/spec/record/compact_spec.rb
CHANGED
@@ -6,6 +6,11 @@ describe LHS::Record do
|
|
6
6
|
before do
|
7
7
|
class Place < LHS::Record
|
8
8
|
endpoint 'http://datastore/places/{id}'
|
9
|
+
endpoint 'http://datastore/users/{user_id}/places/{id}'
|
10
|
+
|
11
|
+
def display_name
|
12
|
+
"*#{name}*"
|
13
|
+
end
|
9
14
|
end
|
10
15
|
|
11
16
|
class User < LHS::Record
|
@@ -24,14 +29,24 @@ describe LHS::Record do
|
|
24
29
|
items: [
|
25
30
|
{
|
26
31
|
href: 'http://datastore/users/123/places/789'
|
32
|
+
}, {
|
33
|
+
href: 'http://datastore/users/123/places/790'
|
27
34
|
}
|
28
35
|
],
|
29
|
-
total:
|
36
|
+
total: 2,
|
30
37
|
offset: 0,
|
31
38
|
limit: 10
|
32
39
|
}.to_json)
|
33
40
|
|
34
41
|
stub_request(:get, 'http://datastore/users/123/places/789?limit=100')
|
42
|
+
.to_return(
|
43
|
+
body: {
|
44
|
+
href: 'http://datastore/users/123/places/789?limit=100',
|
45
|
+
name: 'Mc Donalds'
|
46
|
+
}.to_json
|
47
|
+
)
|
48
|
+
|
49
|
+
stub_request(:get, 'http://datastore/users/123/places/790?limit=100')
|
35
50
|
.to_return(
|
36
51
|
status: 404,
|
37
52
|
body: {
|
@@ -51,17 +66,25 @@ describe LHS::Record do
|
|
51
66
|
end
|
52
67
|
|
53
68
|
context '.compact' do
|
54
|
-
|
55
69
|
it 'removes linked resouces which could not get fetched' do
|
56
|
-
expect(places.compact.length).to eq
|
57
|
-
expect(places.length).not_to eq
|
70
|
+
expect(places.compact.length).to eq 1
|
71
|
+
expect(places.length).not_to eq 1 # leaves the original intact
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'record casting' do
|
76
|
+
let(:expected_display_name) { '*Mc Donalds*' }
|
77
|
+
|
78
|
+
it 'finds the right record class' do
|
79
|
+
expect(places.first.display_name).to eq expected_display_name
|
80
|
+
expect(places.compact.map(&:display_name)).to eq [expected_display_name]
|
58
81
|
end
|
59
82
|
end
|
60
83
|
|
61
84
|
context '.compact!' do
|
62
85
|
it 'removes linked resouces which could not get fetched' do
|
63
|
-
expect(places.compact!.length).to eq
|
64
|
-
expect(places.length).to eq
|
86
|
+
expect(places.compact!.length).to eq 1
|
87
|
+
expect(places.length).to eq 1 # and changes the original intact
|
65
88
|
end
|
66
89
|
end
|
67
90
|
end
|
@@ -8,6 +8,14 @@ describe LHS::Record do
|
|
8
8
|
class Customer < LHS::Record
|
9
9
|
endpoint 'http://datastore/customers/{id}'
|
10
10
|
end
|
11
|
+
|
12
|
+
class User < LHS::Record
|
13
|
+
endpoint 'http://datastore/users/{id}'
|
14
|
+
endpoint 'http://datastore/customers/{customer_id}/users/{id}'
|
15
|
+
def reversed_name
|
16
|
+
name.split.reverse.join(' ')
|
17
|
+
end
|
18
|
+
end
|
11
19
|
end
|
12
20
|
|
13
21
|
let!(:customer_request) do
|
@@ -18,6 +26,9 @@ describe LHS::Record do
|
|
18
26
|
},
|
19
27
|
'contact_addresses' => {
|
20
28
|
'href' => "http://datastore/contact_addresses"
|
29
|
+
},
|
30
|
+
'users' => {
|
31
|
+
'href' => 'http://datastore/customers/1/users'
|
21
32
|
}
|
22
33
|
}.to_json)
|
23
34
|
end
|
@@ -49,5 +60,37 @@ describe LHS::Record do
|
|
49
60
|
assert_requested(electronic_addresses_request)
|
50
61
|
assert_requested(contact_addresses_request)
|
51
62
|
end
|
63
|
+
|
64
|
+
describe 'mapping related classes correctly' do
|
65
|
+
before do
|
66
|
+
stub_request(:get, 'http://datastore/customers/1/users?limit=100').to_return(
|
67
|
+
status: 200,
|
68
|
+
body: {
|
69
|
+
href: 'http://datastore/customers/1/users?offset=0&limit=100',
|
70
|
+
items: [
|
71
|
+
{ href: 'http://datastore/customers/1/users/1' }
|
72
|
+
],
|
73
|
+
total: 1,
|
74
|
+
offset: 0,
|
75
|
+
limit: 10
|
76
|
+
}.to_json
|
77
|
+
)
|
78
|
+
|
79
|
+
stub_request(:get, 'http://datastore/customers/1/users/1?limit=100')
|
80
|
+
.with(headers: { 'Authentication' => 'Bearer 123' })
|
81
|
+
.to_return(body: { href: 'http://datastore/users/1', name: 'Elizabeth Baker' }.to_json)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'maps correctly' do
|
85
|
+
users = Customer
|
86
|
+
.includes(:users)
|
87
|
+
.references(users: referencing_options)
|
88
|
+
.find(1)
|
89
|
+
.users
|
90
|
+
|
91
|
+
expect(users.first.reversed_name).to be_present
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
52
95
|
end
|
53
96
|
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: 24.1.
|
4
|
+
version: 24.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhs/graphs/contributors
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-08-
|
11
|
+
date: 2020-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -552,7 +552,7 @@ homepage: https://github.com/local-ch/lhs
|
|
552
552
|
licenses:
|
553
553
|
- GPL-3.0
|
554
554
|
metadata: {}
|
555
|
-
post_install_message:
|
555
|
+
post_install_message:
|
556
556
|
rdoc_options: []
|
557
557
|
require_paths:
|
558
558
|
- lib
|
@@ -568,8 +568,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
568
568
|
version: '0'
|
569
569
|
requirements:
|
570
570
|
- Ruby >= 2.3.0
|
571
|
-
rubygems_version: 3.0.
|
572
|
-
signing_key:
|
571
|
+
rubygems_version: 3.0.3
|
572
|
+
signing_key:
|
573
573
|
specification_version: 4
|
574
574
|
summary: 'REST services accelerator: Rails gem providing an easy, active-record-like
|
575
575
|
interface for http (hypermedia) json services'
|