Skip to content

Commit

Permalink
Enables iterating over index endpoint results in a given block
Browse files Browse the repository at this point in the history
  • Loading branch information
eikes committed Jan 11, 2022
1 parent 9181d07 commit 20d8dc6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 48 deletions.
17 changes: 11 additions & 6 deletions lib/ioki/apis/endpoints/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ def full_path
base_path + [resource_path_name]
end

def call(client, args = [], options = {})
def call(client, args = [], options = {}, &block)
auto_paginate = options.delete(:auto_paginate)

if auto_paginate
paginated_requests(client, args, options)
paginated_requests(client, args, options, &block)
elsif block_given?
send_request(client, args, options).first.each { |item| block.call(item) }
else
send_request(client, args, options).first
end
end

private

def paginated_requests(client, args, options)
# Accumulates request results, while iterating over pages. The iteration stops based on the response's meta data
# so we're incrementing a counter and handling the collection manually.
def paginated_requests(client, args, options, &block)
# Accumulates request results or yields them to given block while iterating over pages.

results = []
current_page = 1
Expand All @@ -47,7 +48,11 @@ def paginated_requests(client, args, options)
options[:params][:page] = current_page

page_results, parsed_response = send_request(client, args, options)
results += page_results
if block_given?
page_results.each { |item| block.call(item) }
else
results += page_results
end
current_page += 1

break results unless parsed_response.dig('meta', 'last_page') == false
Expand Down
107 changes: 65 additions & 42 deletions spec/ioki/apis/endpoints/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,92 @@
let(:parsed_response) { instance_double(Hash, 'parsed_response', :[] => [], dig: {}) }
let(:full_response) { instance_double(Faraday::Response, 'full_response') }
let(:endpoint) { described_class.new('product', base_path: ['base'], model_class: model_class) }
let(:params) { instance_double(Hash, 'params') }
let(:params) { { per_page: 2 } }

it 'calls #request on the client instantiating classes from the result' do
expect(client).to receive(:request).
with(hash_including(
url: url,
params: params
)).and_return(
{ 'data' => [{ 'id' => '0815' }, { 'id' => '4711' }] }
)
before 'stub client.request' do
allow(client).to receive(:request) do |params|
case params.dig(:params, :page)
when nil, 1
{ 'data' => [{ 'id' => '001' }, { 'id' => '002' }], 'meta' => { 'page' => 1, 'last_page' => false } }
when 2
{ 'data' => [{ 'id' => '003' }, { 'id' => '004' }], 'meta' => { 'page' => 2, 'last_page' => false } }
when 3
{ 'data' => [{ 'id' => '005' }], 'meta' => { 'page' => 3, 'last_page' => true } }
else
raise 'unexpected call'
end
end
end

it 'calls #request on the client' do
expect(client).to receive(:request).with(hash_including(url: url, params: params)).and_return('data' => [])
endpoint.call client, [], model_class: model_class, params: params
end

result = endpoint.call(client, [], model_class: model_class, params: params)
it 'returns a two element Array' do
result = endpoint.call client, [], model_class: model_class, params: params

expect(result).to be_kind_of(Array)
expect(result.size).to eq(2)
end

expect(result.first).to be_kind_of(model_class)
expect(result.first.id).to eq('0815')
it 'returns an Array of the correct model_class' do
result = endpoint.call client, [], model_class: model_class, params: params

expect(result.first).to be_kind_of(model_class)
expect(result.last).to be_kind_of(model_class)
expect(result.last.id).to eq('4711')
end

it 'provides a way to auto_paginate all elements' do
call_number = 0
it 'returns an Array with the correct attributes' do
result = endpoint.call client, [], model_class: model_class, params: params

allow(client).to receive(:request) do |params|
call_number += 1
expect(result.first.id).to eq('001')
expect(result.last.id).to eq('002')
end

case call_number
when 1
expect(params[:params].to_s).to eq('{:per_page=>2, :page=>1}')
{ 'data' => [{ 'id' => '001' }, { 'id' => '002' }], 'meta' => { 'page' => 1, 'last_page' => false } }
when 2
expect(params[:params].to_s).to eq('{:per_page=>2, :page=>2}')
{ 'data' => [{ 'id' => '003' }, { 'id' => '004' }], 'meta' => { 'page' => 2, 'last_page' => false } }
when 3
expect(params[:params].to_s).to eq('{:per_page=>2, :page=>3}')
{ 'data' => [{ 'id' => '005' }], 'meta' => { 'page' => 3, 'last_page' => true } }
else
raise 'unexpected call'
end
end
it 'yields 2 times to the given block' do
expect do |block|
endpoint.call client, [], model_class: model_class, params: params, &block
end.to yield_control.exactly(2).times
end

result = endpoint.call(
client,
[],
model_class: model_class,
params: { per_page: 2 },
auto_paginate: true
it 'yields each model to the given block with the correct model' do
expect do |block|
endpoint.call client, [], model_class: model_class, params: params, &block
end.to yield_successive_args(
have_attributes('id' => '001'),
have_attributes('id' => '002')
)
end

it 'provides a way to auto_paginate all elements' do
result = endpoint.call client, [], model_class: model_class, params: params, auto_paginate: true

expect(result).to be_kind_of(Array)
expect(result.size).to eq(5)
expect(result.map(&:id)).to eq(%w[001 002 003 004 005])
end

it 'works without an explicit params hash' do
expect(client).
to receive(:request).
with(params: { page: 1 }, url: url).
and_return('data' => [{ 'id' => '4711' }])
it 'yields 5 times to the given block using auto_paginate' do
expect do |block|
endpoint.call client, [], model_class: model_class, params: params, auto_paginate: true, &block
end.to yield_control.exactly(5).times
end

it 'yields each model to the given block with the correct model using auto_paginate' do
expect do |block|
endpoint.call client, [], model_class: model_class, params: params, auto_paginate: true, &block
end.to yield_successive_args(
have_attributes('id' => '001'),
have_attributes('id' => '002'),
have_attributes('id' => '003'),
have_attributes('id' => '004'),
have_attributes('id' => '005')
)
end

it 'works without an explicit params hash' do
expect(client).to receive(:request).with(params: { page: 1 }, url: url).and_return('data' => [])
endpoint.call(client, [], model_class: model_class, auto_paginate: true)
end
end

0 comments on commit 20d8dc6

Please sign in to comment.