Class: Cielo::Connection
- Inherits:
-
Object
- Object
- Cielo::Connection
- Defined in:
- lib/cielo/connection.rb
Instance Attribute Summary collapse
-
#chave_acesso ⇒ Object
readonly
Returns the value of attribute chave_acesso.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#numero_afiliacao ⇒ Object
readonly
Returns the value of attribute numero_afiliacao.
Instance Method Summary collapse
-
#initialize(numero_afiliacao = Cielo.numero_afiliacao, chave_acesso = Cielo.chave_acesso) ⇒ Connection
constructor
A new instance of Connection.
- #make_request!(message) ⇒ Object
- #parse_elements(elements) ⇒ Object
- #parse_response(response) ⇒ Object
- #request!(params = {}) ⇒ Object
- #xml_builder(group_name, target = :after, &block) ⇒ Object
Constructor Details
#initialize(numero_afiliacao = Cielo.numero_afiliacao, chave_acesso = Cielo.chave_acesso) ⇒ Connection
Returns a new instance of Connection.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/cielo/connection.rb', line 9 def initialize numero_afiliacao = Cielo.numero_afiliacao, chave_acesso = Cielo.chave_acesso @environment = eval(Cielo.environment.to_s.capitalize) @numero_afiliacao = numero_afiliacao @chave_acesso = chave_acesso port = 443 # if behind a proxy so set it up here Cielo.proxy.empty? ? @http = Net::HTTP.new(@environment::BASE_URL,port) : @http = Net::HTTP.new(@environment::BASE_URL,port, Cielo.proxy[:host], Cielo.proxy[:port], Cielo.proxy[:login], Cielo.proxy[:password]) @http.use_ssl = true @http.open_timeout = 10*1000 @http.read_timeout = 40*1000 end |
Instance Attribute Details
#chave_acesso ⇒ Object (readonly)
Returns the value of attribute chave_acesso.
7 8 9 |
# File 'lib/cielo/connection.rb', line 7 def chave_acesso @chave_acesso end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
5 6 7 |
# File 'lib/cielo/connection.rb', line 5 def environment @environment end |
#numero_afiliacao ⇒ Object (readonly)
Returns the value of attribute numero_afiliacao.
6 7 8 |
# File 'lib/cielo/connection.rb', line 6 def numero_afiliacao @numero_afiliacao end |
Instance Method Details
#make_request!(message) ⇒ Object
48 49 50 51 52 |
# File 'lib/cielo/connection.rb', line 48 def make_request!() params = { :mensagem => .target! } result = self.request! params parse_response(result) end |
#parse_elements(elements) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cielo/connection.rb', line 64 def parse_elements(elements) map={} elements.each do |element| element_map = {} element_map = element.text if element.elements.empty? && element.attributes.empty? element_map.merge!("value" => element.text) if element.elements.empty? && !element.attributes.empty? element_map.merge!(parse_elements(element.elements)) unless element.elements.empty? map.merge!(element.name => element_map) end map.symbolize_keys end |
#parse_response(response) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/cielo/connection.rb', line 54 def parse_response(response) case response when Net::HTTPSuccess document = REXML::Document.new(response.body) parse_elements(document.elements) else {:erro => { :codigo => "000", :mensagem => "Impossível contactar o servidor"}} end end |
#request!(params = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cielo/connection.rb', line 23 def request!(params={}) str_params = '' params.each do |key, value| str_params+="&" unless str_params.empty? str_params+="#{key}=#{value}" end str_params = str_params.remove('<to_s/>') @http.request_post(self.environment::WS_PATH, str_params) end |
#xml_builder(group_name, target = :after, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cielo/connection.rb', line 34 def xml_builder(group_name, target=:after, &block) xml = Builder::XmlMarkup.new xml.instruct! :xml, :version=>"1.0", :encoding=>"ISO-8859-1" xml.tag!(group_name, :id => "#{Time.now.to_i}", :versao => "1.2.1") do block.call(xml) if target == :before xml.tag!("dados-ec") do xml.numero @numero_afiliacao #Cielo.numero_afiliacao xml.chave @chave_acesso #Cielo.chave_acesso end block.call(xml) if target == :after end xml end |