Class: Embulk::Parser::RecordBinder

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/parser/xpath.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, schema, namespaces, on_new_record) ⇒ RecordBinder

Returns a new instance of RecordBinder.



46
47
48
49
50
51
52
53
# File 'lib/embulk/parser/xpath.rb', line 46

def initialize(root, schema, namespaces, on_new_record)
  @root = root
  @schema = schema
  @on_new_record = on_new_record
  @namespaces = namespaces
  clear
  super()
end

Instance Method Details

#characters(string) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/embulk/parser/xpath.rb', line 79

def characters(string)
  return if !@enter || string.strip.size == 0 || @current_element_name.nil?
  val = @current_data[@current_element_name]
  val = "" if val.nil?
  val += string
  @current_data[@current_element_name] = val
end

#clearObject



55
56
57
58
59
60
# File 'lib/embulk/parser/xpath.rb', line 55

def clear
  @find_route_idx = 0
  @enter = false
  @current_element_name = nil
  @current_data = new_map_by_schema
end

#parse(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/embulk/parser/xpath.rb', line 62

def parse(data)
  doc = Nokogiri::XML(data)

  items = doc.xpath(@root, @namespaces)
  items.each do |item|
    @current_data = new_map_by_schema
   
    @schema.each do |key,value|
      doc.xpath(key, @namespaces).each do |item|
        @current_data[key] = item.text
      end
    end
    
    @on_new_record.call(@current_data.map{|k, v| v})
  end
end