lhs 14.4.0 → 14.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/cider-ci/bin/bundle +3 -0
- data/lhs.gemspec +1 -1
- data/lib/lhs/concerns/record/batch.rb +3 -3
- data/lib/lhs/concerns/record/chainable.rb +2 -2
- data/lib/lhs/concerns/record/configuration.rb +11 -4
- data/lib/lhs/concerns/record/request.rb +26 -14
- data/lib/lhs/pagination/base.rb +2 -2
- data/lib/lhs/version.rb +1 -1
- data/spec/pagination/parameters_spec.rb +59 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66878335134271a8a2adadfb4a3318d029f0a854
|
4
|
+
data.tar.gz: c579b0f2377f06413b247d58023f10ed1bb908b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48df46b051c333e615122cf5fda68298b2bf3f4116631f93a987408c8942e7e295e52b4d24830861867f099bc74f473e28bd6a9c1c7ae16c43007d7be81f9c31
|
7
|
+
data.tar.gz: f138b1a4a1d25b7b43de711b7bdf4038a13330cd34c94784123234cf9b0be62f25c9cd2c02593916f111542bb1c9c24b70c60d356be90fed82a87a3a5d908f1e
|
data/README.md
CHANGED
@@ -997,8 +997,20 @@ end
|
|
997
997
|
|
998
998
|
`limit_key` key used to work with page limits (e.g. `size`, `limit`, etc.)
|
999
999
|
|
1000
|
+
In case the `limit_key` parameter differs for where it's located in the body and how it's provided as get parameter, when retreiving pages, provide a hash with `body` and `paramter` key, to keep those two use cases separated:
|
1001
|
+
|
1002
|
+
```ruby
|
1003
|
+
configuration limit_key: { body: [:response, :max], parameter: :max }
|
1004
|
+
```
|
1005
|
+
|
1000
1006
|
`pagination_key` key used to paginate multiple pages (e.g. `offset`, `page`, `startAt` etc.).
|
1001
1007
|
|
1008
|
+
In case the `pagination_key` parameter differs for where it's located in the body and how it's provided as get parameter, when retreiving pages, provide a hash with `body` and `paramter` key, to keep those two use cases separated:
|
1009
|
+
|
1010
|
+
```ruby
|
1011
|
+
configuration pagination_key: { body: [:response, :page], parameter: :page }
|
1012
|
+
```
|
1013
|
+
|
1002
1014
|
`pagination_strategy` used to configure the strategy used for navigating (e.g. `offset`, `page`, `start`, etc.).
|
1003
1015
|
|
1004
1016
|
`total_key` key used to determine the total amount of items (e.g. `total`, `totalResults`, etc.).
|
data/cider-ci/bin/bundle
CHANGED
data/lhs.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.add_dependency 'activesupport', '> 4.2'
|
25
25
|
s.add_dependency 'activemodel'
|
26
26
|
|
27
|
-
s.add_development_dependency 'rspec-rails', '>= 3.
|
27
|
+
s.add_development_dependency 'rspec-rails', '>= 3.7.0'
|
28
28
|
s.add_development_dependency 'rails', '>= 4.0.0'
|
29
29
|
s.add_development_dependency 'webmock'
|
30
30
|
s.add_development_dependency 'pry'
|
@@ -23,9 +23,9 @@ class LHS::Record
|
|
23
23
|
batch_size = options[:batch_size] || LHS::Pagination::Base::DEFAULT_LIMIT
|
24
24
|
params = options[:params] || {}
|
25
25
|
loop do # as suggested by Matz
|
26
|
-
data = request(params: params.merge(limit_key => batch_size, pagination_key => start))
|
27
|
-
batch_size = data._raw[limit_key]
|
28
|
-
left = data._raw
|
26
|
+
data = request(params: params.merge(limit_key(:parameter) => batch_size, pagination_key(:parameter) => start))
|
27
|
+
batch_size = data._raw[limit_key(:parameter)]
|
28
|
+
left = data._raw.dig(*total_key).to_i - data._raw[pagination_key(:parameter)].to_i - data._raw[limit_key(:paramter)].to_i
|
29
29
|
yield new(data)
|
30
30
|
break if left <= 0
|
31
31
|
start += batch_size
|
@@ -418,8 +418,8 @@ class LHS::Record
|
|
418
418
|
end
|
419
419
|
pagination = @record_class.pagination_class
|
420
420
|
{
|
421
|
-
@record_class.pagination_key => pagination.page_to_offset(page, per),
|
422
|
-
@record_class.limit_key => per
|
421
|
+
@record_class.pagination_key(:parameter) => pagination.page_to_offset(page, per),
|
422
|
+
@record_class.limit_key(:parameter) => per
|
423
423
|
}
|
424
424
|
end
|
425
425
|
|
@@ -32,9 +32,10 @@ class LHS::Record
|
|
32
32
|
)
|
33
33
|
end
|
34
34
|
|
35
|
-
def limit_key
|
35
|
+
def limit_key(type = nil)
|
36
36
|
symbolize_unless_complex(
|
37
|
-
@configuration.try(:[], :limit_key) ||
|
37
|
+
pagination_parameter(@configuration.try(:[], :limit_key), type) ||
|
38
|
+
:limit
|
38
39
|
)
|
39
40
|
end
|
40
41
|
|
@@ -45,9 +46,10 @@ class LHS::Record
|
|
45
46
|
end
|
46
47
|
|
47
48
|
# Key used for determine current page
|
48
|
-
def pagination_key
|
49
|
+
def pagination_key(type = nil)
|
49
50
|
symbolize_unless_complex(
|
50
|
-
@configuration.try(:[], :pagination_key) ||
|
51
|
+
pagination_parameter(@configuration.try(:[], :pagination_key), type) ||
|
52
|
+
:offset
|
51
53
|
)
|
52
54
|
end
|
53
55
|
|
@@ -65,6 +67,11 @@ class LHS::Record
|
|
65
67
|
return value.to_sym unless value.is_a?(Array)
|
66
68
|
value
|
67
69
|
end
|
70
|
+
|
71
|
+
def pagination_parameter(configuration, type)
|
72
|
+
return configuration unless configuration.is_a?(Hash)
|
73
|
+
configuration[type]
|
74
|
+
end
|
68
75
|
end
|
69
76
|
end
|
70
77
|
end
|
@@ -41,7 +41,7 @@ class LHS::Record
|
|
41
41
|
# returned by the endpoint to make further requests
|
42
42
|
def apply_limit!(options)
|
43
43
|
options[:params] ||= {}
|
44
|
-
options[:params] = options[:params].merge(limit_key => options[:params][limit_key] || LHS::Pagination::Base::DEFAULT_LIMIT)
|
44
|
+
options[:params] = options[:params].merge(limit_key(:parameter) => options[:params][limit_key(:parameter)] || LHS::Pagination::Base::DEFAULT_LIMIT)
|
45
45
|
end
|
46
46
|
|
47
47
|
# Convert URLs in options to endpoint templates
|
@@ -230,11 +230,11 @@ class LHS::Record
|
|
230
230
|
def load_and_merge_not_paginated_collection!(data, options)
|
231
231
|
return if data.length.zero?
|
232
232
|
options = options.is_a?(Hash) ? options : {}
|
233
|
-
limit = options.dig(:params, limit_key) || pagination_class::DEFAULT_LIMIT
|
234
|
-
offset = options.dig(:params, pagination_key) || pagination_class::DEFAULT_OFFSET
|
233
|
+
limit = options.dig(:params, limit_key(:parameter)) || pagination_class::DEFAULT_LIMIT
|
234
|
+
offset = options.dig(:params, pagination_key(:parameter)) || pagination_class::DEFAULT_OFFSET
|
235
235
|
options[:params] = options.fetch(:params, {}).merge(
|
236
|
-
limit_key => limit,
|
237
|
-
pagination_key => pagination_class.next_offset(
|
236
|
+
limit_key(:parameter) => limit,
|
237
|
+
pagination_key(:parameter) => pagination_class.next_offset(
|
238
238
|
offset,
|
239
239
|
limit
|
240
240
|
)
|
@@ -245,8 +245,16 @@ class LHS::Record
|
|
245
245
|
end
|
246
246
|
end
|
247
247
|
|
248
|
+
# sets nested data for a source object that needs to be accessed with a given path e.g. [:response, :total]
|
249
|
+
def set_nested_data(source, path, value)
|
250
|
+
return source[path] = value unless path.is_a?(Array)
|
251
|
+
path = path.dup
|
252
|
+
last = path.pop
|
253
|
+
path.inject(source, :fetch)[last] = value
|
254
|
+
end
|
255
|
+
|
248
256
|
def load_and_merge_paginated_collection!(data, options)
|
249
|
-
data._raw
|
257
|
+
set_nested_data(data._raw, limit_key(:body), data.length) if data._raw.dig(*limit_key(:body)).blank? && !data.length.zero?
|
250
258
|
pagination = data._record.pagination(data)
|
251
259
|
return data if pagination.pages_left.zero?
|
252
260
|
record = data._record
|
@@ -315,7 +323,7 @@ class LHS::Record
|
|
315
323
|
|
316
324
|
# Checks if given raw is paginated or not
|
317
325
|
def paginated?(raw)
|
318
|
-
!!(raw.is_a?(Hash) && raw
|
326
|
+
!!(raw.is_a?(Hash) && raw.dig(*total_key))
|
319
327
|
end
|
320
328
|
|
321
329
|
def prepare_options_for_include_all_request!(options)
|
@@ -336,10 +344,10 @@ class LHS::Record
|
|
336
344
|
uri = parse_uri(option[:url], option)
|
337
345
|
get_params = Rack::Utils.parse_nested_query(uri.query)
|
338
346
|
.symbolize_keys
|
339
|
-
.except(limit_key, pagination_key)
|
347
|
+
.except(limit_key(:parameter), pagination_key(:parameter))
|
340
348
|
option[:params] ||= {}
|
341
349
|
option[:params].reverse_merge!(get_params)
|
342
|
-
option[:params][limit_key] ||= LHS::Pagination::Base::DEFAULT_LIMIT
|
350
|
+
option[:params][limit_key(:parameter)] ||= LHS::Pagination::Base::DEFAULT_LIMIT
|
343
351
|
option[:url] = option[:url].gsub("?#{uri.query}", '')
|
344
352
|
option.delete(:including)
|
345
353
|
option.delete(:referencing)
|
@@ -357,9 +365,13 @@ class LHS::Record
|
|
357
365
|
|
358
366
|
def merge_batch_data_with_parent!(batch_data, parent_data)
|
359
367
|
parent_data.concat(input: parent_data._raw, items: batch_data.raw_items, record: self)
|
360
|
-
|
361
|
-
|
362
|
-
|
368
|
+
[limit_key(:body), total_key, pagination_key(:body)].each do |pagination_attribute|
|
369
|
+
set_nested_data(
|
370
|
+
parent_data._raw,
|
371
|
+
pagination_attribute,
|
372
|
+
batch_data._raw.dig(*pagination_attribute)
|
373
|
+
)
|
374
|
+
end
|
363
375
|
end
|
364
376
|
|
365
377
|
# Merge explicit params nested in 'params' namespace with original hash.
|
@@ -415,8 +427,8 @@ class LHS::Record
|
|
415
427
|
pagination.pages_left.times do |index|
|
416
428
|
page_options = {
|
417
429
|
params: {
|
418
|
-
record.limit_key => pagination.limit,
|
419
|
-
record.pagination_key => pagination.next_offset(index + 1)
|
430
|
+
record.limit_key(:parameter) => pagination.limit,
|
431
|
+
record.pagination_key(:parameter) => pagination.next_offset(index + 1)
|
420
432
|
}
|
421
433
|
}
|
422
434
|
page_options[:parent_data] = parent_data if parent_data
|
data/lib/lhs/pagination/base.rb
CHANGED
@@ -19,11 +19,11 @@ module LHS::Pagination
|
|
19
19
|
alias count total
|
20
20
|
|
21
21
|
def limit
|
22
|
-
data._raw.dig(*_record.limit_key) || DEFAULT_LIMIT
|
22
|
+
data._raw.dig(*_record.limit_key(:body)) || DEFAULT_LIMIT
|
23
23
|
end
|
24
24
|
|
25
25
|
def offset
|
26
|
-
data._raw.dig(*_record.pagination_key) || self.class::DEFAULT_OFFSET
|
26
|
+
data._raw.dig(*_record.pagination_key(:body)) || self.class::DEFAULT_OFFSET
|
27
27
|
end
|
28
28
|
alias current_page offset
|
29
29
|
alias start offset
|
data/lib/lhs/version.rb
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
require 'webrick'
|
3
|
+
|
4
|
+
describe LHS::Record do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
class Location < LHS::Record
|
8
|
+
endpoint 'http://uberall/locations'
|
9
|
+
configuration(
|
10
|
+
limit_key: { body: %i[response max], parameter: :max },
|
11
|
+
pagination_key: { body: %i[response offset], parameter: :offset },
|
12
|
+
total_key: %i[response count],
|
13
|
+
items_key: %i[response locations],
|
14
|
+
pagination_strategy: :offset
|
15
|
+
)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'explicit pagination paramters for retreiving pages' do
|
20
|
+
|
21
|
+
it 'uses explicit parameters when retreiving pages' do
|
22
|
+
stub_request(:get, "http://uberall/locations?max=100")
|
23
|
+
.to_return(body: {
|
24
|
+
response: {
|
25
|
+
locations: 10.times.map { |_| { name: WEBrick::Utils.random_string(10) } },
|
26
|
+
max: 10,
|
27
|
+
offset: 0,
|
28
|
+
count: 30
|
29
|
+
}
|
30
|
+
}.to_json)
|
31
|
+
|
32
|
+
stub_request(:get, "http://uberall/locations?max=10&offset=10")
|
33
|
+
.to_return(body: {
|
34
|
+
response: {
|
35
|
+
locations: 10.times.map { |_| { name: WEBrick::Utils.random_string(10) } },
|
36
|
+
max: 10,
|
37
|
+
offset: 10,
|
38
|
+
count: 30
|
39
|
+
}
|
40
|
+
}.to_json)
|
41
|
+
|
42
|
+
stub_request(:get, "http://uberall/locations?max=10&offset=20")
|
43
|
+
.to_return(body: {
|
44
|
+
response: {
|
45
|
+
locations: 10.times.map { |_| { name: WEBrick::Utils.random_string(10) } },
|
46
|
+
max: 10,
|
47
|
+
offset: 20,
|
48
|
+
count: 30
|
49
|
+
}
|
50
|
+
}.to_json)
|
51
|
+
|
52
|
+
locations = Location.all.fetch
|
53
|
+
expect(locations.length).to eq 30
|
54
|
+
expect(locations.count).to eq 30
|
55
|
+
expect(locations.offset).to eq 20
|
56
|
+
expect(locations.limit).to eq 10
|
57
|
+
end
|
58
|
+
end
|
59
|
+
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.
|
4
|
+
version: 14.5.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-10-
|
11
|
+
date: 2017-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lhc
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 3.
|
61
|
+
version: 3.7.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 3.
|
68
|
+
version: 3.7.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rails
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -344,6 +344,7 @@ files:
|
|
344
344
|
- spec/item/warning_codes_spec.rb
|
345
345
|
- spec/item/warnings_spec.rb
|
346
346
|
- spec/pagination/pages_left_spec.rb
|
347
|
+
- spec/pagination/parameters_spec.rb
|
347
348
|
- spec/proxy/create_sub_resource_spec.rb
|
348
349
|
- spec/proxy/load_spec.rb
|
349
350
|
- spec/proxy/record_identification_spec.rb
|
@@ -526,6 +527,7 @@ test_files:
|
|
526
527
|
- spec/item/warning_codes_spec.rb
|
527
528
|
- spec/item/warnings_spec.rb
|
528
529
|
- spec/pagination/pages_left_spec.rb
|
530
|
+
- spec/pagination/parameters_spec.rb
|
529
531
|
- spec/proxy/create_sub_resource_spec.rb
|
530
532
|
- spec/proxy/load_spec.rb
|
531
533
|
- spec/proxy/record_identification_spec.rb
|