Class: Scrobbler::Library

Inherits:
Base
  • Object
show all
Defined in:
lib/scrobbler/library.rb

Overview

TODO:

everything

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key=, connection, get, maybe_streamable_attribute, maybe_streamable_node, post_request, request, sanitize, secret=

Constructor Details

#initialize(user) ⇒ Library

Returns a new instance of Library.



6
7
8
9
# File 'lib/scrobbler/library.rb', line 6

def initialize(user)
  @user = user if user.class == Scrobbler::User
  @user = Scrobbler::User.new(user.to_s) unless user.class == Scrobbler::User
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/scrobbler/library.rb', line 4

def user
  @user
end

Instance Method Details

#add_albumObject

Raises:

  • (NotImplementedError)


11
12
13
14
# File 'lib/scrobbler/library.rb', line 11

def add_album
    # This function require authentication, but SimpleAuth is not yet 2.0
    raise NotImplementedError
end

#add_artistObject

Raises:

  • (NotImplementedError)


16
17
18
19
# File 'lib/scrobbler/library.rb', line 16

def add_artist
    # This function require authentication, but SimpleAuth is not yet 2.0
    raise NotImplementedError
end

#add_trackObject

Raises:

  • (NotImplementedError)


21
22
23
24
# File 'lib/scrobbler/library.rb', line 21

def add_track
  # This function require authentication, but SimpleAuth is not yet 2.0
  raise NotImplementedError
end

#albums(options = {}) ⇒ Object

A list of all the albums in a user’s library, with play counts and tag counts.



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
# File 'lib/scrobbler/library.rb', line 28

def albums(options={})
    options = {:force => false, :all => true}.merge options
    options[:user] = @user.name
    albums = []
    if options[:all]
        doc = Base.request('library.getalbums', options)
        root = nil
        doc.root.children.each do |child|
            next unless child.name == 'albums'
            root = child
        end
        total_pages = root['totalPages'].to_i
        root.children.each do |child|
            next unless child.name == 'album'
            albums << Scrobbler::Album.new_from_libxml(child)
        end
        for i in 2..total_pages do
            options[:page] = i
            albums.concat get_response('library.getalbums', :none, 'albums', 'album', options, true)
        end
    else
        albums = get_response('library.getalbums', :get_albums, 'albums', 'album', options, true)
    end
    albums
end

#artists(options = {}) ⇒ Object

A list of all the artists in a user’s library, with play counts and tag counts.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/scrobbler/library.rb', line 56

def artists(options={})
    options = {:force => false, :all => true}.merge options
    options[:user] = @user.name
    artists = []
    if options[:all]
        doc = Base.request('library.getartists', options)
        root = nil
        doc.root.children.each do |child|
            next unless child.name == 'artists'
            root = child
        end
        total_pages = root['totalPages'].to_i
        root.children.each do |child|
            next unless child.name == 'artist'
            artists << Scrobbler::Artist.new_from_libxml(child)
        end
        for i in 2..total_pages do
            options[:page] = i
            artists.concat get_response('library.getartists', :none, 'artists', 'artist', options, true)
        end
    else
        artists = get_response('library.getartists', :get_albums, 'artists', 'artist', options, true)
    end
    artists
end

#tracks(options = {}) ⇒ Object

A list of all the tracks in a user’s library, with play counts and tag counts.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/scrobbler/library.rb', line 84

def tracks(options={})
    options = {:force => false, :all => true}.merge options
    options[:user] = @user.name
    tracks = []
    if options[:all]
        doc = Base.request('library.gettracks', options)
        root = nil
        doc.root.children.each do |child|
            next unless child.name == 'tracks'
            root = child
        end
        total_pages = root['totalPages'].to_i
        root.children.each do |child|
            next unless child.name == 'track'
            tracks << Scrobbler::Track.new_from_libxml(child)
        end
        for i in 2..total_pages do
            options[:page] = i
            tracks.concat get_response('library.gettracks', :none, 'tracks', 'track', options, true)
        end
    else
        tracks = get_response('library.gettracks', :get_albums, 'tracks', 'track', options, true)
    end
    tracks
end