dhs 1.4.0 → 1.4.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: c31dbca2778853f9bafb3a74e2c7ef830d3897c0caa78b66e5bb58203fc8bb11
4
- data.tar.gz: 8e5968134c395c0b4e6196506ef5502f5941f68754423bc885bc536a44c2ba33
3
+ metadata.gz: 8ef9be83e5148eedcda73523574e4d53684aecbf906e9034b12f52c9b8881a7b
4
+ data.tar.gz: c8195c1668a305f9e9fcc723ba2464de9b87f17d2eaaa57ec3d4787ec36d8e45
5
5
  SHA512:
6
- metadata.gz: 54e1c9ec3c0b71566a8b6831690250d0eecc00f00155617dc39873c20d49a26ef57051ae04d7d3a1bbb3733f75fc3ccc0024b96c3b4bd086dafe48a1634cb8cf
7
- data.tar.gz: 1bea7f70ccd0cba8dc488604d5f4a668d1bc602a2cb30d19058b195a17b2d9f1aaa5d72e21286e910570a89be0e78c25adac4bb45186a6f3f11ffcc81128b39c
6
+ metadata.gz: be55a13a577be249b76b1eaeb7baec3a307b94f4a7c2c4643ea1fcf66edb3f8df3927386886babe211b687821229180d33f0cf5e5af04f1af9534ccd67c20ffd
7
+ data.tar.gz: 5ab39a51ac32085b8cb71e183c34edd64a7c5e78c86e6e08faabdd03d196a5aadbbfeba8e7b2ac8dc571d59b2ad7281cda2a20c5d8c131e3fbafdf061c6a0adc
@@ -204,7 +204,7 @@ class DHS::Record
204
204
  end
205
205
 
206
206
  def load_and_merge_not_paginated_collection!(data, options)
207
- return if data.length.zero?
207
+ return if data.empty?
208
208
  options = options.is_a?(Hash) ? options : {}
209
209
  limit = options.dig(:params, limit_key(:parameter)) || pagination_class::DEFAULT_LIMIT
210
210
  offset = options.dig(:params, pagination_key(:parameter)) || pagination_class::DEFAULT_OFFSET
@@ -230,7 +230,7 @@ class DHS::Record
230
230
  end
231
231
 
232
232
  def load_and_merge_paginated_collection!(data, options)
233
- set_nested_data(data._raw, limit_key(:body), data.length) if data._raw.dig(*limit_key(:body)).blank? && !data.length.zero?
233
+ set_nested_data(data._raw, limit_key(:body), data.length) if data._raw.dig(*limit_key(:body)).blank? && !data.empty?
234
234
  pagination = data._record.pagination(data)
235
235
  return data unless pagination.pages_left?
236
236
  record = data._record
@@ -256,7 +256,7 @@ class DHS::Record
256
256
  page_data = if next_value.is_a?(String) && next_value.match(/^http/)
257
257
  record.request(options.except(:all).merge(url: next_value))
258
258
  else
259
- record.request(options.except(:all).merge(params: (options.dig(:params) || {}).merge(next_value) ))
259
+ record.request(options.except(:all).merge(params: (options.dig(:params) || {}).merge(next_value)))
260
260
  end
261
261
  next_value = pagination.next(page_data._raw)
262
262
  merge_batch_data_with_parent!(page_data, data, pagination)
@@ -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|
@@ -3,7 +3,7 @@
3
3
  class DHS::Pagination::NextOffset < DHS::Pagination::Base
4
4
 
5
5
  DEFAULT_OFFSET = 0
6
-
6
+
7
7
  def total
8
8
  data._raw.dig(*_record.items_key).count || 0
9
9
  end
@@ -54,7 +54,7 @@ module DHS::Problems
54
54
  end
55
55
 
56
56
  def messages_from_response(response = nil)
57
- return {} if !response || !response.body.is_a?(String) || response.body.length.zero?
57
+ return {} if !response || !response.body.is_a?(String) || response.body.empty?
58
58
  json = JSON.parse(response.body)
59
59
  parse_messages(json)
60
60
  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.0'
4
+ VERSION = '1.4.2'
5
5
  end
@@ -19,15 +19,15 @@ describe DHS::Record::Request do
19
19
 
