Class: Scrobbler::Album

Inherits:
BaseXmlInfo show all
Includes:
ImageObjectFuncs
Defined in:
lib/scrobbler/album.rb

Overview

Class for handling album.* requests to the Last.fm API and reading Album data provided in return.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ImageObjectFuncs

#check_image_node, #image

Methods inherited from Base

add_cache, api_key=, #call, #call_pageable, fetch_http, get, load_from_cache, #populate_data, post_request, #request, request, sanitize, save_to_cache, secret=, validate_response_document

Constructor Details

#initialize(data = {}) ⇒ Album

Create a new Scrobbler::Artist instance

If the additional parameter :include_info is set to true, additional information is loaded

Parameters:

  • data (Hash) (defaults to: {})

    The options to initialize the class

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/scrobbler/album.rb', line 33

def initialize(data={})
  raise ArgumentError unless data.kind_of?(Hash)
  super(data)
  
  if @artist.is_a?(String) then
    @artist = Artist.new(:name => @artist)
  end
  
  raise ArgumentError, "Artist or mbid is required" if @artist.nil? && @mbid.nil?
  raise ArgumentError, "Name is required" if @name.empty?
end

Instance Attribute Details

#artistObject (readonly)

Returns the value of attribute artist



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

def artist
  @artist
end

#artist_mbidObject (readonly)

Returns the value of attribute artist_mbid



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

def artist_mbid
  @artist_mbid
end

#chartpositionObject (readonly)

Returns the value of attribute chartposition



13
14
15
# File 'lib/scrobbler/album.rb', line 13

def chartposition
  @chartposition
end

#countObject (readonly)

Returns the value of attribute count



13
14
15
# File 'lib/scrobbler/album.rb', line 13

def count
  @count
end

#listenersObject (readonly)

Returns the value of attribute listeners



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

def listeners
  @listeners
end

#mbidObject (readonly)

Returns the value of attribute mbid



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

def mbid
  @mbid
end

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

#playcountObject (readonly)

Returns the value of attribute playcount



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

def playcount
  @playcount
end

#positionObject (readonly)

Returns the value of attribute position



13
14
15
# File 'lib/scrobbler/album.rb', line 13

def position
  @position
end

#rankObject (readonly)

Returns the value of attribute rank



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

def rank
  @rank
end

#reachObject (readonly)

Returns the value of attribute reach



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

def reach
  @reach
end

#release_dateObject (readonly)

Returns the value of attribute release_date



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

def release_date
  @release_date
end

#tagcountObject (readonly)

Returns the value of attribute tagcount



13
14
15
# File 'lib/scrobbler/album.rb', line 13

def tagcount
  @tagcount
end

#top_tagsObject (readonly)

Returns the value of attribute top_tags



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

def top_tags
  @top_tags
end

#urlObject (readonly)

Returns the value of attribute url



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

def url
  @url
end

Class Method Details

.new_from_libxml(xml) ⇒ Object

Deprecated.

Alias for Album.new(:xml => xml)



18
19
20
# File 'lib/scrobbler/album.rb', line 18

def self.new_from_libxml(xml)
  Album.new(:xml => xml)
end

Instance Method Details

#add_tags(session, tags) ⇒ nil

Tag an album using a list of user supplied tags.

Parameters:

  • session (Scrobbler::Session)

    A valid session to authenticate access

  • tags (Array<String>)

    Tags to add to this album

Returns:

  • (nil)


128
129
130
# File 'lib/scrobbler/album.rb', line 128

def add_tags(session, tags)
  Base.post_request('album.addTags', {:sk => session.key, :signed => true, :tags => tags.join(',')})
end

Get the links to buy this album

Raises:

  • (NotImplementedError)


145
146
147
# File 'lib/scrobbler/album.rb', line 145

def buylinks()
  raise NotImplementedError
end

#load_from_xml(node) ⇒ nil

Load the data for this object out of a XML-Node

Parameters:

  • node (LibXML::XML::Node)

    The XML node containing the information

Returns:

  • (nil)


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scrobbler/album.rb', line 49

def load_from_xml(node)
  # Get all information from the root's children nodes
  node.children.each do |child|
    case child.name.to_s
      when 'name'
        @name = child.content
      when 'title'
        @name = child.content
      when 'playcount'
        @playcount = child.content.to_i
      when 'tagcount'
        @tagcount = child.content.to_i
      when 'mbid'
        @mbid = child.content
      when 'url'
        @url = child.content
      when 'artist'
        @artist = Artist.new(:xml => child)
      when 'image'
        check_image_node(child)
      when 'id'
        @id = child.content.to_s
      when 'releasedate'
        @release_date = Time.parse(child.content.strip)
      when 'listeners'
        @listeners = child.content.to_i
      when 'toptags'
        @top_tags = [] if @top_tags.nil?
        child.children.each do |tag|
          next unless tag.name == 'tag'
          @top_tags << Tag.new_from_libxml(tag)
        end
      when 'wiki'            # @todo Handle wiki entries
      when 'text'
        # ignore, these are only blanks
      when '#text'
        # libxml-jruby version of blanks
      else
        raise NotImplementedError, "Field '#{child.name}' not known (#{child.content})"
    end #^ case
  end #^ do |child|

  # Get all information from the root's attributes
  @mbid = node['mbid'].to_s unless node['mbid'].nil?
  @rank = node['rank'].to_i unless node['rank'].nil?
  @position = node['position'].to_i unless node['position'].nil?

  # If we have not found anything in the content of this node yet then
  # this must be a simple artist node which has the name of the artist
  # as its content
  @name = node.content if @name.nil?
end

#load_infoObject

TODO:

Parse wiki content

TODO:

Add language code for wiki translation

Load additional information about this album

Calls "album.getinfo" REST method



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/scrobbler/album.rb', line 111

def load_info
  return nil if @info_loaded
  xml = Base.request('album.getinfo', {:artist => @artist.name, :album => @name})
  unless xml.root['status'] == 'failed'
    xml.root.children.each do |childL1|
      next unless childL1.name == 'album'
      load_from_xml(childL1)
    end # xml.children.each do |childL1|
    @info_loaded  = true
  end
end

#remove_tagObject

Remove a user's tag from an album.

Raises:

  • (NotImplementedError)


139
140
141
142
# File 'lib/scrobbler/album.rb', line 139

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

#searchObject

Raises:

  • (NotImplementedError)


22
23
24
25
# File 'lib/scrobbler/album.rb', line 22

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

#shareObject

Share this album with a friend

Raises:

  • (NotImplementedError)


150
151
152
# File 'lib/scrobbler/album.rb', line 150

def share()
  raise NotImplementedError
end

#tagsObject

Get the tags applied by an individual user to an album on Last.fm.

Raises:

  • (NotImplementedError)


133
134
135
136
# File 'lib/scrobbler/album.rb', line 133

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