Merge pull request #332 from stripe/brandur-pass-all-filters-to-pages

Pass through all paging filters to descendent pages
This commit is contained in:
Brandur 2015-10-10 12:10:41 -07:00
commit a41691d972
3 changed files with 26 additions and 19 deletions

View File

@ -8,9 +8,13 @@ module Stripe
response, opts = request(:get, url, filters, opts)
obj = ListObject.construct_from(response, opts)
# set a limit so that we can fetch the same number when accessing the
# next and previous pages
obj.limit = filters[:limit]
# set filters so that we can fetch the same limit, expansions, and
# predicates when accessing the next and previous pages
#
# just for general cleanliness, remove any paging options
obj.filters = filters.dup
obj.filters.delete(:ending_before)
obj.filters.delete(:starting_after)
obj
end

View File

@ -4,10 +4,10 @@ module Stripe
include Stripe::APIOperations::List
include Stripe::APIOperations::Request
# This accessor allows a `ListObject` to inherit a limit that was given to
# a predecessor. This allows consistent limits as a user pages through
# resources.
attr_accessor :limit
# This accessor allows a `ListObject` to inherit various filters that were
# given to a predecessor. This allows for things like consistent limits,
# expansions, and predicates as a user pages through resources.
attr_accessor :filters
# An empty list object. This is returned from +next+ when we know that
# there isn't a next page in order to replicate the behavior of the API
@ -16,6 +16,11 @@ module Stripe
ListObject.construct_from({ :data => [] }, opts)
end
def initialize(*args)
super
self.filters = {}
end
def [](k)
case k
when String, Symbol
@ -75,10 +80,9 @@ module Stripe
return self.class.empty_list(opts) if !has_more
last_id = data.last.id
params = {
:limit => limit, # may be nil
params = filters.merge({
:starting_after => last_id,
}.merge(params)
}).merge(params)
list(params, opts)
end
@ -90,10 +94,9 @@ module Stripe
def previous_page(params={}, opts={})
first_id = data.first.id
params = {
params = filters.merge({
:ending_before => first_id,
:limit => limit, # may be nil
}.merge(params)
}).merge(params)
list(params, opts)
end

View File

@ -79,11 +79,11 @@ module Stripe
should "fetch a next page through #next_page and respect limit" do
list = TestListObject.construct_from({ :data => [{ :id => 1 }], :has_more => true })
list.limit = 3
@mock.expects(:get).once.with("#{Stripe.api_base}/things?limit=3&starting_after=1", nil, nil).
list.filters = { :expand => ['data.source'], :limit => 3 }
@mock.expects(:get).once.with("#{Stripe.api_base}/things?expand[]=data.source&limit=3&starting_after=1", nil, nil).
returns(make_response({ :data => [{ :id => 2 }], :has_more => false }))
next_list = list.next_page
assert_equal 3, next_list.limit
assert_equal({ :expand => ['data.source'], :limit => 3 }, next_list.filters)
end
should "fetch an empty page through #next_page" do
@ -106,11 +106,11 @@ module Stripe
should "fetch a next page through #previous_page and respect limit" do
list = TestListObject.construct_from({ :data => [{ :id => 2 }] })
list.limit = 3
@mock.expects(:get).once.with("#{Stripe.api_base}/things?ending_before=2&limit=3", nil, nil).
list.filters = { :expand => ['data.source'], :limit => 3 }
@mock.expects(:get).once.with("#{Stripe.api_base}/things?ending_before=2&expand[]=data.source&limit=3", nil, nil).
returns(make_response({ :data => [{ :id => 1 }] }))
next_list = list.previous_page
assert_equal 3, next_list.limit
assert_equal({ :expand => ['data.source'], :limit => 3 }, next_list.filters)
end
#