dhs 1.0.1 → 1.0.2

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: 3c77465b9e45a9f59c90566da5a2b706d7b2b1c83ea4a02da1133ae69008b113
4
- data.tar.gz: 80d5e98d765171120c67167c214ca4b9a7a02a2f017d33d1e1d3d98da16edd6d
3
+ metadata.gz: f42a1fdc00478fe3e24e797f52396c953442e4e0d2da738b28fb7cc55b908ea3
4
+ data.tar.gz: 1465403fb48c2dca7f2b955550309a7983a4d264ef10cbb35d620df88f01f1fe
5
5
  SHA512:
6
- metadata.gz: 6ff3c40cdf4da8c18be8420afb4dae547f3d3c3dc6211bdce27706ea48dc1e4099dedf9dc35975cfa7e8291d6a8e0ca485cf58b0a790900883ac547ba14b10a3
7
- data.tar.gz: a366a714e0fb4235f75475079973824a2e620fa8ceca02866fc223f43a69897cb664b6d46120dedb1d6d0aa3dfbeef1ab68de620187bbc74085a01a712ea25bd
6
+ metadata.gz: bd6412647a124e42350e185d60a6a0040aa9ef30db503fddf529db0979f4c6b7151bc3e9cdad8378a111512b4f481868a39839c89e42f7ea420c3a27e2539050
7
+ data.tar.gz: a6936544068aa3b6591163f768a6e3c96ce38317abc183ca3bec8e62c50da10d25518f215b939945acd990f449615865d19dec5886a8cd2e88e59965c1fcb5d8
@@ -8,20 +8,9 @@ jobs:
8
8
 
9
9
  steps:
10
10
  - uses: actions/checkout@v2
11
- - uses: actions/setup-ruby@v1
11
+ - uses: ruby/setup-ruby@master
12
12
  with:
13
- ruby-version: 3.0.0
14
- - name: Cache Ruby Gems
15
- uses: actions/cache@v2
16
- with:
17
- path: /.tmp/vendor/bundle
18
- key: ${{ runner.os }}-gems-latest-${{ hashFiles('**/Gemfile.lock') }}
19
- restore-keys: |
20
- ${{ runner.os }}-gems-latest-
21
- - name: Bundle Install
22
- run: |
23
- bundle config path /.tmp/vendor/bundle
24
- bundle install --jobs 4 --retry 3
13
+ bundler-cache: true
25
14
  - name: Run Rubocop
26
15
  run: |
27
16
  bundle exec rubocop
@@ -8,20 +8,9 @@ jobs:
8
8
 
9
9
  steps:
10
10
  - uses: actions/checkout@v2
11
- - uses: actions/setup-ruby@v1
11
+ - uses: ruby/setup-ruby@master
12
12
  with:
13
- ruby-version: 3.0.0
14
- - name: Cache Ruby Gems
15
- uses: actions/cache@v2
16
- with:
17
- path: /.tmp/vendor/bundle
18
- key: ${{ runner.os }}-gems-latest-${{ hashFiles('**/Gemfile.lock') }}
19
- restore-keys: |
20
- ${{ runner.os }}-gems-latest-
21
- - name: Bundle Install
22
- run: |
23
- bundle config path /.tmp/vendor/bundle
24
- bundle install --jobs 4 --retry 3
13
+ bundler-cache: true
25
14
  - name: Run Tests
26
15
  run: |
27
16
  bundle exec rspec
@@ -22,17 +22,18 @@ class DHS::Record
22
22
  def find_in_batches(options = {})
23
23
  raise 'No block given' unless block_given?
24
24
  options = options.dup
25
- start = options.delete(:start) || 1
26
- batch_size = options.delete(:batch_size) || DHS::Pagination::Base::DEFAULT_LIMIT
25
+ start = options[:start] || self.pagination_class::DEFAULT_OFFSET
26
+ batch_size = options.delete(:batch_size) || self.pagination_class::DEFAULT_LIMIT
27
27
  loop do # as suggested by Matz
28
28
  options = options.dup
29
29
  options[:params] = (options[:params] || {}).merge(limit_key(:parameter) => batch_size, pagination_key(:parameter) => start)
30
30
  data = request(options)
31
- batch_size = data._raw.dig(*limit_key(:body))
32
- left = data._raw.dig(*total_key).to_i - data._raw.dig(*pagination_key(:body)).to_i - data._raw.dig(*limit_key(:body)).to_i
31
+ pagination = self.pagination(data)
32
+ batch_size = pagination.limit
33
+ left = pagination.pages_left
33
34
  yield new(data)
34
35
  break if left <= 0
35
- start += batch_size
36
+ start = pagination.next_offset
36
37
  end
37
38
  end
38
39
  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.0.1'
4
+ VERSION = '1.0.2'
5
5
  end
@@ -29,11 +29,11 @@ describe DHS::Collection do
29
29
 
30
30
  context 'find_each' do
31
31
  it 'processes each record by fetching records in batches' do
32
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=1").to_return(status: 200, body: api_response((1..100).to_a, 1))
33
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=101").to_return(status: 200, body: api_response((101..200).to_a, 101))
34
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=201").to_return(status: 200, body: api_response((201..300).to_a, 201))
35
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=301").to_return(status: 200, body: api_response((301..400).to_a, 301))
36
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=401").to_return(status: 200, body: api_response((401..total).to_a, 401))
32
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=0").to_return(status: 200, body: api_response((1..100).to_a, 0))
33
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=100").to_return(status: 200, body: api_response((101..200).to_a, 100))
34
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=200").to_return(status: 200, body: api_response((201..300).to_a, 200))
35
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=300").to_return(status: 200, body: api_response((301..400).to_a, 300))
36
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=400").to_return(status: 200, body: api_response((401..total).to_a, 400))
37
37
  count = 0
