Permalink
Browse files

add configuration class so host, port and request path can be changed

  • Loading branch information...
rafaelss committed May 11, 2012
1 parent 91441bb commit 341ca5bd4e4e7ff6d4f73767fcd3e27c6092e7e2
Showing with 63 additions and 2 deletions.
  1. +11 −0 lib/cielo.rb
  2. +11 −0 lib/cielo/configuration.rb
  3. +2 −2 lib/cielo/transaction_request.rb
  4. +26 −0 spec/cielo/configuration_spec.rb
  5. +13 −0 spec/cielo_spec.rb
View
@@ -8,4 +8,15 @@ module Cielo
autoload :Card, "cielo/card"
autoload :TransactionRequest, "cielo/transaction_request"
autoload :Transaction, "cielo/transaction"
+ autoload :Configuration, "cielo/configuration"
+
+ class << self
+ def configuration
+ @configuration ||= Configuration.new
+ end
+
+ def configure
+ yield configuration if block_given?
+ end
+ end
end
View
@@ -0,0 +1,11 @@
+module Cielo
+ class Configuration
+ attr_accessor :host, :port, :path
+
+ def initialize
+ @host = "qasecommerce.cielo.com.br"
+ @port = 443
+ @path = "/servicos/ecommwsec.do"
+ end
+ end
+end
@@ -16,11 +16,11 @@ class TransactionRequest
element :capture, Boolean, :tag => "capturar", :on_save => proc { |value| value.to_s }
def create
- http = Net::HTTP.new("qasecommerce.cielo.com.br", 443)
+ http = Net::HTTP.new(Cielo.configuration.host, Cielo.configuration.port)
http.use_ssl = true
http.open_timeout = 10 * 1000
http.read_timeout = 40 * 1000
- response = http.request_post("/servicos/ecommwsec.do", "mensagem=#{to_xml}")
+ response = http.request_post(Cielo.configuration.path, "mensagem=#{to_xml}")
Transaction.parse(response.body, :single => true)
end
end
@@ -0,0 +1,26 @@
+require "spec_helper"
+
+describe Cielo::Configuration do
+ subject { described_class.new }
+
+ its(:host) { should == "qasecommerce.cielo.com.br" }
+ its(:port) { should == 443 }
+ its(:path) { should == "/servicos/ecommwsec.do" }
+
+ context "defining new values" do
+ it "changes the host" do
+ subject.host = "a.com"
+ subject.host.should == "a.com"
+ end
+
+ it "changes the port" do
+ subject.host = 80
+ subject.host.should == 80
+ end
+
+ it "changes the path" do
+ subject.host = "/something_else"
+ subject.host.should == "/something_else"
+ end
+ end
+end
View
@@ -0,0 +1,13 @@
+require "spec_helper"
+
+describe Cielo do
+ it "returns a configuration instance" do
+ Cielo.configuration.should be_instance_of(Cielo::Configuration)
+ end
+
+ it "yields a configuration instance" do
+ configuration = nil
+ Cielo.configure { |c| configuration = c }
+ configuration.should be_instance_of(Cielo::Configuration)
+ end
+end

0 comments on commit 341ca5b

Please sign in to comment.