dhs 1.4.2 → 1.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 +44 -0
- data/lib/dhs/concerns/record/pagination.rb +2 -0
- data/lib/dhs/pagination/next_parameter.rb +32 -0
- data/lib/dhs/version.rb +1 -1
- data/lib/dhs.rb +1 -0
- data/spec/pagination/next_parameter_spec.rb +59 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c02b8006074cd3482fac996fcf904ed9e904d6ab8f8b504afcd9c27318dd5724
|
4
|
+
data.tar.gz: 2ce157d1fd6f0333d9638516e650f2ef040b6f08145664e4f3e8b041f908ca66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8975d2399f0a7a63066fdf8e23b96dbf2b9809bd9e75efc73a85779b1a32e97eae997dce4573222ed5d7b1556ca9dc2276d12eee5dddfa911d71698ae26b10d0
|
7
|
+
data.tar.gz: 9d00278274df289db27965f24a6bc2a106d46e64ea09814b8129d5aeae15f6e0e2d7e7cde37a7f7b49447142ecc52a960ea458bef1cb9f46396ec9457c2acd97
|
data/README.md
CHANGED
@@ -1431,6 +1431,50 @@ GET https://service.example.com/assets?offset=39
|
|
1431
1431
|
}
|
1432
1432
|
```
|
1433
1433
|
|
1434
|
+
##### Pagination strategy: next_parameter
|
1435
|
+
|
1436
|
+
The `next_parameter` strategy continuously follows in-response offset parameter information to following pages until the last page is reached (indicated by next parameter being empty or unset).
|
1437
|
+
|
1438
|
+
*WARNING*
|
1439
|
+
|
1440
|
+
Loading all pages from a resource paginated with next_parameter only can result in very poor performance, as pages can only be loaded sequentially!
|
1441
|
+
|
1442
|
+
```ruby
|
1443
|
+
# app/models/record.rb
|
1444
|
+
|
1445
|
+
class Search < DHS::Record
|
1446
|
+
configuration pagination_strategy: 'next_parameter'
|
1447
|
+
|
1448
|
+
endpoint '{+service}/assets'
|
1449
|
+
end
|
1450
|
+
```
|
1451
|
+
|
1452
|
+
```ruby
|
1453
|
+
# app/controllers/some_controller.rb
|
1454
|
+
|
1455
|
+
Record.all
|
1456
|
+
|
1457
|
+
```
|
1458
|
+
```
|
1459
|
+
GET https://service.example.com/assets?limit=100
|
1460
|
+
{
|
1461
|
+
items: [{...}, ...],
|
1462
|
+
limit: 10,
|
1463
|
+
next: "LXBrPTIzMzE4NDE0"
|
1464
|
+
}
|
1465
|
+
GET https://service.example.com/assets?next=LXBrPTIzMzE4NDE0
|
1466
|
+
{
|
1467
|
+
items: [{...}, ...],
|
1468
|
+
limit: 10,
|
1469
|
+
next: "ASDASdads9123"
|
1470
|
+
}
|
1471
|
+
GET https://service.example.com/assets?next=ASDASdads9123
|
1472
|
+
{
|
1473
|
+
items: [{...}, ...],
|
1474
|
+
limit: 10
|
1475
|
+
}
|
1476
|
+
```
|
1477
|
+
|
1434
1478
|
#### Pagination keys
|
1435
1479
|
|
1436
1480
|
##### limit_key
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class DHS::Pagination::NextParameter < DHS::Pagination::Base
|
4
|
+
|
5
|
+
DEFAULT_OFFSET = nil
|
6
|
+
|
7
|
+
def total
|
8
|
+
data._raw.dig(*_record.items_key).count || 0
|
9
|
+
end
|
10
|
+
alias count total
|
11
|
+
|
12
|
+
def self.next_offset(offset, limit)
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def parallel?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
def pages_left?
|
21
|
+
next_offset = data._raw.dig(*_record.pagination_key(:body))
|
22
|
+
next_offset.present? && !next_offset.blank?
|
23
|
+
end
|
24
|
+
|
25
|
+
def next(current)
|
26
|
+
next_value = current.dig(*_record.pagination_key(:body))
|
27
|
+
return if next_value.blank? || next_value.blank?
|
28
|
+
{
|
29
|
+
_record.pagination_key(:parameter) => current.dig(*_record.pagination_key(:body))
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|
data/lib/dhs/version.rb
CHANGED
data/lib/dhs.rb
CHANGED
@@ -40,6 +40,7 @@ module DHS
|
|
40
40
|
autoload :TotalPages, 'dhs/pagination/total_pages'
|
41
41
|
autoload :OffsetPage, 'dhs/pagination/offset_page'
|
42
42
|
autoload :NextOffset, 'dhs/pagination/next_offset'
|
43
|
+
autoload :NextParameter, 'dhs/pagination/next_parameter'
|
43
44
|
autoload :Start, 'dhs/pagination/start'
|
44
45
|
autoload :Link, 'dhs/pagination/link'
|
45
46
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
describe DHS::Record do
|
6
|
+
context 'next parameter pagination' do
|
7
|
+
|
8
|
+
def stub_api_request(this_parameter:, next_parameter:, items: [])
|
9
|
+
stub_request(:get, ["http://depay.fi/v2/transactions?limit=100", this_parameter ? "next=#{this_parameter}" : nil].compact.join('&'))
|
10
|
+
.to_return(body: { items: items, next: next_parameter }.to_json)
|
11
|
+
end
|
12
|
+
|
13
|
+
let!(:requests) do
|
14
|
+
stub_api_request(items: (0...100).to_a, this_parameter: nil, next_parameter: 'NEXT1')
|
15
|
+
stub_api_request(items: (100...200).to_a, this_parameter: 'NEXT1', next_parameter: 'NEXT2')
|
16
|
+
stub_api_request(items: nil, this_parameter: 'NEXT2', next_parameter: nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
before do
|
20
|
+
class Transaction < DHS::Record
|
21
|
+
configuration pagination_strategy: :next_parameter, pagination_key: { body: :next, parameter: :next }
|
22
|
+
|
23
|
+
endpoint 'http://depay.fi/v2/transactions'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'fetches all the pages' do
|
28
|
+
transactions = Transaction.all.fetch
|
29
|
+
expect(transactions.to_a).to eq (0...200).to_a
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'next parameter pagination with items configuration and nil response' do
|
34
|
+
|
35
|
+
def stub_api_request(this_parameter:, next_parameter:, items: [])
|
36
|
+
stub_request(:get, ["http://depay.fi/v2/transactions?limit=100", this_parameter ? "next=#{this_parameter}" : nil].compact.join('&'))
|
37
|
+
.to_return(body: { assets: items, next: next_parameter }.to_json)
|
38
|
+
end
|
39
|
+
|
40
|
+
let!(:requests) do
|
41
|
+
stub_api_request(items: (0...100).to_a, this_parameter: nil, next_parameter: 'NEXT1')
|
42
|
+
stub_api_request(items: (100...200).to_a, this_parameter: 'NEXT1', next_parameter: 'NEXT2')
|
43
|
+
stub_api_request(items: nil, this_parameter: 'NEXT2', next_parameter: nil)
|
44
|
+
end
|
45
|
+
|
46
|
+
before do
|
47
|
+
class Transaction < DHS::Record
|
48
|
+
configuration items_key: :assets, pagination_strategy: :next_parameter, pagination_key: { body: :next, parameter: :next }
|
49
|
+
|
50
|
+
endpoint 'http://depay.fi/v2/transactions'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'fetches and merges all the assets/items' do
|
55
|
+
transactions = Transaction.all.fetch
|
56
|
+
expect(transactions.to_a).to eq (0...200).to_a
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dhs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/DePayFi/dhs/graphs/contributors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -328,6 +328,7 @@ files:
|
|
328
328
|
- lib/dhs/pagination/base.rb
|
329
329
|
- lib/dhs/pagination/link.rb
|
330
330
|
- lib/dhs/pagination/next_offset.rb
|
331
|
+
- lib/dhs/pagination/next_parameter.rb
|
331
332
|
- lib/dhs/pagination/offset.rb
|
332
333
|
- lib/dhs/pagination/offset_page.rb
|
333
334
|
- lib/dhs/pagination/page.rb
|
@@ -468,6 +469,7 @@ files:
|
|
468
469
|
- spec/pagination/link/parallel_spec.rb
|
469
470
|
- spec/pagination/link/total_spec.rb
|
470
471
|
- spec/pagination/next_offset_spec.rb
|
472
|
+
- spec/pagination/next_parameter_spec.rb
|
471
473
|
- spec/pagination/offset/pages_left_spec.rb
|
472
474
|
- spec/pagination/offset_page_spec.rb
|
473
475
|
- spec/pagination/parameters_spec.rb
|
@@ -706,6 +708,7 @@ test_files:
|
|
706
708
|
- spec/pagination/link/parallel_spec.rb
|
707
709
|
- spec/pagination/link/total_spec.rb
|
708
710
|
- spec/pagination/next_offset_spec.rb
|
711
|
+
- spec/pagination/next_parameter_spec.rb
|
709
712
|
- spec/pagination/offset/pages_left_spec.rb
|
710
713
|
- spec/pagination/offset_page_spec.rb
|
711
714
|
- spec/pagination/parameters_spec.rb
|