Class: GpxRuby::Gpx::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gpx_ruby/gpx/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gpx_ruby/gpx/parser.rb', line 7

def initialize(input)
  @xml = case input
           when File
             input.read
           when Tempfile
             input.read
           when String
             ::File.open(input, 'r') { |f| f.read }
           when Hash
             if input[:file_path] && input[:file_path].is_a?(String)
             ::File.open(input[:file_path], 'r') { |f| f.read }
             end
             if input[:xml] && input[:xml].is_a?(String)
               input[:xml]
             elsif input[:file] && (input[:file].is_a?(File) || input[:file].is_a?(Tempfile))
               input[:file].read
             else
               raise 'Error: invalid input!'
            end
           else
             raise 'Error: invalid input!'
         end
end

Instance Method Details

#parseObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gpx_ruby/gpx/parser.rb', line 32

def parse
  doc = Nokogiri::XML(@xml)
  @gpx_node = doc.at_xpath('//xmlns:gpx')

  creator = if @gpx_node.at_xpath('@creator').nil?
    ''
  else
    @gpx_node.at_xpath('@creator').value
  end

  version = if @gpx_node.at_xpath('@version').nil?
    0
  else
    @gpx_node.at_xpath('@version').value.to_f
  end

  properties = {
      creator: creator,
      version: version,
      tracks: []
  }

  # parse track elements
  tracks = Parsers::TrackParser.new(@gpx_node).parse
  properties = properties.merge(tracks: tracks)

  Document.new properties
end