Class: Muzak::Song
Overview
Represents a single song for muzak.
Instance Attribute Summary collapse
-
#album ⇒ String?
readonly
The album of the song, identified from metadata.
-
#artist ⇒ String?
readonly
The artist of the song, identified from metadata.
-
#comment ⇒ String?
readonly
Any comments in the song's metadata.
-
#genre ⇒ String?
readonly
The genre of the song, identified from metadata.
-
#length ⇒ Integer
readonly
The length of the song, in seconds.
-
#path ⇒ String
readonly
The fully-qualified path to the song.
-
#title ⇒ String
readonly
The title of the song, identified from metadata.
-
#track ⇒ Integer, 0
readonly
The track number of the song, identified from metadata.
-
#year ⇒ Integer, 0
readonly
The year of the song, identified from metadata.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Whether or not the given object is equal to this one.
-
#best_guess_album_art ⇒ String
A best guess path for the song's cover art.
-
#full_title ⇒ String
The "full" title of the song, including artist and album if available.
-
#initialize(path) ⇒ Song
constructor
A new instance of Song.
-
#to_h ⇒ Hash
A hash representation of the song, including pathname.
-
#to_json(*options) ⇒ String
A JSON-serialized representation of the song.
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.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/muzak/song.rb', line 38 def initialize(path) @path = path TagLib::FileRef.open(path) do |ref| break if ref.null? @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
#album ⇒ String? (readonly)
Returns the album of the song, identified from metadata.
20 21 22 |
# File 'lib/muzak/song.rb', line 20 def album @album end |
#artist ⇒ String? (readonly)
Returns the artist of the song, identified from metadata.
17 18 19 |
# File 'lib/muzak/song.rb', line 17 def artist @artist end |
#comment ⇒ String? (readonly)
Returns any comments in the song's metadata.
32 33 34 |
# File 'lib/muzak/song.rb', line 32 def comment @comment end |
#genre ⇒ String? (readonly)
Returns the genre of the song, identified from metadata.
29 30 31 |
# File 'lib/muzak/song.rb', line 29 def genre @genre end |
#length ⇒ Integer (readonly)
Returns the length of the song, in seconds.
35 36 37 |
# File 'lib/muzak/song.rb', line 35 def length @length end |
#path ⇒ String (readonly)
Returns the fully-qualified path to the song.
10 11 12 |
# File 'lib/muzak/song.rb', line 10 def path @path end |
#title ⇒ String (readonly)
if metadata is missing, the basename of the path is used instead
Returns the title of the song, identified from metadata.
14 15 16 |
# File 'lib/muzak/song.rb', line 14 def title @title end |
#track ⇒ Integer, 0 (readonly)
Returns the track number of the song, identified from metadata.
26 27 28 |
# File 'lib/muzak/song.rb', line 26 def track @track end |
#year ⇒ Integer, 0 (readonly)
Returns the year of the song, identified from metadata.
23 24 25 |
# File 'lib/muzak/song.rb', line 23 def year @year end |
Instance Method Details
#==(other) ⇒ Boolean
compares song paths, not metadata
Returns whether or not the given object is equal to this one.
82 83 84 |
# File 'lib/muzak/song.rb', line 82 def ==(other) other.is_a?(Song) && path == other.path end |
#best_guess_album_art ⇒ String
Returns A best guess path for the song's cover art.
61 62 63 64 65 66 |
# File 'lib/muzak/song.rb', line 61 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 ⇒ String
Returns the "full" title of the song, including artist and album if available.
72 73 74 75 76 77 78 |
# File 'lib/muzak/song.rb', line 72 def full_title full = title.dup full << " by #{artist}" if artist full << " on #{album}" if album full end |
#to_h ⇒ Hash
Returns a hash representation of the song, including pathname.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/muzak/song.rb', line 87 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
uses #to_h to build the representation
Returns a JSON-serialized representation of the song.
104 105 106 |
# File 'lib/muzak/song.rb', line 104 def to_json(*) to_h.to_json(*) end |