class TrackList::TrackParser

The purpose of this class is to parse a single track and return a formatted string.

Public Class Methods

new(track) click to toggle source

When initializing the class, you must pass in a file path to the track you want parsed.

@param [String] track The track to be parsed.

# File lib/track_list/track_parser.rb, line 18
def initialize(track)
    @track = track
end

Public Instance Methods

parse() click to toggle source

The bulk of the work is done in this method. Calling this method returns a parsed track in a formatted string.

@return [String] if valid audio file.

# File lib/track_list/track_parser.rb, line 28
def parse
    TagLib::FileRef.open(@track) do |fileref|
      unless fileref.null?
        tag = fileref.tag
        properties = fileref.audio_properties
        time_conversion = TrackList::TimeConverter.new(properties.length)
        length = time_conversion.format_time

        template_parser = TrackList::TemplateParser.new('~/.track_list.yaml')
        template = template_parser.load

        template_strings = {
          '%TRACK%' => tag.track.to_s,
          '%TITLE%' => tag.title,
          '%LENGTH%' => length,
          '%ARTIST%' => tag.artist,
          '%ALBUM%' => tag.album,
          '%YEAR%' => tag.year.to_s,
          '%GENRE%' => tag.genre,
          '%COMMENT%' => tag.comment 
        }

        parsed_string = ''

        template_strings.each do |key, val|
          if template['output'].include? key
            parsed_string = template['output'].gsub!(key, val)
          end
        end

        return parsed_string
    end
  end
end