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



94
95
96
# File 'lib/br_nfe/product/response/build/base.rb', line 94

def body_xml
	@body_xml ||= parse_nokogiri_xml( response_xml.xpath('//soap:Envelope/soap:Body', 'xmlns:soap' => "http://www.w3.org/2003/05/soap-envelope").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



116
117
118
# File 'lib/br_nfe/product/response/build/base.rb', line 116

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



105
106
107
# File 'lib/br_nfe/product/response/build/base.rb', line 105

def header_xml
	@header_xml ||= parse_nokogiri_xml( response_xml.xpath('//soap:Envelope/soap:Header', 'xmlns:soap' => "http://www.w3.org/2003/05/soap-envelope").try(:children).try(:to_xml) )
end

#nf_xmlnsObject

XMLNS PADRÃO DA NOTA FISCAL



132
133
134
# File 'lib/br_nfe/product/response/build/base.rb', line 132

def nf_xmlns
	'http://www.portalfiscal.inf.br/nfe'
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.



16
17
18
19
20
21
22
23
24
# File 'lib/br_nfe/product/response/build/base.rb', line 16

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



32
33
34
# File 'lib/br_nfe/product/response/build/base.rb', line 32

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



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

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



43
44
45
# File 'lib/br_nfe/product/response/build/base.rb', line 43

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



53
54
55
# File 'lib/br_nfe/product/response/build/base.rb', line 53

def url_xmlns_retorno
	@url_xmlns_retorno ||= header_xml.collect_namespaces['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ão pode ter uma estrutura diferente, o conteúdo desse método
irá auxiliar na decisão de como buscar o conteúdo.

Exemplos:

Versão do XML      Retorno
    '1.00'     =>    :v1_00
    '2.01'     =>    :v2_01
    '3.10'     =>    :v3_10

# Type: Symbol



75
76
77
78
79
# File 'lib/br_nfe/product/response/build/base.rb', line 75

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’



82
83
84
85
# File 'lib/br_nfe/product/response/build/base.rb', line 82

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