Class: Scrobbler::Album

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connection, fetch_and_parse

Constructor Details

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

Returns a new instance of Album.

Raises:

  • (ArgumentError)


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

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.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def artist
  @artist
end

#artist_mbidObject

Returns the value of attribute artist_mbid.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def artist_mbid
  @artist_mbid
end

#chartpositionObject

needed for weekly album charts



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

def chartposition
  @chartposition
end

#countObject

needed on top albums for tag



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

def count
  @count
end

#image_largeObject

Returns the value of attribute image_large.



47
48
49
# File 'lib/scrobbler/album.rb', line 47

def image_large
  @image_large
end

#image_mediumObject

Returns the value of attribute image_medium.



47
48
49
# File 'lib/scrobbler/album.rb', line 47

def image_medium
  @image_medium
end

#image_smallObject

Returns the value of attribute image_small.



47
48
49
# File 'lib/scrobbler/album.rb', line 47

def image_small
  @image_small
end

#mbidObject

Returns the value of attribute mbid.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def name
  @name
end

#playcountObject

Returns the value of attribute playcount.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def playcount
  @playcount
end

#rankObject

Returns the value of attribute rank.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def rank
  @rank
end

#reachObject

Returns the value of attribute reach.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def reach
  @reach
end

#release_dateObject

Returns the value of attribute release_date.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def release_date
  @release_date
end

#streamableObject

needed on top albums for tag



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

def streamable
  @streamable
end

#tracksObject



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

def tracks
  load_info if @tracks.nil?
  @tracks
end

#urlObject

Returns the value of attribute url.



46
47
48
# File 'lib/scrobbler/album.rb', line 46

def url
  @url
end

Class Method Details

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



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

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

.new_from_xml(xml, doc = nil) ⇒ Object



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

def new_from_xml(xml, doc=nil)
  name             = (xml).at(:name).inner_html                   if (xml).at(:name)
  name             = xml['name']                                  if name.nil? && xml['name']
  artist           = (xml).at(:artist)['name']                    if (xml).at(:artist) && (xml).at(:artist)['name']
  artist           = (xml).at(:artist).inner_html                 if artist.nil? && (xml).at(:artist)
  artist           = doc.root['artist']                           if artist.nil? && doc.root['artist']
  a                = Album.new(artist, name)
  a.artist_mbid    = (xml).at(:artist)['mbid']                    if (xml).at(:artist) && (xml).at(:artist)['mbid']
  a.artist_mbid    = (xml).at(:artist).at(:mbid).inner_html       if a.artist_mbid.nil? && (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).at(:rank).inner_html                   if (xml).at(:rank)
  a.url            = (xml/:url).last.inner_html                   if (xml/:url).size > 1
  a.url            = (xml).at(:url).inner_html                    if a.url.nil? && (xml).at(:url)
  a.reach          = (xml).at(:reach).inner_html                  if (xml).at(:reach)
  a.image_large    = (xml).at(:image).at(:large).inner_html       if (xml).at(:image) && (xml).at(:image).at(:large)
  a.image_medium   = (xml).at(:image).at(:medium).inner_html      if (xml).at(:image) && (xml).at(:image).at(:medium)
  a.image_small    = (xml).at(:image).at(:small).inner_html       if (xml).at(:image) && (xml).at(:image).at(:small)
  
  # coverart element used on top albums for tag
  a.image_large    = (xml).at(:coverart).at(:large).inner_html    if a.image_large.nil? && (xml).at(:coverart) && (xml).at(:coverart).at(:large)
  a.image_medium   = (xml).at(:coverart).at(:medium).inner_html   if a.image_medium.nil? && (xml).at(:coverart) && (xml).at(:coverart).at(:medium)
  a.image_small    = (xml).at(:coverart).at(:small).inner_html    if a.image_small.nil? && (xml).at(:coverart) && (xml).at(:coverart).at(: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

#api_pathObject



102
103
104
# File 'lib/scrobbler/album.rb', line 102

def api_path
  "/#{API_VERSION}/album/#{CGI::escape(artist)}/#{CGI::escape(name)}"
end

#image(which = :small) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
# File 'lib/scrobbler/album.rb', line 132

def image(which=:small)
  which = which.to_s
  raise ArgumentError unless ['small', 'medium', 'large'].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



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

def load_info
  doc           = self.class.fetch_and_parse("#{api_path}/info.xml")
  @reach        = (doc).at(:reach).inner_html
  @url          = (doc).at(:url).inner_html
  @release_date = Time.parse((doc).at(:releasedate).inner_html.strip)
  @image_large  = (doc).at(:coverart).at(:large).inner_html
  @image_medium = (doc).at(:coverart).at(:medium).inner_html
  @image_small  = (doc).at(:coverart).at(:small).inner_html
  @mbid         = (doc).at(:mbid).inner_html
  @tracks       = (doc/:track).inject([]) do |tracks, track|
    t             = Track.new(artist, track['title'])
    t.artist_mbid = artist_mbid
    t.album       = name
    t.album_mbid  = mbid
    t.url         = (track).at(:url).inner_html
    t.reach       = (track).at(:reach).inner_html
    tracks << t
    tracks
  end
end