Class: ETL::Parser::NokogiriXmlParser

Inherits:
Parser show all
Defined in:
lib/etl/parser/nokogiri_xml_parser.rb

Defined Under Namespace

Classes: Field

Instance Attribute Summary

Attributes inherited from Parser

#options, #source

Instance Method Summary collapse

Methods inherited from Parser

class_for_name

Constructor Details

#initialize(source, options = {}) ⇒ NokogiriXmlParser

Initialize the parser

  • source: The Source object

  • options: Parser options Hash



11
12
13
14
# File 'lib/etl/parser/nokogiri_xml_parser.rb', line 11

def initialize(source, options={})
  super
  configure
end

Instance Method Details

#eachObject

Returns each row



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/etl/parser/nokogiri_xml_parser.rb', line 17

def each
  Dir.glob(file).each do |source|

    doc = nil

    gzip = false
    magic = "1F8B".to_i(base=16)  # Check for gzip archives
    if File.exist?(source)
      gzip = true if magic == (
        File.open(source).read(2).unpack("H2H2").to_s.to_i(base=16))
    end

    if gzip
      doc = Nokogiri::XML(Zlib::GzipReader.open(source))
    else
      doc = Nokogiri::XML(open(source))
    end
    
    doc.xpath(@collection_xpath).each do |nodeset|
      row = {}

      fields.each do |f|
        value = nodeset.xpath(f.xpath).text
        row[f.name] = value
      end
      yield row
    end

  end
end

#fieldsObject

Get an array of defined fields



49
50
51
# File 'lib/etl/parser/nokogiri_xml_parser.rb', line 49

def fields
  @fields ||= []
end