Permalink
Browse files

using a class to represent card's validity

  • Loading branch information...
1 parent 9c5a069 commit e4cb5fe7867fdcbe835adfb9f55109d2a89b3d8c @rafaelss committed May 14, 2012
Showing with 46 additions and 2 deletions.
  1. +1 −0 lib/cielo.rb
  2. +1 −1 lib/cielo/card.rb
  3. +19 −0 lib/cielo/validity.rb
  4. +1 −1 spec/cielo/card_spec.rb
  5. +24 −0 spec/cielo/validity_spec.rb
View
@@ -7,6 +7,7 @@ module Cielo
autoload :Shop, "cielo/shop"
autoload :Order, "cielo/order"
autoload :Payment, "cielo/payment"
+ autoload :Validity, "cielo/validity"
autoload :Card, "cielo/card"
autoload :TransactionRequest, "cielo/transaction_request"
View
@@ -4,7 +4,7 @@ class Card
tag "dados-portador"
element :number, String, :tag => "numero"
- element :validity, String, :tag => "validade"
+ element :validity, Validity, :tag => "validade"
element :indicator, String, :tag => "indicador"
element :security_code, Integer, :tag => "codigo-seguranca"
element :owner, String, :tag => "nome-portador"
View
@@ -0,0 +1,19 @@
+module Cielo
+ class Validity
+ class InvalidDate < StandardError; end
+
+ def initialize(year, month)
+ @year = year.to_i
+ @month = month.to_i
+
+ now = Time.now
+ if @year < now.year || @month < now.month
+ raise InvalidDate, "#{@month}/#{@year} is not valid"
+ end
+ end
+
+ def to_s
+ [@year, "%02d" % @month].join
+ end
+ end
+end
@@ -5,7 +5,7 @@
it "#to_xml" do
subject.number = "4012001037141112"
- subject.validity = "201511"
+ subject.validity = Cielo::Validity.new(2015, 11)
subject.indicator = 1
subject.security_code = 371
subject.owner = "FULANO DE TAL"
@@ -0,0 +1,24 @@
+require "spec_helper"
+
+describe Cielo::Validity do
+ before do
+ Timecop.freeze(Time.parse("2012-05-14 09:23:00"))
+ end
+
+ after do
+ Timecop.return
+ end
+
+ it "raises an exception if year is not valid" do
+ expect { described_class.new(Time.now.year - 1, Time.now.month) }.should raise_error(described_class::InvalidDate)
+ end
+
+ it "raises an exception if month is not valid" do
+ expect { described_class.new(Time.now.year, Time.now.month - 1) }.should raise_error(described_class::InvalidDate)
+ end
+
+ it "#to_s" do
+ validity = described_class.new(Time.now.year, Time.now.month)
+ validity.to_s.should == "201205"
+ end
+end

0 comments on commit e4cb5fe

Please sign in to comment.