This repository has been archived by the owner on Aug 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
album_browse.rb
78 lines (69 loc) · 2.35 KB
/
album_browse.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module Hallon
# AlbumBrowse objects are for retrieving additional data from
# an album that cannot otherwise be acquired. This includes
# tracks, reviews, copyright information.
#
# AlbumBrowse object triggers the `:load` callback on itself
# when it loads.
#
# @example
# browse = album.browse # album is a Hallon::Album
# browse.on(:load) do
# puts "Album browser for #{browse.album.name} has been loaded!"
# end
# session.wait_for { browse.loaded? } # will eventually trigger above callback
#
# @see Album
class AlbumBrowse < Base
include Hallon::Observable
# Creates an AlbumBrowse instance from an Album or an Album pointer.
#
# @note Use {Album#browse} to browse an Album.
# @param [Album, FFI::Pointer] album
def initialize(album)
session = Hallon::Session.instance
album = album.pointer if album.respond_to?(:pointer)
@callback = proc { trigger(:load) }
albumbrowse = Spotify::albumbrowse_create(session.pointer, album, @callback, nil)
@pointer = Spotify::Pointer.new(albumbrowse, :albumbrowse, false)
end
# @return [Boolean] true if the album is loaded
def loaded?
Spotify::albumbrowse_is_loaded(@pointer)
end
# @see Error
# @return [Symbol] album browser error status
def error
Spotify::albumbrowse_error(@pointer)
end
# @return [String] album review
def review
Spotify::albumbrowse_review(@pointer)
end
# @return [Artist] artist performing this album
def artist
pointer = Spotify::albumbrowse_artist(@pointer)
Hallon::Artist.new(pointer) unless pointer.null?
end
# @return [Album] album this object is browsing
def album
pointer = Spotify::albumbrowse_album(@pointer)
Hallon::Album.new(pointer) unless pointer.null?
end
# @return [Enumerator<String>] list of copyright notices
def copyrights
size = Spotify::albumbrowse_num_copyrights(@pointer)
Hallon::Enumerator.new(size) do |i|
Spotify::albumbrowse_copyright(@pointer, i)
end
end
# @return [Enumerator<Track>] list of tracks
def tracks
size = Spotify::albumbrowse_num_tracks(@pointer)
Hallon::Enumerator.new(size) do |i|
pointer = Spotify::albumbrowse_track(@pointer, i)
Hallon::Track.new(pointer) unless pointer.null?
end
end
end
end