lhs 9.0.2 → 9.0.3
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/complex.rb +1 -1
- data/lib/lhs/concerns/record/request.rb +30 -13
- data/lib/lhs/version.rb +1 -1
- data/spec/complex/reduce_spec.rb +6 -0
- data/spec/record/includes_all_spec.rb +31 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a94202476f1970242cdb7215def030000b3ee3b9
|
4
|
+
data.tar.gz: 074bb140334dcd7e09f1fdb93896edea6935c72c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d300ee0903b0c74999cb3b8cccc46bda9efdc2005ac18e9bddb01ec64e274063f7d12782b36578fca4ef97f38cbbf06640bc46f92a95326e3c421ba553c9634
|
7
|
+
data.tar.gz: 0556539c40153065c023cb49638d4f799391e814abd8d3150fdebad5a952df3a1c6ad7d1f9f174db159f489288840a07286ad86c1a815f3f2ba6a44f9e53c80b
|
data/lib/lhs/complex.rb
CHANGED
@@ -9,7 +9,8 @@ class LHS::Record
|
|
9
9
|
def request(options)
|
10
10
|
options ||= {}
|
11
11
|
options = options.deep_dup
|
12
|
-
if options.is_a?
|
12
|
+
if options.is_a?(Array)
|
13
|
+
filter_request_options!(options)
|
13
14
|
multiple_requests(options)
|
14
15
|
else
|
15
16
|
single_request(options)
|
@@ -18,6 +19,13 @@ class LHS::Record
|
|
18
19
|
|
19
20
|
private
|
20
21
|
|
22
|
+
def filter_request_options!(options)
|
23
|
+
options.each_with_index do |option, index|
|
24
|
+
next if !option || !option.key?(:url) || !option[:url].nil?
|
25
|
+
options[index] = nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
21
29
|
# Applies limit to the first request of an all request chain
|
22
30
|
# Tries to apply an high value for limit and reacts on the limit
|
23
31
|
# returned by the endpoint to make further requests
|
@@ -72,7 +80,7 @@ class LHS::Record
|
|
72
80
|
|
73
81
|
def extend_base_array!(data, addition, key)
|
74
82
|
data[key].zip(addition) do |item, additional_item|
|
75
|
-
item._raw.merge!(additional_item._raw)
|
83
|
+
item._raw.merge!(additional_item._raw) if additional_item.present?
|
76
84
|
end
|
77
85
|
end
|
78
86
|
|
@@ -159,8 +167,8 @@ class LHS::Record
|
|
159
167
|
return options unless references
|
160
168
|
options ||= {}
|
161
169
|
if options.is_a?(Array)
|
162
|
-
options.map { |request_options| request_options.merge(references) }
|
163
|
-
|
170
|
+
options.map { |request_options| request_options.merge(references) if request_options.present? }
|
171
|
+
elsif options.present?
|
164
172
|
options.merge(references)
|
165
173
|
end
|
166
174
|
end
|
@@ -216,20 +224,28 @@ class LHS::Record
|
|
216
224
|
begin
|
217
225
|
prepare_options_for_include_request!(options, sub_includes, references)
|
218
226
|
if references && references[:all] # include all linked resources
|
219
|
-
|
220
|
-
data = load_all_included!(record, options)
|
221
|
-
references.delete(:all) # for this all remote objects have been fetched
|
222
|
-
continue_including(data, sub_includes, references)
|
227
|
+
load_include_all!(options, record, sub_includes, references)
|
223
228
|
else # simply request first page/batch
|
224
|
-
|
225
|
-
warn "[WARNING] You included `#{options[:url]}`, but this endpoint is paginated. You might want to use `includes_all` instead of `includes` (https://github.com/local-ch/lhs#includes_all-for-paginated-endpoints)." if paginated?(data._raw)
|
226
|
-
data
|
229
|
+
load_include_simple!(options, record)
|
227
230
|
end
|
228
231
|
rescue LHC::NotFound
|
229
232
|
LHS::Data.new({}, data, record)
|
230
233
|
end
|
231
234
|
end
|
232
235
|
|
236
|
+
def load_include_all!(options, record, sub_includes, references)
|
237
|
+
prepare_options_for_include_all_request!(options)
|
238
|
+
data = load_all_included!(record, options)
|
239
|
+
references.delete(:all) # for this reference all remote objects have been fetched
|
240
|
+
continue_including(data, sub_includes, references)
|
241
|
+
end
|
242
|
+
|
243
|
+
def load_include_simple!(options, record)
|
244
|
+
data = record.request(options)
|
245
|
+
warn "[WARNING] You included `#{options[:url]}`, but this endpoint is paginated. You might want to use `includes_all` instead of `includes` (https://github.com/local-ch/lhs#includes_all-for-paginated-endpoints)." if paginated?(data._raw)
|
246
|
+
data
|
247
|
+
end
|
248
|
+
|
233
249
|
# Continues loading included resources after one complete batch/level has been fetched
|
234
250
|
def continue_including(data, including, referencing)
|
235
251
|
handle_includes(including, data, referencing) if including.present? && data.present?
|
@@ -263,6 +279,7 @@ class LHS::Record
|
|
263
279
|
# When including all resources on one level, don't forward :includes & :references
|
264
280
|
# as we have to fetch all resources on this level first, before we continue_including
|
265
281
|
def prepare_option_for_include_all_request!(option)
|
282
|
+
return option unless option.present?
|
266
283
|
option[:params] ||= {}
|
267
284
|
option[:params].merge!(limit_key => option.fetch(:params, {}).fetch(limit_key, LHS::Pagination::Base::DEFAULT_LIMIT))
|
268
285
|
option.delete(:including)
|
@@ -302,8 +319,8 @@ class LHS::Record
|
|
302
319
|
data = LHC.request(options.compact).map do |response|
|
303
320
|
LHS::Data.new(response.body, nil, self, response.request)
|
304
321
|
end
|
305
|
-
including = LHS::Complex.reduce
|
306
|
-
referencing = LHS::Complex.reduce
|
322
|
+
including = LHS::Complex.reduce(options.compact.map { |options| options.delete(:including) }.compact)
|
323
|
+
referencing = LHS::Complex.reduce(options.compact.map { |options| options.delete(:referencing) }.compact)
|
307
324
|
data = restore_with_nils(data, locate_nils(options)) # nil objects in data provide location information for mapping
|
308
325
|
data = LHS::Data.new(data, nil, self)
|
309
326
|
handle_includes(including, data, referencing) if including.present? && data.present?
|
data/lib/lhs/version.rb
CHANGED
data/spec/complex/reduce_spec.rb
CHANGED
@@ -15,6 +15,12 @@ describe LHS::Complex do
|
|
15
15
|
}.to raise_error(ArgumentError)
|
16
16
|
end
|
17
17
|
|
18
|
+
it 'does not fail trying to merge primitive types when they are the same' do
|
19
|
+
expect {
|
20
|
+
LHS::Complex.reduce([{ entry: true }, { entry: true }])
|
21
|
+
}.not_to raise_error
|
22
|
+
end
|
23
|
+
|
18
24
|
context 'first level' do
|
19
25
|
context 'reduces symbols into/with X' do
|
20
26
|
it 'reduces symbols into hash' do
|
@@ -121,19 +121,41 @@ describe LHS::Record do
|
|
121
121
|
class Contract < LHS::Record
|
122
122
|
endpoint 'http://datastore/contracts/:id'
|
123
123
|
end
|
124
|
-
stub_request(:get,
|
124
|
+
stub_request(:get, %r{http://datastore/contracts/\d})
|
125
125
|
.to_return(body: {
|
126
|
-
options:
|
126
|
+
options: nested_resources
|
127
127
|
}.to_json)
|
128
128
|
end
|
129
129
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
.find(1)
|
136
|
-
|
130
|
+
context 'empty array' do
|
131
|
+
let(:nested_resources) { [] }
|
132
|
+
|
133
|
+
it 'includes_all in case of an empty array' do
|
134
|
+
expect(
|
135
|
+
-> { Contract.includes(:product).includes_all(:options).find(1) }
|
136
|
+
).not_to raise_error
|
137
|
+
expect(
|
138
|
+
-> { Contract.includes(:product).includes_all(:options).find(1, 2) }
|
139
|
+
).not_to raise_error
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'weird array without hrefs' do
|
144
|
+
before(:each) do
|
145
|
+
stub_request(:get, "http://datastore/options/1?limit=100")
|
146
|
+
.to_return(body: { type: 'REACH_EXT' }.to_json)
|
147
|
+
end
|
148
|
+
|
149
|
+
let(:nested_resources) { [{ href: 'http://datastore/options/1' }, { type: 'E_COMMERCE' }] }
|
150
|
+
|
151
|
+
it 'includes_all in case of an unexpect objects within array' do
|
152
|
+
expect(
|
153
|
+
-> { Contract.includes(:product).includes_all(:options).find(1) }
|
154
|
+
).not_to raise_error
|
155
|
+
expect(
|
156
|
+
-> { Contract.includes(:product).includes_all(:options).find(1, 2) }
|
157
|
+
).not_to raise_error
|
158
|
+
end
|
137
159
|
end
|
138
160
|
end
|
139
161
|
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
|
+
version: 9.0.3
|
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-
|
11
|
+
date: 2017-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lhc
|