dhs 1.4.2 → 1.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ef9be83e5148eedcda73523574e4d53684aecbf906e9034b12f52c9b8881a7b
4
- data.tar.gz: c8195c1668a305f9e9fcc723ba2464de9b87f17d2eaaa57ec3d4787ec36d8e45
3
+ metadata.gz: 79388bf9da2d5952b1df95513287bbd412dcb5196c99889ea91e28f302cbc64d
4
+ data.tar.gz: e18e9e03e4130cbc2e4dadddb63603170ffee5c2791eb2e64418f39b33f664c8
5
5
  SHA512:
6
- metadata.gz: be55a13a577be249b76b1eaeb7baec3a307b94f4a7c2c4643ea1fcf66edb3f8df3927386886babe211b687821229180d33f0cf5e5af04f1af9534ccd67c20ffd
7
- data.tar.gz: 5ab39a51ac32085b8cb71e183c34edd64a7c5e78c86e6e08faabdd03d196a5aadbbfeba8e7b2ac8dc571d59b2ad7281cda2a20c5d8c131e3fbafdf061c6a0adc
6
+ metadata.gz: 3ccb5289a230ea385574091368925d293106fae4fb5874ead856fb85a68bfa726a531a7adf9ad5b9499c82f24a8a355cf9ac5eb8741b1eb30c862762b3ed6b08
7
+ data.tar.gz: 42edbdaadbf8adc52e7a8a05a979d7b498aa0c8aa084718e29e0237282164482d37cb4b5d499e19633245f8a72e81a67f0fc167a9e8ce87bdf0efad20899932d
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
data/dhs.gemspec CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
 
25
25
  s.add_dependency 'activemodel'
26
26
  s.add_dependency 'activesupport', '>= 6'
27
+ s.add_dependency 'nio4r', '>= 2.7.1'
27
28
  s.add_dependency 'dhc', '>= 2'
28
29
  s.add_dependency 'local_uri'
29
30
 
@@ -1,17 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
- require 'active_support/per_thread_registry'
4
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
5
5
 
6
6
  module DHS
7
7
  module OptionBlocks
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  class CurrentOptionBlock
11
- # Using ActiveSupports PerThreadRegistry to be able to support Active Support v4.
12
- # Will switch to thread_mattr_accessor (which comes with Activesupport) when we dropping support for Active Support v4.
13
- extend ActiveSupport::PerThreadRegistry
14
- attr_accessor :options
11
+ thread_mattr_accessor :options
15
12
  end
16
13
 
17
14
  module ClassMethods
@@ -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
@@ -1,17 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
- require 'active_support/per_thread_registry'
4
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
5
5
 
6
6
  module DHS
7
7
  module Interceptors
8
8
  module AutoOauth
9
9
  extend ActiveSupport::Concern
10
10
  class ThreadRegistry
11
- # Using ActiveSupports PerThreadRegistry to be able to support Active Support v4.
12
- # Will switch to thread_mattr_accessor (which comes with Activesupport) when we dropping support for Active Support v4.
13
- extend ActiveSupport::PerThreadRegistry
14
- attr_accessor :access_token
11
+ thread_mattr_accessor :access_token
15
12
  end
16
13
  end
17
14
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
- require 'active_support/per_thread_registry'
4
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
5
5
 
6
6
  module DHS
7
7
  module Interceptors
@@ -9,10 +9,7 @@ module DHS
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  class ThreadRegistry
12
- # Using ActiveSupports PerThreadRegistry to be able to support Active Support v4.
13
- # Will switch to thread_mattr_accessor (which comes with Activesupport) when we dropping support for Active Support v4.
14
- extend ActiveSupport::PerThreadRegistry
15
- attr_accessor :log
12
+ thread_mattr_accessor :log
16
13
  end
17
14
  end
18
15
  end
@@ -1,17 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
- require 'active_support/per_thread_registry'
5
-
4
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
6
5
  module DHS
7
6
  module Interceptors
8
7
  module RequestCycleCache
9
8
  extend ActiveSupport::Concern
10
9
  class ThreadRegistry
11
- # Using ActiveSupports PerThreadRegistry to be able to support Active Support v4.
12
- # Will switch to thread_mattr_accessor (which comes with Activesupport) when we dropping support for Active Support v4.
13
- extend ActiveSupport::PerThreadRegistry
14
- attr_accessor :request_id
10
+ thread_mattr_accessor :request_id
15
11
  end
