Class: Muzak::Song

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/muzak/song.rb

Overview

Represents a single song for muzak.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

album_art?, #build_response, #danger, #debug, #debug?, #error, #error!, music?, #output, #pretty, #verbose, #verbose?, which?

Constructor Details

#initialize(path) ⇒ Song

Returns a new instance of Song.

Parameters:

  • path (String)

    the path of the song to load



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Attribute Details

#albumString? (readonly)

Returns the album of the song, identified from metadata.

Returns:

  • (String, nil)

    the album of the song, identified from metadata



22
23
24
# File 'lib/muzak/song.rb', line 22

def album
  @album
end

#artistString? (readonly)

Returns the artist of the song, identified from metadata.

Returns:

  • (String, nil)

    the artist of the song, identified from metadata



19
20
21
# File 'lib/muzak/song.rb', line 19

def artist
  @artist
end

#commentString? (readonly)

Returns any comments in the song's metadata.

Returns:

  • (String, nil)

    any comments in the song's metadata



34
35
36
# File 'lib/muzak/song.rb', line 34

def comment
  @comment
end

#genreString? (readonly)

Returns the genre of the song, identified from metadata.

Returns:

  • (String, nil)

    the genre of the song, identified from metadata



31
32
33
# File 'lib/muzak/song.rb', line 31

def genre
  @genre
end

#lengthInteger (readonly)

Returns the length of the song, in seconds.

Returns:

  • (Integer)

    the length of the song, in seconds



37
38
39
# File 'lib/muzak/song.rb', line 37

def length
  @length
end

#pathString (readonly)

Returns the fully-qualified path to the song.

Returns:

  • (String)

    the fully-qualified path to the song



12
13
14
# File 'lib/muzak/song.rb', line 12

def path
  @path
end

#titleString (readonly)

Note:

if metadata is missing, the basename of the path is used instead

Returns the title of the song, identified from metadata.

Returns:

  • (String)

    the title of the song, identified from metadata



16
17
18
# File 'lib/muzak/song.rb', line 16

def title
  @title
end

#trackInteger, 0 (readonly)

Returns the track number of the song, identified from metadata.

Returns:

  • (Integer, 0)

    the track number of the song, identified from metadata



28
29
30
# File 'lib/muzak/song.rb', line 28

def track
  @track
end

#yearInteger, 0 (readonly)

Returns the year of the song, identified from metadata.

Returns:

  • (Integer, 0)

    the year of the song, identified from metadata



25
26
27
# File 'lib/muzak/song.rb', line 25

def year
  @year
end

Instance Method Details

#==(other) ⇒ Boolean

Note:

compares song paths, not metadata

Returns whether or not the given object is equal to this one.

Returns:

  • (Boolean)

    whether or not the given object is equal to this one



83
84
85
# File 'lib/muzak/song.rb', line 83

def ==(other)
  other.is_a?(Song) && path == other.path
end

#best_guess_album_artString

Returns A best guess path for the song's cover art.

Examples:

song.best_guess_album_art # => "/path/to/song/directory/cover.jpg"

Returns:

  • (String)

    A best guess path for the song's cover art



62
63
64
65
66
67
# 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_titleString

Returns the "full" title of the song, including artist and album if available.

Examples:

song.full_title # => "Song by Artist on Album"

Returns:

  • (String)

    the "full" title of the song, including artist and album if available.



73
74
75
76
77
78
79
# 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_hHash

Returns a hash representation of the song, including pathname.

Returns:

  • (Hash)

    a hash representation of the song, including pathname



88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ String

Note:

uses #to_h to build the representation

Returns a JSON-serialized representation of the song.

Parameters:

  • options (Array)

    options passed to the internal to_json call

Returns:

  • (String)

    a JSON-serialized representation of the song



105
106
107
# File 'lib/muzak/song.rb', line 105

def to_json(*options)
  to_h.to_json(*options)
end