Class: TrackList::TrackParser

Inherits:
Object
  • Object
show all
Defined in:
lib/track_list/track_parser.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(track) ⇒ TrackParser

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

Parameters:

  • track (String)

    The track to be parsed.



19
20
21
# File 'lib/track_list/track_parser.rb', line 19

def initialize(track)
    @track = track
end

Instance Method Details

#parseString

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

Returns:

  • (String)

    if valid audio file.



29
30
31
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
60
61
62
63
# File 'lib/track_list/track_parser.rb', line 29

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_in_seconds)
        length = time_conversion.format_time

        template_parser = TrackList::TemplateParser.new
        template = template_parser.load

        template_strings = {
          '%TRACK%' => tag.track.to_s,
          '%TITLE%' => tag.title,
          '%LENGTH%' => length,
          '%LENGTH_IN_MILLISECONDS%' => properties.length_in_milliseconds.to_s,
          '%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