16
12
  end
17
13
  end
@@ -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?
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.2'
4
+ VERSION = '1.6.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
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe DHS::Record do
6
+ context 'next parameter pagination' do
7
+ def stub_api_request(this_parameter:, next_parameter:, items: [])
8
+ stub_request(:get, ['http://depay.fi/v2/transactions?limit=100', this_parameter ? "next=#{this_parameter}" : nil].compact.join('&'))
9
+ .to_return(body: { items: items, next: next_parameter }.to_json)
10
+ end
11
+
12
+ let!(:requests) do
13
+ stub_api_request(items: (0...100).to_a, this_parameter: nil, next_parameter: 'NEXT1')
14
+ stub_api_request(items: (100...200).to_a, this_parameter: 'NEXT1', next_parameter: 'NEXT2')
15
+ stub_api_request(items: nil, this_parameter: 'NEXT2', next_parameter: nil)
16
+ end
17
+
18
+ before do
19
+ class Transaction < DHS::Record
20
+ configuration pagination_strategy: :next_parameter, pagination_key: { body: :next, parameter: :next }
21
+
22
+ endpoint 'http://depay.fi/v2/transactions'
23
+ end
24
+ end
25
+
26
+ it 'fetches all the pages' do
27
+ transactions = Transaction.all.fetch
28
+ expect(transactions.to_a).to eq (0...200).to_a
29
+ end
30
+ end
31
+
32
+ context 'next parameter pagination with items configuration and nil response' do
33
+ def stub_api_request(this_parameter:, next_parameter:, items: [])
34
+ stub_request(:get, ['http://depay.fi/v2/transactions?limit=100', this_parameter ? "next=#{this_parameter}" : nil].compact.join('&'))
35
+ .to_return(body: { assets: items, next: next_parameter }.to_json)
36
+ end
37
+
38
+ let!(:requests) do
39
+ stub_api_request(items: (0...100).to_a, this_parameter: nil, next_parameter: 'NEXT1')
40
+ stub_api_request(items: (100...200).to_a, this_parameter: 'NEXT1', next_parameter: 'NEXT2')
41
+ stub_api_request(items: nil, this_parameter: 'NEXT2', next_parameter: nil)
42
+ end
43
+
44
+ before do
45
+ class Transaction < DHS::Record
46
+ configuration items_key: :assets, pagination_strategy: :next_parameter, pagination_key: { body: :next, parameter: :next }
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...200).to_a
55
+ end
56
+ end
57
+ 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.2
4
+ version: 1.6.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-28 00:00:00.000000000 Z
11
+ date: 2024-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nio4r
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.7.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.7.1
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: dhc
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -328,6 +342,7 @@ files:
328
342
  - lib/dhs/pagination/base.rb
329
343
  - lib/dhs/pagination/link.rb
330
344
  - lib/dhs/pagination/next_offset.rb
345
+ - lib/dhs/pagination/next_parameter.rb
331
346
  - lib/dhs/pagination/offset.rb
332
347
  - lib/dhs/pagination/offset_page.rb
333
348
  - lib/dhs/pagination/page.rb
@@ -468,6 +483,7 @@ files:
468
483
  - spec/pagination/link/parallel_spec.rb
469
484
  - spec/pagination/link/total_spec.rb
470
485
  - spec/pagination/next_offset_spec.rb
486
+ - spec/pagination/next_parameter_spec.rb
471
487
  - spec/pagination/offset/pages_left_spec.rb
472
488
  - spec/pagination/offset_page_spec.rb
473
489
  - spec/pagination/parameters_spec.rb
@@ -580,7 +596,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
580
596
  version: '0'
581
597
  requirements:
582
598
  - Ruby >= 2.7.2
583
- rubygems_version: 3.2.3
599
+ rubygems_version: 3.3.26
584
600
  signing_key:
585
601
  specification_version: 4
586
602
  summary: 'REST services accelerator: Rails gem providing an easy, active-record-like
@@ -706,6 +722,7 @@ test_files:
706
722
  - spec/pagination/link/parallel_spec.rb
707
723
  - spec/pagination/link/total_spec.rb
708
724
  - spec/pagination/next_offset_spec.rb
725
+ - spec/pagination/next_parameter_spec.rb
709
726
  - spec/pagination/offset/pages_left_spec.rb
710
727
  - spec/pagination/offset_page_spec.rb
711
728
  - spec/pagination/parameters_spec.rb