Class: GPX::Gpx

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

Overview

Docu

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Gpx

Returns a new instance of Gpx.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gpx_kml/gpx.rb', line 12

def initialize(file_path)
  return unless correct_path?(file_path) && (File.size(file_path) < 10_000_000)

  @gpx = Nokogiri::XML(File.open(file_path))
  return unless valid?

  @file_name = File.basename(file_path)
  @name = _name
  @author = @gpx.xpath('/xmlns:gpx/xmlns:metadata/xmlns:author/xmlns:name/text()').to_s
  @link = @gpx.xpath('/xmlns:gpx/xmlns:metadata/xmlns:link/@href').to_s
  @tracks = _tracks if tracks?
  @routes = _routes if routes?
  @points = _points if points?
  @points_length = _points_length
  @routes_length = _routes_length
  @tracks_length = _tracks_length
end

Instance Attribute Details

#authorObject (readonly)

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx



31
32
33
# File 'lib/gpx_kml/gpx.rb', line 31

def author
  @author
end

#file_nameObject (readonly)

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx



31
32
33
# File 'lib/gpx_kml/gpx.rb', line 31

def file_name
  @file_name
end

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx



31
32
33
# File 'lib/gpx_kml/gpx.rb', line 31

def link
  @link
end

#nameObject (readonly)

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx



31
32
33
# File 'lib/gpx_kml/gpx.rb', line 31

def name
  @name
end

#pointsObject (readonly)

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx



31
32
33
# File 'lib/gpx_kml/gpx.rb', line 31

def points
  @points
end

#points_lengthObject (readonly)

access in readonly to the quantity of points/routes/tracks in the gpx



34
35
36
# File 'lib/gpx_kml/gpx.rb', line 34

def points_length
  @points_length
end

#routesObject (readonly)

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx



31
32
33
# File 'lib/gpx_kml/gpx.rb', line 31

def routes
  @routes
end

#routes_lengthObject (readonly)

access in readonly to the quantity of points/routes/tracks in the gpx



34
35
36
# File 'lib/gpx_kml/gpx.rb', line 34

def routes_length
  @routes_length
end

#tracksObject (readonly)

used in development of the gem, could be useful going forward to add functionalities attr_reader :gpx



31
32
33
# File 'lib/gpx_kml/gpx.rb', line 31

def tracks
  @tracks
end

#tracks_lengthObject (readonly)

access in readonly to the quantity of points/routes/tracks in the gpx



34
35
36
# File 'lib/gpx_kml/gpx.rb', line 34

def tracks_length
  @tracks_length
end

Instance Method Details

#_nameObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/gpx_kml/gpx.rb', line 63

def _name
  if valid?
    name = @gpx.xpath('/xmlns:gpx/xmlns:metadata/xmlns:name/text()').to_s
    return alt_name if name.empty?

    return name
  end
  puts 'return empty string'
  ''
end

#descriptionObject



74
75
76
77
78
# File 'lib/gpx_kml/gpx.rb', line 74

def description
  return @gpx.xpath('//xmlns:metadata/xmlns:desc/text()').to_s if valid?

  ''
end

#gpx?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/gpx_kml/gpx.rb', line 36

def gpx?
  !@gpx.nil? && !@gpx.xpath('/xmlns:gpx').empty?
end

#points?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/gpx_kml/gpx.rb', line 57

def points?
  return true unless @gpx.xpath('//xmlns:wpt').empty?

  false
end

#routes?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/gpx_kml/gpx.rb', line 45

def routes?
  return true unless @gpx.xpath('//xmlns:rte').empty?

  false
end

#tracks?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/gpx_kml/gpx.rb', line 51

def tracks?
  return true unless @gpx.xpath('//xmlns:trk').empty?

  false
end

#valid?Boolean

For a gpx file to be valid it must only have a waypoint, a route or a track

Returns:

  • (Boolean)


41
42
43
# File 'lib/gpx_kml/gpx.rb', line 41

def valid?
  gpx? && (tracks? || routes? || points?)
end