Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #4 from ilyakatz/master

currency confusion
  • Loading branch information...
commit fcfd143a827e32813079d97f78bf0001d87ab411 2 parents f294845 + 93c2d7e
@topfunky authored
View
3  google-checkout.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = %q{google-checkout}
- s.version = "0.5.0"
+ s.version = "0.6.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Geoffrey Grosenbach"]
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<hoe-git>, ["= 1.3.0"])
s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
s.add_development_dependency(%q<hoe>, [">= 2.8.0"])
+ s.add_development_dependency(%q<isolate>)
else
s.add_dependency(%q<builder>, [">= 0"])
s.add_dependency(%q<nokogiri>, [">= 0"])
View
16 lib/google-checkout/cart.rb
@@ -264,11 +264,17 @@ def currency
# Mixing currency not allowed; this
# library can't convert between
# currencies.
- @currency ||=
- (@contents.map { |item|
- item.currency
- }.uniq.first rescue nil) ||
- 'USD'
+ currencies = @contents.map { |item| item[:currency] }.uniq || "USD"
+
+ case currencies.count
+ when 0
+ "USD"
+ when 1
+ currencies.first
+ else
+ raise RuntimeError.new("Mixing currency not allowed")
+ end
+
end
# Returns the signature for the cart XML.
View
94 spec/google-checkout/cart_spec.rb
@@ -4,9 +4,9 @@
before(:each) do
@cart = GoogleCheckout::Cart.new("my_id", "my_key", {
- :name => "PeepCode Screencast",
- :description => "A few screencasts",
- :price => 9.00
+ :name => "PeepCode Screencast",
+ :description => "A few screencasts",
+ :price => 9.00
})
GoogleCheckout.use_sandbox
end
@@ -35,13 +35,77 @@
end
+describe GoogleCheckout, "Cart (currency)" do
+
+ it "should show correct currency if all items use the same currency" do
+ @cart = GoogleCheckout::Cart.new("my_id", "my_key", {
+ :name => "PeepCode Screencast",
+ :description => "A few screencasts",
+ :price => 9.00,
+ :currency => "CAD"
+ })
+ @cart.currency.should == "CAD"
+ @cart.add_item(
+ :name => "PeepCode Screencast 2",
+ :description => "A few more casts",
+ :price => 9.00,
+ :currency => "CAD"
+ )
+ @cart.currency.should == "CAD"
+ end
+
+ it "should show correct currency if all items use the same currency" do
+ @cart = GoogleCheckout::Cart.new("my_id", "my_key", {
+ :name => "PeepCode Screencast",
+ :description => "A few screencasts",
+ :price => 9.00,
+ :currency => "JPY"
+ })
+ @cart.currency.should == "JPY"
+ end
+
+ it "should show USD for empty cart" do
+ @cart = GoogleCheckout::Cart.new("my_id", "my_key")
+ @cart.currency.should == "USD"
+ end
+
+ it "should throw an exception if currencies are mixed" do
+ @cart = GoogleCheckout::Cart.new("my_id", "my_key", {
+ :name => "PeepCode Screencast",
+ :description => "A few screencasts",
+ :price => 9.00,
+ :currency => "CAD"
+ })
+ @cart.currency.should == "CAD"
+ @cart.add_item(
+ :name => "PeepCode Screencast 2",
+ :description => "A few more casts",
+ :price => 9.00,
+ :currency => "USD"
+ )
+ lambda { @cart.currency }.should raise_exception(RuntimeError, "Mixing currency not allowed")
+ end
+
+
+ it "should show USD if no currency provided" do
+ @cart = GoogleCheckout::Cart.new("my_id", "my_key", {
+ :name => "PeepCode Screencast",
+ :description => "A few screencasts",
+ :price => 9.00
+ })
+ @cart.currency.should == "USD"
+ end
+
+
+end
+
describe GoogleCheckout, "Cart Post" do
before(:each) do
@cart = GoogleCheckout::Cart.new("my_id", "my_key", {
- :name => "PeepCode Screencast",
- :description => "One screencast",
- :price => 9.00
+ :name => "PeepCode Screencast",
+ :description => "One screencast",
+ :price => 9.00
})
GoogleCheckout.use_sandbox
end
@@ -56,7 +120,7 @@
it "should post request to Google" do
# :null_object means eat all other methods and return self
- net_http = mock("net_http", { :null_object => true })
+ net_http = mock("net_http", {:null_object => true})
Net::HTTP.should_receive(:new).and_return(net_http)
success_response = Net::HTTPSuccess.new(Net::HTTP.version_1_2, 200, "OK")
@@ -70,12 +134,12 @@
end
it "should set merchant private data" do
- @cart.merchant_private_data = { "merchant-order-number" => "1234-5678-9012" }
+ @cart.merchant_private_data = {"merchant-order-number" => "1234-5678-9012"}
@cart.merchant_private_data["merchant-order-number"].should == "1234-5678-9012"
end
it "should include merchant private in the generated xml" do
- @cart.merchant_private_data = { "merchant-order-number" => "1234-5678-9012" }
+ @cart.merchant_private_data = {"merchant-order-number" => "1234-5678-9012"}
@cart.to_xml.should match(/<merchant-order-number>1234-5678-9012<\/merchant-order-number>/)
end
@@ -85,12 +149,12 @@
it "should include merchant-item-id in XML if :item_id was passed with the item" do
@cart.add_item({
- :name => "Item",
- :description => "Item description",
- :price => "1.00",
- :quantity => 1,
- :item_id => "ITEM-007"
- })
+ :name => "Item",
+ :description => "Item description",
+ :price => "1.00",
+ :quantity => 1,
+ :item_id => "ITEM-007"
+ })
@cart.to_xml.should match(%r{<merchant-item-id>ITEM-007</merchant-item-id>})
end
Please sign in to comment.
Something went wrong with that request. Please try again.