Class: OpenStreetMap::Parser

Inherits:
HTTParty::Parser
  • Object
show all
Includes:
LibXML::XML::SaxParser::Callbacks
Defined in:
lib/open_street_map/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



6
7
8
# File 'lib/open_street_map/parser.rb', line 6

def collection
  @collection
end

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/open_street_map/parser.rb', line 6

def context
  @context
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/open_street_map/parser.rb', line 6

def description
  @description
end

#langObject

Returns the value of attribute lang.



6
7
8
# File 'lib/open_street_map/parser.rb', line 6

def lang
  @lang
end

Instance Method Details

#on_characters(chars) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/open_street_map/parser.rb', line 61

def on_characters(chars)
  if @context.class.name == 'OpenStreetMap::User'
    if @description
      @context.description = chars
    end
    if @lang
      @context.languages << chars
    end
  end
end

#on_end_documentObject

:nodoc:



33
34
35
# File 'lib/open_street_map/parser.rb', line 33

def on_end_document     # :nodoc:
  end_document if respond_to?(:end_document)
end

#on_end_element(name) ⇒ Object

:nodoc:



53
54
55
56
57
58
59
# File 'lib/open_street_map/parser.rb', line 53

def on_end_element(name)   # :nodoc:
  case name
  when 'description'  then @description = false
  when 'lang'         then @lang        = false
  when 'changeset'    then _end_changeset
  end
end

#on_start_documentObject

:nodoc:



28
29
30
31
# File 'lib/open_street_map/parser.rb', line 28

def on_start_document   # :nodoc:
  @collection = []
  start_document if respond_to?(:start_document)
end

#on_start_element(name, attr_hash) ⇒ Object

:nodoc:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/open_street_map/parser.rb', line 37

def on_start_element(name, attr_hash)   # :nodoc:
  case name
  when 'node'         then _start_node(attr_hash)
  when 'way'          then _start_way(attr_hash)
  when 'relation'     then _start_relation(attr_hash)
  when 'changeset'    then _start_changeset(attr_hash)
  when 'user'         then _start_user(attr_hash)
  when 'tag'          then _tag(attr_hash)
  when 'nd'           then _nd(attr_hash)
  when 'member'       then _member(attr_hash)
  when 'home'         then _home(attr_hash)
  when 'description'  then @description = true
  when 'lang'         then @lang        = true
  end
end

#parseObject



8
9
10
11
12
13
14
15
# File 'lib/open_street_map/parser.rb', line 8

def parse
  return nil if body.nil? || body.empty?
  if supports_format?
    self.send(format) # This is a hack, cause the xml format would not be recognized ways, but for nodes and relations
  else
    body
  end
end

#plainObject



24
25
26
# File 'lib/open_street_map/parser.rb', line 24

def plain
  body
end

#xmlObject



17
18
19
20
21
22
# File 'lib/open_street_map/parser.rb', line 17

def xml
  @parser = LibXML::XML::SaxParser.string(body)
  @parser.callbacks = self
  @parser.parse
  @collection.empty? ? @context : @collection
end