Class: Nfe::Models::NFe

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Helpers::XmlModel::NfeModel
Defined in:
lib/nfe/models/NFe.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::XmlModel::NfeModel

#add_elements, #check_present, included, #parse_elements, #present?, #validate_optional_attributes, #validate_required_attributes

Constructor Details

#initializeNFe

Returns a new instance of NFe.



11
12
13
14
# File 'lib/nfe/models/NFe.rb', line 11

def initialize
  @infNFe = Nfe::Models::InfNFe.new
  @signature = Nfe::Models::Signature.new
end

Instance Attribute Details

#infNFeObject

Returns the value of attribute infNFe.



7
8
9
# File 'lib/nfe/models/NFe.rb', line 7

def infNFe
  @infNFe
end

#signatureObject

Returns the value of attribute signature.



7
8
9
# File 'lib/nfe/models/NFe.rb', line 7

def signature
  @signature
end

Class Method Details

.namespaceObject



16
17
18
# File 'lib/nfe/models/NFe.rb', line 16

def self.namespace
  "http://www.portalfiscal.inf.br/nfe"
end

Instance Method Details

#parse!(xml) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/nfe/models/NFe.rb', line 20

def parse!(xml)
  doc = Nokogiri::XML(xml)

  @infNFe.parse!(doc.css("infNFe").to_xml)

  sign = doc.xpath("//ds:Signature", ds: Signature.namespace).to_xml
  @signature.parse!(sign)
end

#to_xmlObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nfe/models/NFe.rb', line 29

def to_xml
  xml = Builder::XmlMarkup.new

  xml.NFe(xmlns: Nfe::Models::NFe.namespace) do |nfe|

    nfe.infNFe({versao: infNFe.versao, Id: infNFe.id}) do |node|
      node << @infNFe.to_xml
    end

    nfe.Signature(xmlns: Signature.namespace) do |node|
      node << @signature.value
    end
  end

  xml.target!
end