class Muzak::Song

Represents a single song for muzak.

Attributes

album[R]

@return [String, nil] the album of the song, identified from metadata

artist[R]

@return [String, nil] the artist of the song, identified from metadata

comment[R]

@return [String, nil] any comments in the song's metadata

genre[R]

@return [String, nil] the genre of the song, identified from metadata

length[R]

@return [Integer] the length of the song, in seconds

path[R]

@return [String] the fully-qualified path to the song

title[R]

@return [String] the title of the song, identified from metadata @note if metadata is missing, the basename of the path is used instead

track[R]

@return [Integer, 0] the track number of the song, identified from metadata

year[R]

@return [Integer, 0] the year of the song, identified from metadata

Public Class Methods

new(path) click to toggle source

@param path [String] the path of the song to load

# File lib/muzak/song.rb, line 40
def initialize(path)
  @path = path

  TagLib::FileRef.open(path) do |ref|
    @title   = ref&.tag&.title
    @artist  = ref&.tag&.artist
    @album   = ref&.tag&.album
    @year    = ref&.tag&.year
    @track   = ref&.tag&.track
    @genre   = ref&.tag&.genre
    @comment = ref&.tag&.comment
    @length  = ref&.audio_properties&.length
  end

  # provide some sane fallbacks
  @title ||= File.basename(path, File.extname(path)) rescue ""
  @track ||= 0 # we'll need to sort by track number
end

Public Instance Methods

==(other) click to toggle source

@return [Boolean] whether or not the given object is equal to this one @note compares song paths, not metadata

# File lib/muzak/song.rb, line 83
def ==(other)
  other.is_a?(Song) && path == other.path
end
best_guess_album_art() click to toggle source

@return [String] A best guess path for the song's cover art @example

song.best_guess_album_art # => "/path/to/song/directory/cover.jpg"
# File lib/muzak/song.rb, line 62
def best_guess_album_art
  album_dir = File.dirname(path)

  art = Dir.entries(album_dir).find { |ent| Utils.album_art?(ent) }
  File.join(album_dir, art) unless art.nil?
end
full_title() click to toggle source

@return [String] the “full” title of the song, including artist and album

if available.

@example

song.full_title # => "Song by Artist on Album"
# File lib/muzak/song.rb, line 73
def full_title
  full = title.dup
  full << " by #{artist}" if artist
  full << " on #{album}" if album

  full
end
to_h() click to toggle source

@return [Hash] a hash representation of the song, including pathname

# File lib/muzak/song.rb, line 88
def to_h
  {
    path: path,
    title: title,
    artist: artist,
    album: album,
    year: year,
    track: track,
    genre: genre,
    comment: comment,
    length: length,
  }
end
to_json(*options) click to toggle source

@param options [Array] options passed to the internal `to_json` call @return [String] a JSON-serialized representation of the song @note uses {to_h} to build the representation

# File lib/muzak/song.rb, line 105
def to_json(*options)
  to_h.to_json(*options)
end