Permalink
Browse files

Add all fields to generate Bill

Using symbol to identifier the payment method
  • Loading branch information...
1 parent 06dd287 commit b0d2f670226847d9b3f184e893609e3e2e692506 @tinogomes tinogomes committed Jul 5, 2011
Showing with 209 additions and 140 deletions.
  1. +2 −0 .gitignore
  2. +6 −2 Gemfile.lock
  3. +2 −1 Guardfile
  4. +30 −13 lib/rbraspag/bill.rb
  5. +1 −0 rbraspag.gemspec
  6. +168 −124 spec/bill_spec.rb
View
@@ -4,3 +4,5 @@ doc
tags
*.swp
.bundle
+.rvmrc
+
View
@@ -1,8 +1,8 @@
PATH
remote: .
specs:
- rbraspag (0.0.1)
- cs-httpi (= 0.9.5.2)
+ rbraspag (0.0.2)
+ cs-httpi (>= 0.9.5.2)
json
nokogiri
@@ -17,6 +17,9 @@ GEM
fakeweb (1.3.0)
guard (0.4.2)
thor (~> 0.14.6)
+ guard-bundler (0.1.3)
+ bundler (>= 1.0.0)
+ guard (>= 0.2.2)
guard-rspec (0.4.0)
guard (>= 0.4.0)
json (1.5.3)
@@ -50,6 +53,7 @@ PLATFORMS
DEPENDENCIES
fakeweb
+ guard-bundler
guard-rspec
rbraspag!
rspec
View
@@ -4,6 +4,7 @@ end
guard 'rspec', :version => 2, :bundler => false do
watch(%r{^spec/(.*)_spec\.rb$})
- watch(%r{^lib/(.*)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
+ watch(%r{^lib/rbraspag.rb$}) { "spec" }
+ watch(%r{^lib/rbraspag/(.*)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
View
@@ -1,5 +1,15 @@
module Braspag
class Bill
+ PAYMENT_METHOD = {
+ :bradesco => "06",
+ :cef => "07",
+ :hsbc => "08",
+ :bb => "09",
+ :real => "10",
+ :citibank => "13",
+ :itau => "14",
+ :unibanco => "26"
+ }
MAPPING = {
:merchant_id => "merchantId",
@@ -10,7 +20,8 @@ class Bill
:payment_method => "paymentMethod",
:number => "boletoNumber",
:instructions => "instructions",
- :expiration_date => "expirationDate"
+ :expiration_date => "expirationDate",
+ :emails => "emails"
}
def initialize(connection, params)
@@ -27,6 +38,10 @@ def initialize(connection, params)
ok?
end
+ def [](key)
+ @params[key]
+ end
+
def ok?
raise IncompleteParams if @params[:order_id].nil? || @params[:amount].nil? || @params[:payment_method].nil?
@@ -50,21 +65,27 @@ def ok?
end
unless @params[:expiration_date].nil?
- raise InvalidExpirationDate unless @params[:expiration_date].to_s.size == 8
+ date_regexp = /(0[1-9]|1[0-9]|2[0-9]|3[01])\/(0[1-9]|1[012])\/\d\d/
+ raise InvalidExpirationDate unless @params[:expiration_date].to_s =~ date_regexp
end
-
+
+ unless @params[:payment_method].is_a?(Symbol) && PAYMENT_METHOD[@params[:payment_method]]
+ raise InvalidPaymentMethod
+ end
+
true
end
def generate
- data = {}
- @params.each {|name, value|
- if MAPPING[name].nil?
- data[name] = value
+ data = MAPPING.inject({}) do |memo, k|
+ if k[0] == :payment_method
+ memo[k[1]] = PAYMENT_METHOD[@params[:payment_method]]
else
- data[MAPPING[name]] = value
+ memo[k[1]] = @params[k[0]] || "";
end
- }
+
+ memo
+ end
request = ::HTTPI::Request.new uri
request.body = data
@@ -107,7 +128,6 @@ def convert_to_map(document)
}
map.each do |keyForMap , keyValue|
-
if keyValue.is_a?(String) || keyValue.nil?
keyValue = keyForMap if keyValue.nil?
@@ -126,8 +146,5 @@ def convert_to_map(document)
map
end
-
end
-
end
-
View
@@ -26,5 +26,6 @@ Gem::Specification.new do |s|
s.add_development_dependency "fakeweb"
s.add_development_dependency "shoulda-matchers"
s.add_development_dependency "guard-rspec"
+ s.add_development_dependency "guard-bundler"
s.add_development_dependency "ruby-debug19"
end
Oops, something went wrong.

0 comments on commit b0d2f67

Please sign in to comment.