Class: Scrobbler::Album

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

Overview

TODO:

Add missing functions that require authentication

TODO:

Integrate search functionality into this class which is already implemented in Scrobbler::Search

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key=, connection, fetch_and_parse, sanitize

Constructor Details

#initialize(artist, name, o = {}) ⇒ Album

TODO:

Albums should be able to be created via a MusicBrainz id too

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

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
# File 'lib/scrobbler/album.rb', line 91

def initialize(artist, name, o={})
  raise ArgumentError, "Artist is required" if artist.blank?
  raise ArgumentError, "Name is required" if name.blank?
  @artist = artist
  @name   = name
  options = {:include_info => false}.merge(o)
  load_info() if options[:include_info]
end

Instance Attribute Details

#artistObject

Returns the value of attribute artist.



49
50
51
# File 'lib/scrobbler/album.rb', line 49

def artist
  @artist
end

#artist_mbidObject

Returns the value of attribute artist_mbid.



49
50
51
# File 'lib/scrobbler/album.rb', line 49

def artist_mbid
  @artist_mbid
end

#chartpositionObject

needed for weekly album charts



58
59
60
# File 'lib/scrobbler/album.rb', line 58

def chartposition
  @chartposition
end

#countObject

needed on top albums for tag



55
56
57
# File 'lib/scrobbler/album.rb', line 55

def count
  @count
end

#image_largeObject

Returns the value of attribute image_large.



51
52
53
# File 'lib/scrobbler/album.rb', line 51

def image_large
  @image_large
end

#image_mediumObject

Returns the value of attribute image_medium.



51
52
53
# File 'lib/scrobbler/album.rb', line 51

def image_medium
  @image_medium
end

#image_smallObject

Returns the value of attribute image_small.



51
52
53
# File 'lib/scrobbler/album.rb', line 51

def image_small
  @image_small
end

#listenersObject

Returns the value of attribute listeners.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def listeners
  @listeners
end

#mbidObject

Returns the value of attribute mbid.



49
50
51
# File 'lib/scrobbler/album.rb', line 49

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



49
50
51
# File 'lib/scrobbler/album.rb', line 49

def name
  @name
end

#playcountObject

Returns the value of attribute playcount.



49
50
51
# File 'lib/scrobbler/album.rb', line 49

def playcount
  @playcount
end

#rankObject

Returns the value of attribute rank.



49
50
51
# File 'lib/scrobbler/album.rb', line 49

def rank
  @rank
end

#reachObject

Returns the value of attribute reach.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def reach
  @reach
end

#release_dateObject

Returns the value of attribute release_date.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def release_date
  @release_date
end

#streamableObject

needed on top albums for tag



55
56
57
# File 'lib/scrobbler/album.rb', line 55

def streamable
  @streamable
end

#top_tagsObject

Returns the value of attribute top_tags.



50
51
52
# File 'lib/scrobbler/album.rb', line 50

def top_tags
  @top_tags
end

#tracks=(value) ⇒ Object (writeonly)

Sets the attribute tracks

Parameters:

  • value

    the value to set the attribute tracks to.



52
53
54
# File 'lib/scrobbler/album.rb', line 52

def tracks=(value)
  @tracks = value
end

#urlObject

Returns the value of attribute url.



49
50
51
# File 'lib/scrobbler/album.rb', line 49

def url
  @url
end

Class Method Details

.find(artist, name, o = {}) ⇒ Object



61
62
63
# File 'lib/scrobbler/album.rb', line 61

def find(artist, name, o={})
  new(artist, name, o)
end

.new_from_xml(xml, doc = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/scrobbler/album.rb', line 65

def new_from_xml(xml, doc=nil)
  name = Base::sanitize(xml.at(:name).inner_html) if xml.at(:name)
  name = Base::sanitize(xml['name'])              if name.nil? && xml['name']
  artist = Base::sanitize(xml.at('/artist/name').inner_html) if xml.at('/artist/name')
  a = Album.new(artist, name)
  a.artist_mbid = xml.at(:artist).at(:mbid).inner_html if xml.at(:artist) && xml.at(:artist).at(:mbid)
  a.mbid          = xml.at(:mbid).inner_html           if xml.at(:mbid)
  a.playcount     = xml.at(:playcount).inner_html      if xml.at(:playcount)
  a.chartposition = xml.at(:chartposition).inner_html  if xml.at(:chartposition)
  a.rank          = xml['rank']                        if xml['rank']
  a.url           = xml.at('/url').inner_html          if xml.at('/url')
  a.image_large   = xml.at("/image[@size='large']").inner_html  if xml.at("/image[@size='large']")
  a.image_medium  = xml.at("/image[@size='medium']").inner_html if xml.at("/image[@size='medium']")
  a.image_small   = xml.at("/image[@size='small']").inner_html  if xml.at("/image[@size='small']")
          
  # needed on top albums for tag
  a.count          = xml['count'] if xml['count']
  a.streamable     = xml['streamable'] if xml['streamable']
  a
end

Instance Method Details

#image(which = :small) ⇒ Object

Raises:

  • (ArgumentError)


129
130
131
132
133
134
135
136
137
138
# File 'lib/scrobbler/album.rb', line 129

def image(which=:small)
  which = which.to_s
  raise ArgumentError unless ['small', 'medium', 'large', 'extralarge'].include?(which)      
  img_url = instance_variable_get("@image_#{which}")
  if img_url.nil?
    load_info
    img_url = instance_variable_get("@image_#{which}")
  end
  img_url
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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/scrobbler/album.rb', line 108

def load_info
  xml = request('album.getinfo', {'artist' => @artist, 'name' => @name})
  unless xml.at(:lfm)['status'] == 'failed' || @info_loaded
    xml = xml.at(:album)
    @url          = xml.at(:url).inner_html
    @release_date = Time.parse(xml.at(:releasedate).inner_html.strip)
    @image_extralarge = xml.at("/image[@size='extralarge']").inner_html
    @image_large  = xml.at("/image[@size='large']").inner_html
    @image_medium = xml.at("/image[@size='medium']").inner_html
    @image_small  = xml.at("/image[@size='small']").inner_html
    @mbid         = xml.at(:mbid).inner_html
    @listeners    = xml.at(:listeners).inner_html.to_i
    @playcount    = xml.at(:playcount).inner_html.to_i
    @info_loaded  = true
    @top_tags = []
    xml.at(:toptags).search(:tag).each do |elem|
        @top_tags << Tag.new_from_xml(elem)
    end
  end
end