class Cielo::Connection

Attributes

chave_acesso[R]
environment[R]
numero_afiliacao[R]

Public Class Methods

new(numero_afiliacao = Cielo.numero_afiliacao, chave_acesso = Cielo.chave_acesso) click to toggle source
# 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

Public Instance Methods

make_request!(message) click to toggle source
# File lib/cielo/connection.rb, line 48
def make_request!(message)
  params = { :mensagem => message.target! }
  result = self.request! params
  parse_response(result)
end
parse_elements(elements) click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# 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