Permalink
Browse files

Add support for list objects. Bumps version to 1.7.5

  • Loading branch information...
1 parent 7571ab5 commit 7f87b2bc4ad01f80073ef452e81b3ccfcee9302d @boucher boucher committed Oct 26, 2012
Showing with 61 additions and 21 deletions.
  1. +1 −1 VERSION
  2. +2 −1 lib/stripe.rb
  3. +1 −1 lib/stripe/api_resource.rb
  4. +14 −0 lib/stripe/list_object.rb
  5. +2 −1 lib/stripe/util.rb
  6. +1 −1 lib/stripe/version.rb
  7. +10 −2 test/test_helper.rb
  8. +30 −14 test/test_stripe.rb
View
@@ -1 +1 @@
-1.7.4
+1.7.5
View
@@ -24,6 +24,7 @@
require 'stripe/stripe_object'
require 'stripe/api_resource'
require 'stripe/singleton_api_resource'
+require 'stripe/list_object'
require 'stripe/account'
require 'stripe/customer'
require 'stripe/invoice'
@@ -46,7 +47,7 @@
module Stripe
@@ssl_bundle_path = File.join(File.dirname(__FILE__), 'data/ca-certificates.crt')
@@api_key = nil
- @@api_base = 'https://api.stripe.com/v1'
+ @@api_base = 'https://api.stripe.com'
@@verify_ssl_certs = true
def self.api_url(url='')
@@ -8,7 +8,7 @@ def self.url()
if self == APIResource
raise NotImplementedError.new('APIResource is an abstract class. You should perform actions on its subclasses (Charge, Customer, etc.)')
end
- "/#{CGI.escape(class_name.downcase)}s"
+ "/v1/#{CGI.escape(class_name.downcase)}s"
end
def url
View
@@ -0,0 +1,14 @@
+module Stripe
+ class ListObject < StripeObject
+
+ 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
View
@@ -24,7 +24,8 @@ def self.convert_to_stripe_object(resp, api_key)
'plan' => Plan,
'coupon' => Coupon,
'event' => Event,
- 'transfer' => Transfer
+ 'transfer' => Transfer,
+ 'list' => ListObject
}
case resp
when Array
View
@@ -1,3 +1,3 @@
module Stripe
- VERSION = '1.7.4'
+ VERSION = '1.7.5'
end
View
@@ -56,7 +56,11 @@ def test_customer(params={})
end
def test_customer_array
- {:data => [test_customer, test_customer, test_customer]}
+ {
+ :data => [test_customer, test_customer, test_customer],
+ :object => 'list',
+ :url => '/v1/customers'
+ }
end
def test_charge(params={})
@@ -83,7 +87,11 @@ def test_charge(params={})
end
def test_charge_array
- {:data => [test_charge, test_charge, test_charge]}
+ {
+ :data => [test_charge, test_charge, test_charge],
+ :object => 'list',
+ :url => '/v1/charges'
+ }
end
def test_card(params={})
View
@@ -115,7 +115,7 @@ class TestStripeRuby < Test::Unit::TestCase
should "urlencode values in GET params" do
response = test_response(test_charge_array)
- @mock.expects(:get).with('https://api.stripe.com/v1/charges?customer=test%20customer', nil, nil).returns(response)
+ @mock.expects(:get).with("#{Stripe.api_base}/v1/charges?customer=test%20customer", nil, nil).returns(response)
charges = Stripe::Charge.all(:customer => 'test customer').data
assert charges.kind_of? Array
end
@@ -172,20 +172,20 @@ class TestStripeRuby < Test::Unit::TestCase
@mock.expects(:get).with do |url, api_key, params|
uri = URI(url)
query = CGI.parse(uri.query)
- (url =~ %r{^https://api.stripe.com/v1/charges?} &&
+ (url =~ %r{^#{Stripe.api_base}/v1/charges?} &&
query.keys.sort == ['offset', 'sad'])
end.returns(test_response({ :count => 1, :data => [test_charge] }))
c = Stripe::Charge.all(:count => nil, :offset => 5, :sad => false)
@mock.expects(:post).with do |url, api_key, params|
- url == 'https://api.stripe.com/v1/charges' && api_key.nil? && CGI.parse(params) == { 'amount' => ['50'], 'currency' => ['usd'] }
+ url == "#{Stripe.api_base}/v1/charges" && api_key.nil? && CGI.parse(params) == { 'amount' => ['50'], 'currency' => ['usd'] }
end.returns(test_response({ :count => 1, :data => [test_charge] }))
c = Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
end
should "requesting with a unicode ID should result in a request" do
response = test_response(test_missing_id_error, 404)
- @mock.expects(:get).once.with("https://api.stripe.com/v1/customers/%E2%98%83", nil, nil).raises(RestClient::ExceptionWithResponse.new(response, 404))
+ @mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/%E2%98%83", nil, nil).raises(RestClient::ExceptionWithResponse.new(response, 404))
c = Stripe::Customer.new("")
assert_raises(Stripe::InvalidRequestError) { c.refresh }
end
@@ -197,7 +197,7 @@ class TestStripeRuby < Test::Unit::TestCase
should "making a GET request with parameters should have a query string and no body" do
params = { :limit => 1 }
- @mock.expects(:get).once.with("https://api.stripe.com/v1/charges?limit=1", nil, nil).returns(test_response([test_charge]))
+ @mock.expects(:get).once.with("#{Stripe.api_base}/v1/charges?limit=1", nil, nil).returns(test_response([test_charge]))
c = Stripe::Charge.all(params)
end
@@ -233,7 +233,7 @@ class TestStripeRuby < Test::Unit::TestCase
should "updating an object should issue a POST request with only the changed properties" do
@mock.expects(:post).with do |url, api_key, params|
- url == "https://api.stripe.com/v1/customers/c_test_customer" && api_key.nil? && CGI.parse(params) == {'mnemonic' => ['another_mn']}
+ url == "#{Stripe.api_base}/v1/customers/c_test_customer" && api_key.nil? && CGI.parse(params) == {'mnemonic' => ['another_mn']}
end.once.returns(test_response(test_customer))
c = Stripe::Customer.construct_from(test_customer)
c.mnemonic = "another_mn"
@@ -251,7 +251,7 @@ class TestStripeRuby < Test::Unit::TestCase
should "deleting should send no props and result in an object that has no props other deleted" do
@mock.expects(:get).never
@mock.expects(:post).never
- @mock.expects(:delete).with("https://api.stripe.com/v1/customers/c_test_customer", nil, nil).once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
+ @mock.expects(:delete).with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
c = Stripe::Customer.construct_from(test_customer)
c.delete
@@ -287,12 +287,28 @@ class TestStripeRuby < Test::Unit::TestCase
end
end
+ context "list tests" do
+ should "be able to retrieve full lists given a listobject" do
+ @mock.expects(:get).twice.returns(test_response(test_charge_array))
+ c = Stripe::Charge.all
+ assert c.kind_of?(Stripe::ListObject)
+ assert_equal('/v1/charges', c.url)
+ all = c.all
+ assert all.kind_of?(Stripe::ListObject)
+ assert_equal('/v1/charges', all.url)
+ assert all.data.kind_of?(Array)
+ end
+ end
+
context "charge tests" do
should "charges should be listable" do
@mock.expects(:get).once.returns(test_response(test_charge_array))
- c = Stripe::Charge.all.data
- assert c.kind_of? Array
+ c = Stripe::Charge.all
+ assert c.data.kind_of? Array
+ c.each do |charge|
+ assert charge.kind_of?(Stripe::Charge)
+ end
end
should "charges should be refundable" do
@@ -328,7 +344,7 @@ class TestStripeRuby < Test::Unit::TestCase
should "execute should return a new, fully executed charge when passed correct parameters" do
@mock.expects(:post).with do |url, api_key, params|
- url == 'https://api.stripe.com/v1/charges' && api_key.nil? && CGI.parse(params) == {
+ url == "#{Stripe.api_base}/v1/charges" && api_key.nil? && CGI.parse(params) == {
'currency' => ['usd'], 'amount' => ['100'],
'card[exp_year]' => ['2012'],
'card[number]' => ['4242424242424242'],
@@ -393,7 +409,7 @@ class TestStripeRuby < Test::Unit::TestCase
c = Stripe::Customer.retrieve("test_customer")
@mock.expects(:post).once.with do |url, api_key, params|
- url == "https://api.stripe.com/v1/customers/c_test_customer/subscription" && api_key.nil? && CGI.parse(params) == {'plan' => ['silver']}
+ url == "#{Stripe.api_base}/v1/customers/c_test_customer/subscription" && api_key.nil? && CGI.parse(params) == {'plan' => ['silver']}
end.returns(test_response(test_subscription('silver')))
s = c.update_subscription({:plan => 'silver'})
@@ -407,18 +423,18 @@ class TestStripeRuby < Test::Unit::TestCase
# Not an accurate response, but whatever
- @mock.expects(:delete).once.with("https://api.stripe.com/v1/customers/c_test_customer/subscription?at_period_end=true", nil, nil).returns(test_response(test_subscription('silver')))
+ @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription?at_period_end=true", nil, nil).returns(test_response(test_subscription('silver')))
s = c.cancel_subscription({:at_period_end => 'true'})
- @mock.expects(:delete).once.with("https://api.stripe.com/v1/customers/c_test_customer/subscription", nil, nil).returns(test_response(test_subscription('silver')))
+ @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription", nil, nil).returns(test_response(test_subscription('silver')))
s = c.cancel_subscription
end
should "be able to delete a customer's discount" do
@mock.expects(:get).once.returns(test_response(test_customer))
c = Stripe::Customer.retrieve("test_customer")
- @mock.expects(:delete).once.with("https://api.stripe.com/v1/customers/c_test_customer/discount", nil, nil).returns(test_response(test_delete_discount_response))
+ @mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/discount", nil, nil).returns(test_response(test_delete_discount_response))
s = c.delete_discount
assert_equal nil, c.discount
end

0 comments on commit 7f87b2b

Please sign in to comment.