Class: BrNfe::Product::Response::Build::Base

Inherits:
ActiveModelBase show all
Defined in:
lib/br_nfe/product/response/build/base.rb

Instance Attribute Summary collapse

Attributes inherited from ActiveModelBase

#reference

Instance Method Summary collapse

Methods inherited from ActiveModelBase

#assign_attributes, #default_values, #initialize

Constructor Details

This class inherits a constructor from BrNfe::ActiveModelBase

Instance Attribute Details

#operationObject

Returns the value of attribute operation.



7
8
9
# File 'lib/br_nfe/product/response/build/base.rb', line 7

def operation
  @operation
end

#savon_responseObject

Returns the value of attribute savon_response.



6
7
8
# File 'lib/br_nfe/product/response/build/base.rb', line 6

def savon_response
  @savon_response
end

Instance Method Details

#body_xmlObject

Responsável por pegar o conteúdo presente dentro da tag Body da resposta Soap. É útil para facilitar a busca e manupilação do XML presente dentro do Body da resposta Soap

# Type: Nokogiri::XML::Document



98
99
100
101
102
103
# File 'lib/br_nfe/product/response/build/base.rb', line 98

def body_xml
  @body_xml ||= parse_nokogiri_xml( response_xml.xpath(
    paths[:body_xml][:path], 
    paths[:body_xml][:namespaces]
  ).try(:children).try(:to_xml) )
end

#doc_original_xmlObject

Responsável por converter a string xml a ser modificado em um Nokogiri::XML::Document É útil para evitar ficar dando o parse no xml toda vez que precisa ser utilizado.

# Type: Nokogiri::XML::Document



126
127
128
# File 'lib/br_nfe/product/response/build/base.rb', line 126

def doc_original_xml
  @doc_original_xml ||= parse_nokogiri_xml( original_xml )
end

#header_xmlObject

Responsável por pegar o conteúdo presente dentro da tag Header da resposta Soap. É útil para facilitar a busca e manupilação do XML presente dentro do Header da resposta Soap

# Type: Nokogiri::XML::Document



112
113
114
115
116
117
# File 'lib/br_nfe/product/response/build/base.rb', line 112

def header_xml
  @header_xml ||= parse_nokogiri_xml( response_xml.xpath(
    paths[:header_xml][:path], 
    paths[:header_xml][:namespaces]
  ).try(:children).try(:to_xml) )
end

#nf_xmlnsObject

XMLNS PADRÃO DA NOTA FISCAL



142
143
144
# File 'lib/br_nfe/product/response/build/base.rb', line 142

def nf_xmlns
  'http://www.portalfiscal.inf.br/nfe'
end

#nfe_settingsObject



13
14
15
# File 'lib/br_nfe/product/response/build/base.rb', line 13

def nfe_settings
  operation.nfe_settings
end

#original_xmlObject



9
10
11
# File 'lib/br_nfe/product/response/build/base.rb', line 9

def original_xml
  @original_xml ||= operation.original_xml || operation.xml_builder
end

#responseObject

Responsável por instanciar o objeto de resposta contendo os valores pertinentes a cada operação.



20
21
22
23
24
25
26
27
28
# File 'lib/br_nfe/product/response/build/base.rb', line 20

def response
  return @response if @response
  @response = response_class.new({
    soap_xml: savon_response.try(:xml).try(:force_encoding, 'UTF-8'),
    request_status: :success
  })
  @response.assign_attributes(specific_attributes)
  @response
end

#response_classObject

Responsável por definir a classe da resposta que será instânciada para cada operação. Esse método deve ser sobrescrito para cada operação.

Type: Class



36
37
38
# File 'lib/br_nfe/product/response/build/base.rb', line 36

def response_class
  BrNfe::Product::Response::Base
end

#response_xmlObject

Contém o XML completo da resposta Soap. É útil para evitar ficar dando o parse no xml toda vez que precisa ser utilizado.

# Type: Nokogiri::XML::Document



136
137
138
# File 'lib/br_nfe/product/response/build/base.rb', line 136

def response_xml
  @response_xml ||= parse_nokogiri_xml(savon_response.try(:xml))
end

#specific_attributesObject

É utilizado para setar os atributos específicos para o retorno de cada operação. É útil para evitar sobrescrever e duplicar código do método response

Type: Hash



47
48
49
# File 'lib/br_nfe/product/response/build/base.rb', line 47

def specific_attributes
  {}
end

#url_xmlns_retornoObject

Responsável por identificar o xmlns principal da resposta. Esse valor é utilizado para dar o Start para encontrar o conteúdo na arvore de tags xml.

Type: String



57
58
59
# File 'lib/br_nfe/product/response/build/base.rb', line 57

def url_xmlns_retorno
  @url_xmlns_retorno ||= header_xml.collect_namespaces['xmlns'] || operation.url_xmlns
end

#xml_versionObject

Responsável por trazer qual a versão do layout o XML da resposta foi feito. Irá converter a versão em Symbol e alterar o ponto(.) em underline(_). Também irá acrescentar um ‘v’ na frente da versão. Irá pegar a versão a partir da tag ‘versaoDados’ presente no Header da requisição Soap. É útil para saber como pegar as informações do XML, pois como cada

vers

Exemplos:

Vers

# Type: Symbol



79
80
81
82
83
# File 'lib/br_nfe/product/response/build/base.rb', line 79

def xml_version
  return @xml_version if @xml_version
  version = header_xml.xpath('//nf:nfeCabecMsg/nf:versaoDados', nf: url_xmlns_retorno).text
  @xml_version = "v#{version.gsub('.','_')}".to_sym
end

#xml_version_strObject

Versão do XML em forma de String ex: ‘3.10’



86
87
88
89
# File 'lib/br_nfe/product/response/build/base.rb', line 86

def xml_version_str
  return @xml_version_str if @xml_version_str
  @xml_version_str = header_xml.xpath('//nf:nfeCabecMsg/nf:versaoDados', nf: url_xmlns_retorno).text
end