20
20
  it 'calls correct prepare method for a Hash' do
21
21
  expect(subject).to receive(:prepare_option_for_include_all_request!)
22
- .with(abc: 'def').and_return('ignore')
23
- expect(subject.send(:prepare_options_for_include_all_request!, abc: 'def')).to eq(abc: 'def')
22
+ .with({ abc: 'def' }).and_return('ignore')
23
+ expect(subject.send(:prepare_options_for_include_all_request!, { abc: 'def' })).to eq(abc: 'def')
24
24
  end
25
25
 
26
26
  it 'calls correct prepare method for a Hash (collection)' do
27
27
  expect(subject).to receive(:prepare_option_for_include_all_request!)
28
- .with(abc: 'def').and_return('ignore')
28
+ .with({ abc: 'def' }).and_return('ignore')
29
29
  expect(subject).to receive(:prepare_option_for_include_all_request!)
30
- .with(hij: 'kel').and_return('ignore')
30
+ .with({ hij: 'kel' }).and_return('ignore')
31
31
  expect(subject.send(:prepare_options_for_include_all_request!, [{ abc: 'def' }, { hij: 'kel' }]))
32
32
  .to eq([{ abc: 'def' }, { hij: 'kel' }])
33
33
  end
@@ -3,11 +3,9 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  describe DHS::Record do
6
-
7
- context 'pagination' do
8
-
9
- def stub_api_request(items: [], offset: nil, next_offset:)
10
- stub_request(:get, ["http://depay.fi/v2/transactions?limit=100", offset ? "offset=#{offset}" : nil].compact.join('&'))
6
+ context 'next offset pagination' do
7
+ def stub_api_request(next_offset:, items: [], offset: nil)
8
+ stub_request(:get, ['http://depay.fi/v2/transactions?limit=100', offset ? "offset=#{offset}" : nil].compact.join('&'))
11
9
  .to_return(body: { items: items, next_offset: next_offset }.to_json)
12
10
  end
13
11
 
@@ -30,4 +28,56 @@ describe DHS::Record do
30
28
  expect(transactions.to_a).to eq (0...300).to_a
31
29
  end
32
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
33
83
  end
@@ -16,12 +16,12 @@ describe DHS::Record do
16
16
 
17
17
  it 'uses the options that are configured for an endpoint' do
18
18
  expect(DHC).to receive(:request)
19
- .with(
20
- cache_expires_in: 1.day,
21
- retry: 2,
22
- cache: true,
23
- url: 'backend/v2/feedbacks/1'
24
- ).and_call_original
19
+ .with({
20
+ cache_expires_in: 1.day,
21
+ retry: 2,
22
+ cache: true,
23
+ url: 'backend/v2/feedbacks/1'
24
+ }).and_call_original
25
25
 
26
26
  stub_request(:get, 'http://backend/v2/feedbacks/1').to_return(status: 200)
27
27
  Record.find(1)
@@ -214,12 +214,12 @@ describe DHS::Record do
214
214
 
215
215
  it 'overwrites existing pagination paramters if they are already contained in a string' do
216
216
  expect(DHC).to receive(:request)
217
- .with(url: 'http://datastore/customers/1').and_call_original
217
+ .with({ url: 'http://datastore/customers/1' }).and_call_original
218
218
 
219
219
  expect(DHC).to receive(:request)
220
- .with(url: 'http://datastore/customers/1/contracts',
221
- all: true,
222
- params: { limit: 100 }).and_call_original
220
+ .with({ url: 'http://datastore/customers/1/contracts',
221
+ all: true,
222
+ params: { limit: 100 } }).and_call_original
223
223
 
224
224
  expect(DHC).to receive(:request)
225
225
  .with([{ url: 'http://datastore/customers/1/contracts',
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.0
4
+ version: 1.4.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: 2023-04-25 00:00:00.000000000 Z
11
+ date: 2023-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -580,7 +580,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
580
580
  version: '0'
581
581
  requirements:
582
582
  - Ruby >= 2.7.2
583
- rubygems_version: 3.2.33
583
+ rubygems_version: 3.2.3
584
584
  signing_key:
585
585
  specification_version: 4
586
586
  summary: 'REST services accelerator: Rails gem providing an easy, active-record-like