From 0ada9f19f36fa8be446948639aa0f5e8fd1896c5 Mon Sep 17 00:00:00 2001 From: Nando Vieira Date: Wed, 25 Nov 2009 10:35:01 -0200 Subject: [PATCH] Closing issue #1 --- lib/pagseguro/order.rb | 40 +++++++++++++++++++++--------------- spec/pagseguro/order_spec.rb | 30 +++++++++++++++++---------- spec/spec_helper.rb | 5 ++++- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/lib/pagseguro/order.rb b/lib/pagseguro/order.rb index 159cdca..ba3dc76 100644 --- a/lib/pagseguro/order.rb +++ b/lib/pagseguro/order.rb @@ -2,32 +2,32 @@ module PagSeguro class Order # The list of products added to the order attr_accessor :products - + # Define the shipping type. # Can be EN (PAC) or SD (Sedex) attr_accessor :shipping_type - + def initialize(order_id = nil) reset! self.id = order_id end - + # Set the order identifier. Should be a unique # value to identify this order on your own application def id=(identifier) @id = identifier end - + # Get the order identifier def id @id end - + # Remove all products from this order def reset! @products = [] end - + # Add a new product to the PagSeguro order # The allowed values are: # - weight (Optional. If float, will be multiplied by 1000g) @@ -44,24 +44,30 @@ def <<(options) :fees => nil, :quantity => 1 }.merge(options) - + # convert shipping to cents - options[:shipping] = (options[:shipping] * 100).to_i if options[:shipping].kind_of?(Float) - + options[:shipping] = convert_unit(options[:shipping], 100) + # convert fees to cents - options[:fees] = (options[:fees] * 100).to_i if options[:fees].kind_of?(Float) - + options[:fees] = convert_unit(options[:fees], 100) + # convert price to cents - options[:price] = (options[:price] * 100).to_i if options[:price].kind_of?(Float) - + options[:price] = convert_unit(options[:price], 100) + # convert weight to grammes - options[:weight] = (options[:weight] * 1000).to_i if options[:weight].kind_of?(Float) - + options[:weight] = convert_unit(options[:weight], 1000) + products.push(options) end - + def add(options) self << options end - end + + private + def convert_unit(number, unit) + number = (number * unit).to_i unless number.nil? || number.kind_of?(Integer) + number + end + end end diff --git a/spec/pagseguro/order_spec.rb b/spec/pagseguro/order_spec.rb index 78b2c33..008f6f1 100644 --- a/spec/pagseguro/order_spec.rb +++ b/spec/pagseguro/order_spec.rb @@ -5,33 +5,33 @@ @order = PagSeguro::Order.new @product = {:price => 9.90, :description => "Ruby 1.9 PDF", :id => 1} end - + it "should set order id when instantiating object" do @order = PagSeguro::Order.new("ABCDEF") @order.id.should == "ABCDEF" end - + it "should set order id throught setter" do @order.id = "ABCDEF" @order.id.should == "ABCDEF" end - + it "should reset products" do @order.products += [1,2,3] @order.products.should have(3).items @order.reset! @order.products.should be_empty end - + it "should alias add method" do @order.should_receive(:<<).with(:id => 1) @order.add :id => 1 end - + it "should add product with default settings" do @order << @product @order.products.should have(1).item - + p = @order.products.first p[:price].should == 990 p[:description].should == "Ruby 1.9 PDF" @@ -41,11 +41,11 @@ p[:fees].should be_nil p[:shipping].should be_nil end - + it "should add product with custom settings" do @order << @product.merge(:quantity => 3, :shipping => 3.50, :weight => 100, :fees => 1.50) @order.products.should have(1).item - + p = @order.products.first p[:price].should == 990 p[:description].should == "Ruby 1.9 PDF" @@ -55,15 +55,23 @@ p[:shipping].should == 350 p[:fees].should == 150 end - + it "should convert amounts to cents" do @order << @product.merge(:price => 9.99, :shipping => 3.67) - + p = @order.products.first p[:price].should == 999 p[:shipping].should == 367 end - + + it "should convert big decimal to cents" do + @product.merge!(:price => BigDecimal.new("199.00")) + @order << @product + + p = @order.products.first + p[:price].should == 19900 + end + it "should convert weight to grammes" do @order << @product.merge(:weight => 1.3) @order.products.first[:weight].should == 1300 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f472da2..ca469c0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,16 +1,19 @@ RAILS_ENV = "test" +require "rubygems" require "spec" require "fakeweb" require File.dirname(__FILE__) + "/../lib/pagseguro" +require "bigdecimal" + FakeWeb.allow_net_connect = false PagSeguro::ORIGINAL_CONFIG_FILE = PagSeguro::CONFIG_FILE PagSeguro::CONFIG_FILE.gsub!(/^.*$/, File.dirname(__FILE__) + "/pagseguro.yml") class Object def self.unset_class(*args) - class_eval do + class_eval do args.each do |klass| eval(klass) rescue nil remove_const(klass) if const_defined?(klass)