Class: Cieloz::Requisicao

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/cieloz/requisicao.rb,
lib/cieloz/requisicao/dados_ec.rb,
lib/cieloz/requisicao/resposta.rb,
lib/cieloz/requisicao/resposta/erro.rb,
lib/cieloz/requisicao/resposta/transacao.rb

Direct Known Subclasses

RequisicaoTid, RequisicaoTransacao

Defined Under Namespace

Classes: DadosEc, Erro, Resposta, Transacao

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#attributes=, #dasherize_attr, included, #initialize

Instance Attribute Details

#dados_ecObject (readonly)

Returns the value of attribute dados_ec.



8
9
10
# File 'lib/cieloz/requisicao.rb', line 8

def dados_ec
  @dados_ec
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/cieloz/requisicao.rb', line 7

def id
  @id
end

#versaoObject

Returns the value of attribute versao.



7
8
9
# File 'lib/cieloz/requisicao.rb', line 7

def versao
  @versao
end

Instance Method Details

#attributesObject



10
11
12
# File 'lib/cieloz/requisicao.rb', line 10

def attributes
  { dados_ec: @dados_ec }
end

#parse(res) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cieloz/requisicao.rb', line 54

def parse res
  body = res.body.force_encoding("ISO-8859-1").encode "UTF-8"
  return Erro.from(body).tap { |e| e.codigo = res.code } if res.code != "200"

  root = Nokogiri::XML(body).root
  response_class =  case root.name
  when 'erro'       then Erro
  when 'transacao'  then Transacao
  end
  response_class.from body
end

#requested_xmlObject



66
67
68
69
70
71
72
73
# File 'lib/cieloz/requisicao.rb', line 66

def requested_xml
  if @xml
    doc = Nokogiri::XML @xml
    portador = '//requisicao-transacao//dados-portador'
    doc.xpath(portador).children.each {|node| node.content = "*" }
    doc.to_xml
  end
end

#submitObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cieloz/requisicao.rb', line 37

def submit
  @dados_ec = Cieloz::Configuracao.credenciais

  if valid?
    @id     = SecureRandom.uuid if id.blank?
    @versao = "1.2.1"           if versao.blank?

    http = Net::HTTP.new Cieloz::Configuracao.host, 443
    http.use_ssl = true
    http.open_timeout = 5 * 1000
    http.read_timeout = 30 * 1000
    http.ssl_version = :SSLv3 #http://stackoverflow.com/questions/11321403/openssl-trouble-with-ruby-1-9-3

    parse http.post Cieloz::Configuracao.path, "mensagem=#{to_xml}"
  end
end

#to_xmlObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cieloz/requisicao.rb', line 14

def to_xml
  x = Builder::XmlMarkup.new
  x.instruct!
  name = self.class.name.demodulize
  @xml = x.tag! name.underscore.dasherize, id: id, versao: versao do
    attributes.each { |attr, value|
      next if value.nil?

      if value.respond_to? :build_xml
        value.build_xml x
      elsif value.respond_to? :attributes
        x.tag! dasherize_attr(attr) do
          value.attributes.each do |attr, value|
            x.tag!(dasherize_attr(attr), value) unless value.blank?
          end
        end
      else
        x.tag! dasherize_attr(attr), value
      end
    }
  end
end