I have the following method:
def all(response = nil, options = {}, data = [])
response ||= get(resource_path, options)
parsed_response = JSON.parse(response.body)
return bad_response(response) unless response.code == '200'
data += parsed_response['data'].nil? ? [] : parsed_response['data'].map { |obj| new(obj) }
puts data.count
if parsed_response.dig('additional_data', 'pagination', 'more_items_in_collection')
options[:start] = parsed_response.dig('additional_data', 'pagination', 'next_start')
all(nil, options, data) # recursively calls itself, passing cumulated data
end
puts data.count # WHY WOULD IT NOT JUST RETURN ONCE, BUT INSTEAD GO DOWN TO INITIAL VALUE?
data
end
My problem with this method, is that when it's time to exit the method:
(if parsed_response.dig('additional_data', 'pagination', 'more_items_in_collection')
returns false
and the last method's line should be fired), it starts to rewind..
The output is:
Pipedrive::Customer.all.count
100
200
300
400
500
600
700
800
900
1000
1100
1200
1300
1400
1500
1600
1700
1800
1900
2000
2100
2200
2300
2400
2500
2600
2700
2800
2900
3000
3100
3200
3300
3400
3500
3600
3700
3800
3900
3991
3991
3900
3800
3700
3600
3500
3400
3300
3200
3100
3000
2900
2800
2700
2600
2500
2400
2300
2200
2100
2000
1900
1800
1700
1600
1500
1400
1300
1200
1100
1000
900
800
700
600
500
400
300
200
100
=> 100
This looks insane, but I hope I'm just missing something extremely simple (indeed I am, but what exactly :) ).
EDIT
My problem is with the result of the method, not with puts itself. I would expect the Pipedrive::Customer.all.count
to return 3991
, but it returns 100
instead... Any ideas? I already tried to put last line (data
) in else
def all(response = nil, options = {}, data = [])
response ||= get(resource_path, options)
parsed_response = JSON.parse(response.body)
return bad_response(response) unless response.code == '200'
data += parsed_response['data'].nil? ? [] : parsed_response['data'].map { |obj| new(obj) }
if parsed_response.dig('additional_data', 'pagination', 'more_items_in_collection')
options[:start] = parsed_response.dig('additional_data', 'pagination', 'next_start')
all(nil, options, data)
else
data
end
data
end
- it still returns 100
..