Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

extract http request code to a module

  • Loading branch information...
commit aea0a2f0a377d4a0a670e49a24a4924481a476a0 1 parent 9a58faa
@rafaelss authored
View
1  lib/cielo.rb
@@ -3,6 +3,7 @@
module Cielo
autoload :Configuration, "cielo/configuration"
+ autoload :HTTP, "cielo/http"
autoload :Error, "cielo/error"
autoload :Shop, "cielo/shop"
View
17 lib/cielo/authorization_request.rb
@@ -1,27 +1,12 @@
module Cielo
class AuthorizationRequest
include HappyMapper
+ include HTTP
tag "requisicao-autorizacao-tid"
attribute :id, Integer, :on_save => proc { |value| value.to_s }
attribute :version, String, :tag => "versao", :on_save => proc { |value| value ? value : "1.1.1" }
element :tid, String
has_one :shop, Shop
-
- def authorize
- http = Net::HTTP.new(Cielo.configuration.host, Cielo.configuration.port)
- http.use_ssl = true
- http.open_timeout = 10 * 1000
- http.read_timeout = 40 * 1000
- Cielo.logger.info(http.inspect)
-
- post_body = "mensagem=#{to_xml}"
- Cielo.logger.info(post_body)
-
- response = http.request_post(Cielo.configuration.path, post_body)
- Cielo.logger.info(response.body)
-
- Transaction.parse(response.body, :single => true)
- end
end
end
View
17 lib/cielo/cancelation_request.rb
@@ -1,27 +1,12 @@
module Cielo
class CancelationRequest
include HappyMapper
+ include HTTP
tag "requisicao-cancelamento"
attribute :id, Integer, :on_save => proc { |value| value.to_s }
attribute :version, String, :tag => "versao", :on_save => proc { |value| value ? value : "1.1.1" }
element :tid, String
has_one :shop, Shop
-
- def cancel
- http = Net::HTTP.new(Cielo.configuration.host, Cielo.configuration.port)
- http.use_ssl = true
- http.open_timeout = 10 * 1000
- http.read_timeout = 40 * 1000
- Cielo.logger.info(http.inspect)
-
- post_body = "mensagem=#{to_xml}"
- Cielo.logger.info(post_body)
-
- response = http.request_post(Cielo.configuration.path, post_body)
- Cielo.logger.info(response.body)
-
- Transaction.parse(response.body, :single => true)
- end
end
end
View
17 lib/cielo/capture_request.rb
@@ -1,27 +1,12 @@
module Cielo
class CaptureRequest
include HappyMapper
+ include HTTP
tag "requisicao-captura"
attribute :id, Integer, :on_save => proc { |value| value.to_s }
attribute :version, String, :tag => "versao", :on_save => proc { |value| value ? value : "1.1.1" }
element :tid, String
has_one :shop, Shop
-
- def capture
- http = Net::HTTP.new(Cielo.configuration.host, Cielo.configuration.port)
- http.use_ssl = true
- http.open_timeout = 10 * 1000
- http.read_timeout = 40 * 1000
- Cielo.logger.info(http.inspect)
-
- post_body = "mensagem=#{to_xml}"
- Cielo.logger.info(post_body)
-
- response = http.request_post(Cielo.configuration.path, post_body)
- Cielo.logger.info(response.body)
-
- Transaction.parse(response.body, :single => true)
- end
end
end
View
21 lib/cielo/http.rb
@@ -0,0 +1,21 @@
+require "net/http"
+
+module Cielo
+ module HTTP
+ def request
+ http = Net::HTTP.new(Cielo.configuration.host, Cielo.configuration.port)
+ http.use_ssl = true
+ http.open_timeout = 10 * 1000
+ http.read_timeout = 40 * 1000
+ Cielo.logger.info(http.inspect)
+
+ post_body = "mensagem=#{to_xml}"
+ Cielo.logger.info(post_body)
+
+ response = http.request_post(Cielo.configuration.path, post_body)
+ Cielo.logger.info(response.body)
+
+ Transaction.parse(response.body, :single => true) || Error.parse(response.body, :single => true)
+ end
+ end
+end
View
19 lib/cielo/transaction_request.rb
@@ -1,8 +1,7 @@
-require "net/http"
-
module Cielo
class TransactionRequest
include HappyMapper
+ include HTTP
tag "requisicao-transacao"
attribute :id, Integer, :on_save => proc { |value| value.to_s }
@@ -14,21 +13,5 @@ class TransactionRequest
element :return_url, String, :tag => "url-retorno"
element :authorize, Integer, :tag => "autorizar"
element :capture, Boolean, :tag => "capturar", :on_save => proc { |value| value.to_s }
-
- def create
- http = Net::HTTP.new(Cielo.configuration.host, Cielo.configuration.port)
- http.use_ssl = true
- http.open_timeout = 10 * 1000
- http.read_timeout = 40 * 1000
- Cielo.logger.info(http.inspect)
-
- post_body = "mensagem=#{to_xml}"
- Cielo.logger.info(post_body)
-
- response = http.request_post(Cielo.configuration.path, post_body)
- Cielo.logger.info(response.body)
-
- Transaction.parse(response.body, :single => true)
- end
end
end
View
2  spec/cielo/authorization_request_spec.rb
@@ -36,7 +36,7 @@
subject.shop = shop
subject.id = 1
subject.tid = "100699306904CC7E1001"
- transaction = subject.authorize
+ transaction = subject.request
transaction.authorization.code.should == 6
transaction.authorization.message.should == "Transação autorizada"
View
2  spec/cielo/cancelation_request_spec.rb
@@ -36,7 +36,7 @@
subject.shop = shop
subject.id = 1
subject.tid = "100699306904CC7E1001"
- transaction = subject.cancel
+ transaction = subject.request
transaction.tid.should == "100699306904CC7E1001"
transaction.pan.should == "IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0="
View
2  spec/cielo/capture_request_spec.rb
@@ -36,7 +36,7 @@
subject.shop = shop
subject.id = 1
subject.tid = "100699306904CC7E1001"
- transaction = subject.capture
+ transaction = subject.request
transaction.tid.should == "100699306904CC7E1001"
transaction.pan.should == "IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0="
View
2  spec/cielo/transaction_request_spec.rb
@@ -78,7 +78,7 @@
subject.return_url = "http://minha.loja.com/pedido/285813768"
subject.authorize = 2
subject.capture = false
- transaction = subject.create
+ transaction = subject.request
transaction.tid.should == "100699306904CC7E1001"
transaction.pan.should == "IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0="
Please sign in to comment.
Something went wrong with that request. Please try again.