Class: RETS::Base::SAXMetadata

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/rets/base/sax_metadata.rb

Overview

SAX parser for the GetMetadata call.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ SAXMetadata

Returns a new instance of SAXMetadata.



5
6
7
8
9
# File 'lib/rets/base/sax_metadata.rb', line 5

def initialize(block)
  @rets_data = {:delimiter => "\t"}
  @block = block
  @parent = {}
end

Instance Attribute Details

#rets_dataObject

Returns the value of attribute rets_data.



3
4
5
# File 'lib/rets/base/sax_metadata.rb', line 3

def rets_data
  @rets_data
end

Instance Method Details

#characters(string) ⇒ Object



39
40
41
# File 'lib/rets/base/sax_metadata.rb', line 39

def characters(string)
  @buffer << string if @current_tag
end

#end_element(tag) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rets/base/sax_metadata.rb', line 43

def end_element(tag)
  return unless @current_tag

  if @current_tag == "COLUMNS"
    @columns = @buffer.split(@rets_data[:delimiter])
  elsif tag == "DATA"
    data = {}

    list = @buffer.split(@rets_data[:delimiter])
    list.each_index do |index|
      next if @columns[index].nil? or @columns[index] == ""
      data[@columns[index]] = list[index]
    end

    @parent[:data].push(data)
  elsif tag == @parent[:tag]
    @block.call(@parent[:name], @parent[:attrs], @parent[:data])
    @parent[:tag] = nil
  end
end

#start_element(tag, attrs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rets/base/sax_metadata.rb', line 11

def start_element(tag, attrs)
  @current_tag = nil

  # Figure out if the request is a success
  if tag == "RETS"
    @rets_data[:code], @rets_data[:text] = attrs.first.last, attrs.last.last
    if @rets_data[:code] != "0" and @rets_data[:code] != "20201"
      raise RETS::APIError.new("#{@rets_data[:code]}: #{@rets_data[:text]}", @rets_data[:code], @rets_data[:text])
    end

  elsif tag == "SYSTEM"
    @rets_data[:system_id] = attrs.first.last

  # Parsing data
  elsif tag == "COLUMNS" or tag == "DATA"
    @buffer = ""
    @current_tag = tag

  # Start of the parent we're working with
  elsif tag =~ /^METADATA-(.+)/
    @parent[:tag] = tag
    @parent[:name] = $1
    @parent[:data] = []
    @parent[:attrs] = {}
    attrs.each {|attr| @parent[:attrs][attr[0]] = attr[1] }
  end
end