# encoding: utf-8 module GpxTrackGenerator # Track class Track private attr_reader :files, :name, :reverse, :single_segment public def initialize(files, name:, reverse:, single_segment:) @files = files @name = name @reverse = reverse @single_segment = single_segment 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 gpx_files = if reverse files.reverse else files end document.child << metadata document.child << document.create_element('trk') document.css('trk').first << document.create_element('name') document.css('name').first.content = name if single_segment document.css('trk').first << document.create_element('trkseg') gpx_files.each_with_object(document.css('trk').first) do |e, a| a << "" document.css('trkseg').last << (reverse ? e.nodes.reverse : e.nodes) end else 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 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