stripe-ruby/lib/stripe/list_object.rb
Brandur af72a57c9d Add #empty? helper on ListObject
This makes ListObject behave a little more like an Array in that it gets
an #empty? helper. This should fit pretty well with the Enumerable
methods that it already has.

Replaces #193.
2015-09-30 13:13:41 -07:00

41 lines
1.1 KiB
Ruby

module Stripe
class ListObject < StripeObject
include Enumerable
include Stripe::APIOperations::Request
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
# Returns true if the page object contains no elements.
def empty?
self.data.empty?
end
def retrieve(id, opts={})
id, retrieve_params = Util.normalize_id(id)
response, opts = request(:get,"#{url}/#{CGI.escape(id)}", retrieve_params, opts)
Util.convert_to_stripe_object(response, opts)
end
def create(params={}, opts={})
response, opts = request(:post, url, params, opts)
Util.convert_to_stripe_object(response, opts)
end
def all(params={}, opts={})
response, opts = request(:get, url, params, opts)
Util.convert_to_stripe_object(response, opts)
end
end
end