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.
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
#album ⇒ String? (readonly)
Returns the album of the song, identified from metadata.
22 23 24 |
# File 'lib/muzak/song.rb', line 22 def album @album end |
#artist ⇒ String? (readonly)
Returns the artist of the song, identified from metadata.
19 20 21 |
# File 'lib/muzak/song.rb', line 19 def artist @artist end |
#comment ⇒ String? (readonly)
Returns any comments in the song's metadata.
34 35 36 |
# File 'lib/muzak/song.rb', line 34 def comment @comment end |
#genre ⇒ String? (readonly)
Returns the genre of the song, identified from metadata.
31 32 33 |
# File 'lib/muzak/song.rb', line 31 def genre @genre end |
#length ⇒ Integer (readonly)
Returns the length of the song, in seconds.
37 38 39 |
# File 'lib/muzak/song.rb', line 37 def length @length end |
#path ⇒ String (readonly)
Returns the fully-qualified path to the song.
12 13 14 |
# File 'lib/muzak/song.rb', line 12 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.
16 17 18 |
# File 'lib/muzak/song.rb', line 16 def title @title end |
#track ⇒ Integer, 0 (readonly)
Returns the track number of the song, identified from metadata.
28 29 30 |
# File 'lib/muzak/song.rb', line 28 def track @track end |
#year ⇒ Integer, 0 (readonly)
Returns 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
compares song paths, not metadata
Returns 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_art ⇒ String
Returns 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_title ⇒ String
Returns 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_h ⇒ Hash
Returns 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
uses #to_h to build the representation
Returns a JSON-serialized representation of the song.
105 106 107 |
# File 'lib/muzak/song.rb', line 105 def to_json(*) to_h.to_json(*) end |