Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added payment attributes for xml generation

  • Loading branch information...
commit 6aff219451ddaa0fb6fa8f49e94b085adb884a61 1 parent 0418a3c
@teonimesic teonimesic authored
View
2  Guardfile
@@ -2,4 +2,6 @@ guard 'rspec', version: 2 do
watch(%r{^spec/.+_spec\.rb})
watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
+ watch('lib/pag_seguro.rb') { "spec" }
+ watch('lib/checkout.xml.haml') { "spec/pag_seguro/checkout_xml_spec.rb" }
end
View
15 lib/pag_seguro.rb
@@ -1,6 +1,21 @@
$: << "lib/pag_seguro"
+# Core Ruby Libraries
+require "net/https"
+require "uri"
+
+# Third party gems
+require "active_model"
+require "nokogiri"
+require "haml"
+
+# PagSeguro classes
+require "item"
require "payment"
+require "sender"
+require "shipping"
+
+# Version
require "version"
module PagSeguro
View
59 lib/pag_seguro/checkout.xml.haml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+%checkout
+ - if payment.id.present?
+ %reference= payment.id
+ - if payment.extra_amount.present?
+ %extraAmount= payment.extra_amount
+ - if payment.redirect_url.present?
+ %redirectUrl= payment.redirect_url
+ - if payment.max_uses.present?
+ %maxUses= payment.max_uses
+ - if payment. max_age.present?
+ %maxAge= payment.max_age
+
+ %currency BRL
+
+ %items
+ - items.each do |item|
+ %item
+ %id= item.id
+ %description= item.description
+ %amount= item.amount
+ %quantity= item.quantity
+ - if item.shipping_cost.present?
+ %shippingCost= item.shipping_cost
+ - if item.weight.present?
+ %weight= item.weight
+
+ - if sender.present?
+ %sender
+ - if sender.name.present?
+ %name= sender.name
+ - if sender.email.present?
+ %email= sender.email
+ %phone
+ - if sender.phone_ddd.present?
+ %areaCode= sender.phone_ddd
+ - if sender.phone_number.present?
+ %number= sender.phone_number
+
+ - if shipping.present?
+ %shipping
+ - if shipping.type.present?
+ %type= shipping.type
+ %address
+ %country BRA
+ - if shipping.state.present?
+ %state= shipping.state
+ - if shipping.city.present?
+ %city= shipping.city
+ - if shipping.postal_code.present?
+ %postalCode= shipping.postal_code
+ - if shipping.district.present?
+ %district= shipping.district
+ - if shipping.street.present?
+ %street= shipping.street
+ - if shipping.number.present?
+ %number= shipping.number
+ - if shipping.complement.present?
+ %complement= shipping.complement
View
31 lib/pag_seguro/item.rb
@@ -0,0 +1,31 @@
+module PagSeguro
+ class Item
+ include ActiveModel::Validations
+
+ attr_accessor :id, :description, :amount, :quantity, :shipping_cost, :weight
+
+ validates_presence_of :id, :description, :amount, :quantity
+ validates_format_of :amount, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot"
+ validates_format_of :shipping_cost, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot"
+ validates_format_of :weight, with: /^\d+$/, message: " must be an integer"
+ validate :quantity_amount
+
+ def initialize(attributes = {})
+ @id = attributes[:id]
+ @description = attributes[:description]
+ @amount = attributes[:amount]
+ @quantity = attributes[:quantity]
+ @shipping_cost = attributes[:shipping_cost]
+ @weight = attributes[:weight]
+ end
+
+ def description
+ @description.present? && @description.size > 100 ? @description[0..99] : @description
+ end
+
+ protected
+ def quantity_amount
+ errors.add(:quantity, " must be a number between 1 and 999") if @quantity.present? && (@quantity == "0" || @quantity.to_s !~ /^\d{1,3}$/)
+ end
+ end
+end
View
38 lib/pag_seguro/payment.rb
@@ -1,7 +1,41 @@
-require "net/https"
-
module PagSeguro
class Payment
+ include ActiveModel::Validations
+
BASE_URL = 'ws.pagseguro.uol.com.br/v2/checkout'
+
+ attr_accessor :id, :email, :token, :items, :sender, :shipping, :extra_amount, :redirect_url, :max_uses, :max_age
+
+ validates_presence_of :email, :token
+ validates_format_of :extra_amount, with: /^\d+\.\d{2}$/, message: " must be a decimal and have 2 digits after the dot", allow_blank: true
+ validates_format_of :redirect_url, with: URI::regexp(%w(http https)), message: " must give a correct url for redirection", allow_blank: true
+ validate :max_uses_number, :max_age_number
+
+ def initialize(email = nil, token = nil, options = {})
+ @email = email unless email.nil?
+ @token = token unless token.nil?
+ @id = options[:id]
+ @sender = options[:sender] || Sender.new
+ @shipping = options[:shipping] || Shipping.new
+ @items = options[:items] || []
+ @extra_amount = options[:extra_amount]
+ @redirect_url = options[:redirect_url]
+ @max_uses = options[:max_uses]
+ @max_age = options[:max_age]
+ end
+
+ def checkout_xml
+ xml_content = File.open( File.dirname(__FILE__) + "/checkout.xml.haml" ).read
+ Haml::Engine.new(xml_content).render(nil, items: @items, payment: self, sender: @sender, shipping: @shipping)
+ end
+
+ protected
+ def max_uses_number
+ errors.add(:max_uses, " must be an integer greater than 0") if @max_uses.present? && @max_uses.to_i <= 0
+ end
+
+ def max_age_number
+ errors.add(:max_age, " must be an integer grater or equal to 30") if @max_age.present? && @max_age.to_i < 30
+ end
end
end
View
32 lib/pag_seguro/sender.rb
@@ -0,0 +1,32 @@
+module PagSeguro
+ class Sender
+ attr_accessor :name, :email, :phone_ddd, :phone_number
+
+ def initialize(options = {})
+ @name = options[:name]
+ @email = options[:email]
+ @phone_ddd = options[:phone_ddd]
+ @phone_number = options[:phone_number]
+ end
+
+ def email
+ valid_email? ? @email : nil
+ end
+
+ def valid_email?
+ @email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i && @email.size <= 60
+ end
+
+ def name
+ ( @name.present? && @name.size > 50 ) ? @name[0..49] : @name
+ end
+
+ def phone_ddd
+ @phone_ddd if @phone_ddd =~ /^\d{2}$/
+ end
+
+ def phone_number
+ @phone_number if @phone_number =~/^\d{8}$/
+ end
+ end
+end
View
28 lib/pag_seguro/shipping.rb
@@ -0,0 +1,28 @@
+module PagSeguro
+ class Shipping
+ include ActiveModel::Validations
+
+ PAC = 1
+ SEDEX = 2
+ NOT_IDENTIFIED = 3
+
+ validates_format_of :postal_code, with: /^\d{8}$/, message: " must be an integer with 8 digits", allow_blank: true
+
+ attr_accessor :type, :state, :city, :postal_code, :district, :street, :number, :complement
+
+ def initialize(attributes = {})
+ @type = attributes[:type]
+ @state = attributes[:state]
+ @city = attributes[:city]
+ @postal_code = attributes[:postal_code]
+ @district = attributes[:district]
+ @street = attributes[:street]
+ @number = attributes[:number]
+ @complement = attributes[:complement]
+ end
+
+ def postal_code
+ @postal_code if @postal_code.present? && @postal_code.to_s.size == 8
+ end
+ end
+end
View
2  lib/pag_seguro/version.rb
@@ -1,3 +1,3 @@
module PagSeguro
- VERSION = "0.0.1"
+ VERSION = "0.1.0"
end
View
12 pag_seguro.gemspec
@@ -3,18 +3,22 @@ $:.push File.expand_path("../lib", __FILE__)
require "pag_seguro/version"
Gem::Specification.new do |s|
+ s.platform = Gem::Platform::RUBY
s.name = "pag_seguro"
s.version = PagSeguro::VERSION
s.authors = ["Stefano Diem Benatti"]
s.email = ["stefano.diem@gmail.com"]
- s.homepage = ""
+ s.homepage = "http://github.com/heavenstudio/pag_seguro"
s.summary = %q{A ruby gem to handle PagSeguro's API version 2}
- s.description = %q{A ruby gem to handle PagSeguro's API version 2}
-
- s.rubyforge_project = "pag_seguro"
+ s.required_ruby_version = '>= 1.9.2'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
+ s.has_rdoc = false
+
+ s.add_dependency('activemodel')
+ s.add_dependency('haml')
+ s.add_dependency('nokogiri')
end
View
176 spec/pag_seguro/checkout_xml_spec.rb
@@ -0,0 +1,176 @@
+# encoding: utf-8
+require 'spec_helper'
+
+items = [
+ PagSeguro::Item.new(id: 25, description: "A Bic Pen", amount: "1.50", quantity: "4", shipping_cost: "1.00", weight: 10),
+ PagSeguro::Item.new(id: 73, description: "A Book", amount: "38.23", quantity: "1", shipping_cost: "12.00", weight: 300),
+ PagSeguro::Item.new(id: 95, description: "A Towel", amount: "69.35", quantity: "2", weight: 400),
+ PagSeguro::Item.new(id: 17, description: "A pipe", amount: "3.00", quantity: "89")
+]
+
+sender_info = {name: "Stefano Diem Benatti", email: "stefano@heavenstudio.com.br", phone_ddd: "11", phone_number: "93430994"}
+
+shipping_info = {type: PagSeguro::Shipping::SEDEX, state: "SP", city: "São Paulo", postal_code: "05363000",
+ district: "Jd. PoliPoli", street: "Av. Otacilio Tomanik", number: "775", complement: "apto. 92"}
+
+
+describe PagSeguro::Payment do
+ context "checkout_xml" do
+ before { @payment = PagSeguro::Payment.new }
+
+ it "should be a valid xml" do
+ lambda { Nokogiri::XML(@payment.checkout_xml) { |config| config.options = Nokogiri::XML::ParseOptions::STRICT } }.should_not raise_error
+ end
+
+ it "should have encoding UTF-8" do
+ @payment.checkout_xml.should match(/^<\?xml.+encoding="UTF-8".+\?>$/)
+ end
+
+ it "should have currency BRL" do
+ Nokogiri::XML(@payment.checkout_xml).css("checkout currency").first.content.should == "BRL"
+ end
+
+ context "items" do
+ before do
+ @payment.items = items
+ @xml = Nokogiri::XML(@payment.checkout_xml)
+ end
+
+ it "should have 4 items" do
+ @xml.css("checkout items item").size.should == 4
+ end
+
+ it "should show all ids" do
+ @xml.css("checkout items item id").map(&:content).should == ["25","73","95","17"]
+ end
+
+ it "should show all descriptions" do
+ @xml.css("checkout items item description").map(&:content).should == ["A Bic Pen","A Book","A Towel","A pipe"]
+ end
+
+ it "should show all amounts" do
+ @xml.css("checkout items item amount").map(&:content).should == ["1.50","38.23","69.35","3.00"]
+ end
+
+ it "should show all quantities" do
+ @xml.css("checkout items item quantity").map(&:content).should == ["4","1","2","89"]
+ end
+
+ it "should show all shipping_costs" do
+ @xml.css("checkout items item shippingCost").map(&:content).should == ["1.00","12.00"]
+ end
+
+ it "should show all weights" do
+ @xml.css("checkout items item weight").map(&:content).should == ["10","300","400"]
+ end
+ end
+
+ context "sender info" do
+ before do
+ @xml_without_sender_info = Nokogiri::XML(@payment.checkout_xml)
+ @payment.sender = PagSeguro::Sender.new(sender_info)
+ @xml = Nokogiri::XML(@payment.checkout_xml)
+ end
+
+ it "should have sender name" do
+ @xml_without_sender_info.css("checkout sender name").should be_empty
+ @xml.css("checkout sender name").first.content.should == "Stefano Diem Benatti"
+ end
+
+ it "should have sender email" do
+ @xml_without_sender_info.css("checkout sender email").should be_empty
+ @xml.css("checkout sender email").first.content.should == "stefano@heavenstudio.com.br"
+ end
+
+ it "should have sender phone ddd" do
+ @xml_without_sender_info.css("checkout sender phone areaCode").should be_empty
+ @xml.css("checkout sender phone areaCode").first.content.should == "11"
+ end
+
+ it "should have sender phone number" do
+ @xml_without_sender_info.css("checkout sender phone number").should be_empty
+ @xml.css("checkout sender phone number").first.content.should == "93430994"
+ end
+ end
+
+ context "shipping" do
+ before do
+ @xml_without_shipping_info = Nokogiri::XML(@payment.checkout_xml)
+ @payment.shipping = PagSeguro::Shipping.new(shipping_info)
+ @xml = Nokogiri::XML(@payment.checkout_xml)
+ end
+
+ it "should have shipping type" do
+ @xml_without_shipping_info.css("checkout shipping type").should be_empty
+ @xml.css("checkout shipping type").first.content.to_i.should == PagSeguro::Shipping::SEDEX
+ end
+
+ it "should have state" do
+ @xml_without_shipping_info.css("checkout shipping address state").should be_empty
+ @xml.css("checkout shipping address state").first.content.should == "SP"
+ end
+
+ it "should have city" do
+ @xml_without_shipping_info.css("checkout shipping address city").should be_empty
+ @xml.css("checkout shipping address city").first.content.should == "São Paulo"
+ end
+
+ it "should have posta code" do
+ @xml_without_shipping_info.css("checkout shipping address postalCode").should be_empty
+ @xml.css("checkout shipping address postalCode").first.content.should == "05363000"
+ end
+
+ it "should have district" do
+ @xml_without_shipping_info.css("checkout shipping address district").should be_empty
+ @xml.css("checkout shipping address district").first.content.should == "Jd. PoliPoli"
+ end
+
+ it "should have street" do
+ @xml_without_shipping_info.css("checkout shipping address street").should be_empty
+ @xml.css("checkout shipping address street").first.content.should == "Av. Otacilio Tomanik"
+ end
+
+ it "should have number" do
+ @xml_without_shipping_info.css("checkout shipping address number").should be_empty
+ @xml.css("checkout shipping address number").first.content.should == "775"
+ end
+
+ it "should have complement" do
+ @xml_without_shipping_info.css("checkout shipping address complement").should be_empty
+ @xml.css("checkout shipping address complement").first.content.should == "apto. 92"
+ end
+ end
+
+ context "payment settings" do
+ it "should not show id unless specified" do
+ Nokogiri::XML(@payment.checkout_xml).css("checkout reference").should be_empty
+ @payment.id = 305
+ Nokogiri::XML(@payment.checkout_xml).css("checkout reference").first.content.should == "305"
+ end
+
+ it "should not show extra amount unless specified" do
+ Nokogiri::XML(@payment.checkout_xml).css("checkout extraAmount").should be_empty
+ @payment.extra_amount = "10.50"
+ Nokogiri::XML(@payment.checkout_xml).css("checkout extraAmount").first.content.should == "10.50"
+ end
+
+ it "should not show redirect url unless specified" do
+ Nokogiri::XML(@payment.checkout_xml).css("checkout redirectUrl").should be_empty
+ @payment.redirect_url = "http://heavenstudio.com.br"
+ Nokogiri::XML(@payment.checkout_xml).css("checkout redirectUrl").first.content.should == "http://heavenstudio.com.br"
+ end
+
+ it "should not show max uses unless specified" do
+ Nokogiri::XML(@payment.checkout_xml).css("checkout maxUses").should be_empty
+ @payment.max_uses = "10"
+ Nokogiri::XML(@payment.checkout_xml).css("checkout maxUses").first.content.should == "10"
+ end
+
+ it "should not show max age unless specified" do
+ Nokogiri::XML(@payment.checkout_xml).css("checkout maxAge").should be_empty
+ @payment.max_age = "5000"
+ Nokogiri::XML(@payment.checkout_xml).css("checkout maxAge").first.content.should == "5000"
+ end
+ end
+ end
+end
View
79 spec/pag_seguro/item_spec.rb
@@ -0,0 +1,79 @@
+require "spec_helper"
+
+valid_attributes = {
+ id: 1,
+ description: "descrevendo um item",
+ amount: "100.50",
+ quantity: 1,
+ shipping_cost: "10.50",
+ weight: 300
+}
+
+describe PagSeguro::Item do
+ context "instance" do
+ before { @item = PagSeguro::Item.new }
+
+ it { @item.should have_attribute_accessor(:id) }
+ it { @item.should have_attribute_accessor(:description) }
+ it { @item.should have_attribute_accessor(:amount) }
+ it { @item.should have_attribute_accessor(:quantity) }
+ it { @item.should have_attribute_accessor(:shipping_cost) }
+ it { @item.should have_attribute_accessor(:weight) }
+
+ it "should be valid with valid attributes" do
+ PagSeguro::Item.new(valid_attributes).should be_valid
+ end
+
+ it "should not be valid without an id" do
+ PagSeguro::Item.new(valid_attributes.except(:id)).should_not be_valid
+ end
+
+ it "should not be valid without a description" do
+ PagSeguro::Item.new(valid_attributes.except(:description)).should_not be_valid
+ end
+
+ it "should trim description to 100 characters if it has more than 100 characters" do
+ item = PagSeguro::Item.new(valid_attributes)
+ item.description = "-" * 100
+ item.description.size.should == 100
+ item.should be_valid
+ item.description = "-" * 101
+ item.description.size.should == 100
+ item.should be_valid
+ end
+
+ it "should not be valid without an amount" do
+ PagSeguro::Item.new(valid_attributes.except(:amount)).should_not be_valid
+ end
+
+ it "should not allow invalid amount formats" do
+ PagSeguro::Item.new(valid_attributes.merge(amount: "10")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(amount: "10,50")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(amount: "R$ 10.50")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(amount: "-10.50")).should_not be_valid
+ end
+
+ it "should not be valid without a quantity" do
+ PagSeguro::Item.new(valid_attributes.except(:quantity)).should_not be_valid
+ end
+
+ it "should not allow invalid quantities" do
+ PagSeguro::Item.new(valid_attributes.merge(quantity: "1000")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(quantity: "0")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(quantity: "-1")).should_not be_valid
+ end
+
+ it "should not allow invalid shipping_cost formats" do
+ PagSeguro::Item.new(valid_attributes.merge(shipping_cost: "10")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(shipping_cost: "10,50")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(shipping_cost: "R$ 10.50")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(shipping_cost: "-10.50")).should_not be_valid
+ end
+
+ it "should not allow non integer values for weight" do
+ PagSeguro::Item.new(valid_attributes.merge(weight: "-10")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(weight: "10.5")).should_not be_valid
+ PagSeguro::Item.new(valid_attributes.merge(weight: "10,5")).should_not be_valid
+ end
+ end
+end
View
79 spec/pag_seguro/payment_spec.rb
@@ -4,4 +4,83 @@
it "should have a base url to 'ws.pagseguro.uol.com.br/v2/checkout'" do
PagSeguro::Payment::BASE_URL.should == 'ws.pagseguro.uol.com.br/v2/checkout'
end
+
+ context "instance" do
+ context "accessors" do
+ before { @payment = PagSeguro::Payment.new }
+
+ it { @payment.should have_attribute_accessor(:id) }
+ it { @payment.should have_attribute_accessor(:items) }
+ it { @payment.should have_attribute_accessor(:sender) }
+ it { @payment.should have_attribute_accessor(:shipping) }
+ it { @payment.should have_attribute_accessor(:email) }
+ it { @payment.should have_attribute_accessor(:token) }
+ it { @payment.should have_attribute_accessor(:extra_amount) }
+ it { @payment.should have_attribute_accessor(:redirect_url) }
+ it { @payment.should have_attribute_accessor(:max_uses) }
+ it { @payment.should have_attribute_accessor(:max_age) }
+
+ it "should have items" do
+ @payment.items.should be_instance_of(Array)
+ @payment.items.should be_empty
+ end
+
+ it "should have a sender" do
+ @payment.sender.should be_instance_of(PagSeguro::Sender)
+ end
+ end
+
+ it "should allow to set email and token initialization" do
+ payment = PagSeguro::Payment.new("mymail", "mytoken")
+ payment.email.should == "mymail"
+ payment.token.should == "mytoken"
+ end
+
+ context "validation" do
+ before { @payment = PagSeguro::Payment.new("mymail", "mytoken") }
+ it "should be valid with valid attributes" do
+ @payment.should be_valid
+ end
+
+ it "should not be valid without email" do
+ @payment.email = nil
+ @payment.should_not be_valid
+ end
+
+ it "should not be valid without token" do
+ @payment.token = nil
+ @payment.should_not be_valid
+ end
+
+ it "should not be valid with invalid extra amount format" do
+ @payment.extra_amount = "10,50"
+ @payment.should_not be_valid
+ @payment.extra_amount = "R$ 10.50"
+ @payment.should_not be_valid
+ @payment.extra_amount = "10.50"
+ @payment.should be_valid
+ end
+
+ it "should not allow invalid urls" do
+ @payment.redirect_url = "httd://something"
+ @payment.should_not be_valid
+ @payment.redirect_url = "http://heavenstudio.com.br"
+ @payment.should be_valid
+ end
+
+ it "should allow an invalid number of uses" do
+ @payment.max_uses = "0"
+ @payment.should_not be_valid
+ @payment.max_uses = "10"
+ @payment.should be_valid
+ end
+
+ it "should allow an invalid second time" do
+ @payment.max_age = "29"
+ @payment.should_not be_valid
+ @payment.max_age = "30"
+ @payment.should be_valid
+ end
+ end
+ end
end
View
60 spec/pag_seguro/sender_spec.rb
@@ -0,0 +1,60 @@
+require "spec_helper"
+
+describe PagSeguro::Sender do
+ context "instance" do
+ before { @sender = PagSeguro::Sender.new }
+
+ it "should have an email accessor" do
+ @sender.should have_attribute_accessor(:email)
+ end
+
+ it "should have a name accessor" do
+ @sender.should have_attribute_accessor(:name)
+ end
+
+ it "should have a phone_ddd accessor" do
+ @sender.should have_attribute_accessor(:phone_ddd)
+ end
+
+ it "should have a phone_number" do
+ @sender.should have_attribute_accessor(:phone_number)
+ end
+
+ it "should be able to initialize with all attributes" do
+ sender = PagSeguro::Sender.new(name: "Stefano Diem Benatti", email: "stefano@heavenstudio.com.br", phone_ddd: "11", phone_number: "93430994")
+ sender.name.should == "Stefano Diem Benatti"
+ sender.email.should == "stefano@heavenstudio.com.br"
+ sender.phone_ddd.should == "11"
+ sender.phone_number.should == "93430994"
+ end
+
+ it "should tell valid e-mail appart" do
+ @sender.email = "nothing"
+ @sender.should_not be_a_valid_email
+ @sender.email = ("a" * 50) + "waytoolongemail@mail.com"
+ @sender.should_not be_a_valid_email
+ @sender.email = "stefano@heavenstudio.com.br"
+ @sender.should be_a_valid_email
+ end
+
+ it "should not show invalid e-mail" do
+ @sender.email = "nothing"
+ @sender.email.should be_nil
+ end
+
+ it "should crop the name if it is too big" do
+ @sender.name = "a" * 50 + "b" * 10
+ @sender.name.should == "a" * 50
+ end
+
+ it "should not show invalid phone ddd's" do
+ @sender.phone_ddd = "111"
+ @sender.phone_ddd.should be_nil
+ end
+
+ it "should not show invalid phone number" do
+ @sender.phone_number = "1234567"
+ @sender.phone_number.should be_nil
+ end
+ end
+end
View
36 spec/pag_seguro/shipping_spec.rb
@@ -0,0 +1,36 @@
+# encoding: utf-8
+require "spec_helper"
+
+valid_attributes = {
+ type: PagSeguro::Shipping::SEDEX,
+ state: "SP",
+ city: "São Paulo",
+ postal_code: "05363000",
+ district: "Jd. PoliPoli",
+ street: "Av. Otacilio Tomanik",
+ number: "775",
+ complement: "apto. 92"
+}
+
+describe PagSeguro::Shipping do
+ context "instance" do
+ before { @shipping = PagSeguro::Shipping.new }
+ it { @shipping.should have_attribute_accessor(:type) }
+ it { @shipping.should have_attribute_accessor(:state) }
+ it { @shipping.should have_attribute_accessor(:city) }
+ it { @shipping.should have_attribute_accessor(:postal_code) }
+ it { @shipping.should have_attribute_accessor(:district) }
+ it { @shipping.should have_attribute_accessor(:street) }
+ it { @shipping.should have_attribute_accessor(:number) }
+ it { @shipping.should have_attribute_accessor(:complement) }
+ end
+
+ it "should be able to initialize all attributes" do
+ PagSeguro::Shipping.new(valid_attributes).should be_valid
+ end
+
+ it "should not show postal code unless valid" do
+ PagSeguro::Shipping.new(valid_attributes).postal_code.should == "05363000"
+ PagSeguro::Shipping.new(valid_attributes.merge(postal_code: 1234567)).postal_code.should be_blank
+ end
+end
View
26 spec/spec_helper.rb
@@ -1,4 +1,28 @@
require 'simplecov'
SimpleCov.start
-require File.dirname(__FILE__) + "/../lib/pag_seguro"
+require File.dirname(__FILE__) + "/../lib/pag_seguro"
+
+class HaveAttributeAccessor
+ def initialize(attribute)
+ @attribute = attribute
+ end
+
+ def matches?(target)
+ @target = target
+ @target.respond_to?(:"#{@attribute}").should == true
+ @target.respond_to?(:"#{@attribute}=").should == true
+ end
+
+ def failure_message
+ "expected #{@target.inspect} to have '#{@expected}' attribute accessor"
+ end
+
+ def negative_failure_message
+ "expected #{@target.inspect} not to have '#{@expected}' attribute accessor"
+ end
+end
+
+def have_attribute_accessor(attribute)
+ HaveAttributeAccessor.new(attribute)
+end
Please sign in to comment.
Something went wrong with that request. Please try again.