# encoding: utf-8 module GpxTrackGenerator # Track class Track private attr_reader :files, :name, :reverse public def initialize(files, name:, reverse:) @files = files @name = name @reverse = reverse end def to_s build_document end private def creator 'gpx_track_generator' end def creator_url 'https://github.com/maxmeyer/gpx_track_generator' end def build_document document.child << metadata document.child << document.create_element('trk') document.css('trk').first << document.create_element('name') document.css('name').first.content = name files.each_with_object(document.css('trk').first) do |e, a| a << "" a << document.create_element('trkseg') a.css('trkseg').last << (reverse ? e.nodes.reverse : e.nodes) end document.human end def document @document ||= Nokogiri::XML( <<-EOS.strip_heredoc EOS ) end def metadata @metadata ||= Nokogiri::XML::DocumentFragment.parse <<-EOS.strip_heredoc GPX file generated by #{creator} #{creator} EOS end end end