38
38
  Record.find_each do |record|
39
39
  count += 1
@@ -45,7 +45,7 @@ describe DHS::Collection do
45
45
  end
46
46
 
47
47
  it 'passes options to the requests made' do
48
- request = stub_request(:get, 'http://depay.fi/v2/feedbacks?limit=100&offset=1')
48
+ request = stub_request(:get, 'http://depay.fi/v2/feedbacks?limit=100&offset=0')
49
49
  .with(headers: { 'Authorization' => 'Bearer 123' })
50
50
  .to_return(body: {
51
51
  items: []
@@ -29,11 +29,11 @@ describe DHS::Collection do
29
29
 
30
30
  context 'find_batches' do
31
31
  it 'processes records in batches' do
32
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=1").to_return(status: 200, body: api_response((1..100).to_a, 1))
33
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=101").to_return(status: 200, body: api_response((101..200).to_a, 101))
34
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=201").to_return(status: 200, body: api_response((201..300).to_a, 201))
35
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=301").to_return(status: 200, body: api_response((301..400).to_a, 301))
36
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=401").to_return(status: 200, body: api_response((401..total).to_a, 401))
32
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=0").to_return(status: 200, body: api_response((1..100).to_a, 0))
33
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=100").to_return(status: 200, body: api_response((101..200).to_a, 100))
34
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=200").to_return(status: 200, body: api_response((201..300).to_a, 200))
35
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=300").to_return(status: 200, body: api_response((301..400).to_a, 300))
36
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=400").to_return(status: 200, body: api_response((401..total).to_a, 400))
37
37
  length = 0
38
38
  Record.find_in_batches do |records|
39
39
  length += records.length
@@ -44,11 +44,11 @@ describe DHS::Collection do
44
44
  end
45
45
 
46
46
  it 'adapts to backend max limit' do
47
- stub_request(:get, "#{datastore}/feedbacks?limit=230&offset=1").to_return(status: 200, body: api_response((1..100).to_a, 1))
48
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=101").to_return(status: 200, body: api_response((101..200).to_a, 101))
49
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=201").to_return(status: 200, body: api_response((201..300).to_a, 201))
50
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=301").to_return(status: 200, body: api_response((301..400).to_a, 301))
51
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=401").to_return(status: 200, body: api_response((401..total).to_a, 401))
47
+ stub_request(:get, "#{datastore}/feedbacks?limit=230&offset=0").to_return(status: 200, body: api_response((1..100).to_a, 0))
48
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=100").to_return(status: 200, body: api_response((101..200).to_a, 100))
49
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=200").to_return(status: 200, body: api_response((201..300).to_a, 200))
50
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=300").to_return(status: 200, body: api_response((301..400).to_a, 300))
51
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=400").to_return(status: 200, body: api_response((401..total).to_a, 400))
52
52
  length = 0
53
53
  Record.find_in_batches(batch_size: 230) do |records|
54
54
  length += records.length
@@ -59,8 +59,8 @@ describe DHS::Collection do
59
59
  end
60
60
 
61
61
  it 'forwards offset' do
62
- stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=401").to_return(status: 200, body: api_response((401..total).to_a, 401))
63
- Record.find_in_batches(start: 401) do |records|
62
+ stub_request(:get, "#{datastore}/feedbacks?limit=100&offset=400").to_return(status: 200, body: api_response((401..total).to_a, 400))
63
+ Record.find_in_batches(start: 400) do |records|
64
64
  expect(records.length).to eq(total - 400)
65
65
  end
66
66
  end
@@ -119,4 +119,42 @@ describe DHS::Collection do
119
119
  expect(length).to eq total
120
120
  end
121
121
  end
122
+
123
+ context 'different pagination strategy' do
124
+ before do
125
+ class Transaction < DHS::Record
126
+ endpoint 'https://api/transactions'
127
+ configuration limit_key: :items_on_page, pagination_strategy: :total_pages, pagination_key: :page, items_key: :transactions, total_key: :total_pages
128
+ end
129
+
130
+ stub_request(:get, 'https://api/transactions?items_on_page=50&page=1')
131
+ .to_return(body: {
132
+ page: 1,
133
+ total_pages: 2,
134
+ items_on_page: 50,
135
+ transactions: 50.times.map { |index| { id: index } }
136
+ }.to_json)
137
+
138
+ stub_request(:get, 'https://api/transactions?items_on_page=50&page=2')
139
+ .to_return(body: {
140
+ page: 2,
141
+ total_pages: 2,
142
+ items_on_page: 50,
143
+ transactions: 22.times.map { |index| { id: 50 + index } }
144
+ }.to_json)
145
+ end
146
+
147
+ it 'find in batches and paginates automatically even for different pagination strategies' do
148
+ total = 0
149
+ transactions = []
150
+
151
+ Transaction.find_in_batches(batch_size: 50) do |batch|
152
+ total += batch.length
153
+ transactions << batch
154
+ end
155
+
156
+ expect(total).to eq(72)
157
+ expect(transactions.flatten.as_json).to eq(72.times.map { |index| { id: index }.as_json })
158
+ end
159
+ end
122
160
  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.0.1
4
+ version: 1.0.2
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: 2021-04-01 00:00:00.000000000 Z
11
+ date: 2021-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel