stripe-ruby/lib/stripe/list_object.rb
Evan Broder 1fbce5288c Add a more helpful error message to ListObject#[]
Our list calls return their results wrapped in an object so that we
can include extra information. We use this, e.g., to include the URL
to query for more records in the Transfer#transactions sublist.

When you get a ListObject, if you want to actually manipulate it as a
list, you have to call `#data` first to get the actual underlying
list.

This adds an exception to the `#[]` method to make what's going on
clearer.

Fixes #68
2013-06-30 23:00:01 -07:00

23 lines
616 B
Ruby

module Stripe
class ListObject < StripeObject
def [](k)
case k
when String, Symbol
super
else
raise ArgumentError.new("You tried to access the #{k.inspect} index, but ListObject types only support String keys. (HINT: List calls return an object with a 'data' (which is the data array). You likely want to call #data[#{k.inspect}])")
end
end
def each(&blk)
self.data.each(&blk)
end
def all(filters={})
response, api_key = Stripe.request(:get, url, @api_key, filters)
Util.convert_to_stripe_object(response, api_key)
end
end
end