Skip to content

Commit

Permalink
Merge pull request #87 from DigitalNZ/gm/custom-scroll-params
Browse files Browse the repository at this point in the history
feat(Scroll pagination): Allow to customise the scroll duration param
  • Loading branch information
motizuki authored Nov 3, 2022
2 parents 117f6bd + 927394d commit 62c4170
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
14 changes: 12 additions & 2 deletions lib/supplejack_common/paginated_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class PaginatedCollection
:type,
:next_page_token_location,
:total_selector,
:duration_parameter,
:duration_value,
:initial_param

# rubocop:disable Metrics/CyclomaticComplexity
Expand All @@ -36,6 +38,8 @@ def initialize(klass, pagination_options = {}, options = {})
@job = pagination_options[:job]
@base_urls = pagination_options[:base_urls] || []
@block = pagination_options[:block]
@duration_parameter = pagination_options[:duration_parameter]
@duration_value = pagination_options[:duration_value]

puts "Starting from #{@base_urls[0]}"

Expand Down Expand Up @@ -121,10 +125,16 @@ def next_paginated_url(url)
end

def next_scroll_url(url)
return url unless klass._document.present?
return url + joiner(url) + scroll_url_query_params unless klass._document.present?

base_url = url.match('(?<base_url>.+\/collection)')[:base_url]
base_url + klass._document.headers[:location]

base_url + klass._document.headers[:location] + joiner(url) + scroll_url_query_params
end

def scroll_url_query_params
return '' unless duration_parameter.present? && duration_value.present?
{ duration_parameter => duration_value }.to_query
end

def url_options
Expand Down
30 changes: 27 additions & 3 deletions spec/supplejack_common/paginated_collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
end

context 'scroll API' do
let(:params) { { type: 'scroll' } }
let(:params) { { type: 'scroll', duration_parameter: 'scrolling_duration', duration_value: '10m' } }
let(:collection) { klass.new(SupplejackCommon::Base, params) }

context 'when the _document is present' do
Expand All @@ -125,7 +125,7 @@
end

it 'generates the next url based on the header :location in the response' do
expect(collection.send(:next_url, 'http://google/collection/_scroll')).to eq 'http://google/collection/scroll/scroll_token/pages'
expect(collection.send(:next_url, 'http://google/collection/_scroll')).to eq 'http://google/collection/scroll/scroll_token/pages?scrolling_duration=10m'
end
end

Expand All @@ -135,7 +135,31 @@
end

it 'uses the url that it was instantiated with' do
expect(collection.send(:next_url, 'http://google/collection/_scroll')).to eq 'http://google/collection/_scroll'
expect(collection.send(:next_url, 'http://google/collection/_scroll')).to eq 'http://google/collection/_scroll?scrolling_duration=10m'
end

context 'when duration_value is not present' do
let(:params) { { type: 'scroll', duration_parameter: 'scrolling_duration' } }

it 'does not add any query params' do
expect(collection.send(:next_url, 'http://google/collection/_scroll')).to eq 'http://google/collection/_scroll?'
end
end

context 'when duration_parameter is not present' do
let(:params) { { type: 'scroll', duration_value: '1m' } }

it 'does not add any query params' do
expect(collection.send(:next_url, 'http://google/collection/_scroll')).to eq 'http://google/collection/_scroll?'
end
end

context 'when duration_parameter and duration_value are not present' do
let(:params) { { type: 'scroll' } }

it 'does not add any query params' do
expect(collection.send(:next_url, 'http://google/collection/_scroll')).to eq 'http://google/collection/_scroll?'
end
end
end
end
Expand Down

0 comments on commit 62c4170

Please sign in to comment.