dhs 1.4.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 690186fffb1221bbc432b5a502bdfecb895b1f117e42602e6bef6b2c55cd7bd1
4
- data.tar.gz: 7f544c63db8a4db092a6c3980b3cb2f437879876db145a6ac7d8c6cfc7647a05
3
+ metadata.gz: c02b8006074cd3482fac996fcf904ed9e904d6ab8f8b504afcd9c27318dd5724
4
+ data.tar.gz: 2ce157d1fd6f0333d9638516e650f2ef040b6f08145664e4f3e8b041f908ca66
5
5
  SHA512:
6
- metadata.gz: '087dacf286f24b6860419759c5ea7cd443d69e2cd8f4900b2ee17fdf93d2d9f55918534ec0aff8294e6a4906c2dad51d3c4eb2b6ad8b3f19111a4133ed15f6c5'
7
- data.tar.gz: f14b0dad8dffb72c65cd6b785469a59c590a99187639e554865e99bcf8e696434f262c4e95d64b1241bfd997180ef278ce2f0f0f5b91c6e5f11a673466f7eff8
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
@@ -32,6 +32,8 @@ class DHS::Record
32
32
  DHS::Pagination::Link
33
33
  when :next_offset
34
34
  DHS::Pagination::NextOffset
35
+ when :next_parameter
36
+ DHS::Pagination::NextParameter
35
37
  else
36
38
  DHS::Pagination::Offset
37
39
  end
@@ -387,6 +387,7 @@ class DHS::Record
387
387
  end
388
388
 
389
389
  def merge_batch_data_with_parent!(batch_data, parent_data, pagination = nil)
390
+ return if batch_data.raw_items.blank?
390
391
  parent_data.concat(input: parent_data._raw, items: batch_data.raw_items, record: self)
391
392
  return if pagination.present? && pagination.is_a?(DHS::Pagination::Link)
392
393
  [limit_key(:body), total_key, pagination_key(:body)].each do |pagination_attribute|
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DHS
4
- VERSION = '1.4.1'
4
+ VERSION = '1.5.0'
5
5
  end
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
@@ -3,7 +3,7 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  describe DHS::Record do
6
- context 'pagination' do
6
+ context 'next offset pagination' do
7
7
  def stub_api_request(next_offset:, items: [], offset: nil)
8
8
  stub_request(:get, ['http://depay.fi/v2/transactions?limit=100', offset ? "offset=#{offset}" : nil].compact.join('&'))
9
9
  .to_return(body: { items: items, next_offset: next_offset }.to_json)
@@ -28,4 +28,56 @@ describe DHS::Record do
28
28
  expect(transactions.to_a).to eq (0...300).to_a
29
29
  end
30
30
  end
31
+
32
+ context 'next offset pagination with items configuration' do
33
+ def stub_api_request(next_offset:, items: [], offset: nil)
34
+ stub_request(:get, ['http://depay.fi/v2/transactions?limit=100', offset ? "offset=#{offset}" : nil].compact.join('&'))
35
+ .to_return(body: { assets: items, next_offset: next_offset }.to_json)
36
+ end
37
+
38
+ let!(:requests) do
39
+ stub_api_request(items: (0...100).to_a, next_offset: 99)
40
+ stub_api_request(items: (100...200).to_a, offset: 99, next_offset: 199)
41
+ stub_api_request(items: (200...300).to_a, offset: 199, next_offset: 0)
42
+ end
43
+
44
+ before do
45
+ class Transaction < DHS::Record
46
+ configuration items_key: :assets, pagination_strategy: :next_offset, pagination_key: { body: :next_offset, parameter: :offset }
47
+
48
+ endpoint 'http://depay.fi/v2/transactions'
49
+ end
50
+ end
51
+
52
+ it 'fetches and merges all the assets/items' do
53
+ transactions = Transaction.all.fetch
54
+ expect(transactions.to_a).to eq (0...300).to_a
55
+ end
56
+ end
57
+
58
+ context 'next offset pagination with items configuration and nil response' do
59
+ def stub_api_request(next_offset:, items: [], offset: nil)
60
+ stub_request(:get, ['http://depay.fi/v2/transactions?limit=100', offset ? "offset=#{offset}" : nil].compact.join('&'))
61
+ .to_return(body: { assets: items, next_offset: next_offset }.to_json)
62
+ end
63
+
64
+ let!(:requests) do
65
+ stub_api_request(items: (0...100).to_a, next_offset: 99)
66
+ stub_api_request(items: (100...200).to_a, offset: 99, next_offset: 199)
67
+ stub_api_request(items: nil, offset: 199, next_offset: 0)
68
+ end
69
+
70
+ before do
71
+ class Transaction < DHS::Record
72
+ configuration items_key: :assets, pagination_strategy: :next_offset, pagination_key: { body: :next_offset, parameter: :offset }
73
+
74
+ endpoint 'http://depay.fi/v2/transactions'
75
+ end
76
+ end
77
+
78
+ it 'fetches and merges all the assets/items' do
79
+ transactions = Transaction.all.fetch
80
+ expect(transactions.to_a).to eq (0...200).to_a
81
+ end
82
+ end
31
83
  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.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-04-25 00:00:00.000000000 Z
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