Skip to content

Commit

Permalink
Closing issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Nov 25, 2009
1 parent b6a1f81 commit 0ada9f1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
40 changes: 23 additions & 17 deletions lib/pagseguro/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
30 changes: 19 additions & 11 deletions spec/pagseguro/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 0ada9f1

Please sign in to comment.