Class: Rockstar::Album

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connection, fetch_and_parse, get_instance

Constructor Details

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

Returns a new instance of Album.

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
# File 'lib/rockstar/album.rb', line 47

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.



19
20
21
# File 'lib/rockstar/album.rb', line 19

def artist
  @artist
end

#artist_mbidObject

Returns the value of attribute artist_mbid.



19
20
21
# File 'lib/rockstar/album.rb', line 19

def artist_mbid
  @artist_mbid
end

#chartpositionObject

needed for weekly album charts



26
27
28
# File 'lib/rockstar/album.rb', line 26

def chartposition
  @chartposition
end

#contentObject

Returns the value of attribute content.



20
21
22
# File 'lib/rockstar/album.rb', line 20

def content
  @content
end

#countObject

needed on top albums for tag



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

def count
  @count
end

#image_largeObject

Returns the value of attribute image_large.



20
21
22
# File 'lib/rockstar/album.rb', line 20

def image_large
  @image_large
end

#image_mediumObject

Returns the value of attribute image_medium.



20
21
22
# File 'lib/rockstar/album.rb', line 20

def image_medium
  @image_medium
end

#image_smallObject

Returns the value of attribute image_small.



20
21
22
# File 'lib/rockstar/album.rb', line 20

def image_small
  @image_small
end

#imagesObject

Returns the value of attribute images.



20
21
22
# File 'lib/rockstar/album.rb', line 20

def images
  @images
end

#mbidObject

Returns the value of attribute mbid.



19
20
21
# File 'lib/rockstar/album.rb', line 19

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/rockstar/album.rb', line 19

def name
  @name
end

#playcountObject

Returns the value of attribute playcount.



19
20
21
# File 'lib/rockstar/album.rb', line 19

def playcount
  @playcount
end

#rankObject

Returns the value of attribute rank.



19
20
21
# File 'lib/rockstar/album.rb', line 19

def rank
  @rank
end

#release_dateObject

Returns the value of attribute release_date.



19
20
21
# File 'lib/rockstar/album.rb', line 19

def release_date
  @release_date
end

#streamableObject

needed on top albums for tag



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

def streamable
  @streamable
end

#summaryObject

Returns the value of attribute summary.



20
21
22
# File 'lib/rockstar/album.rb', line 20

def summary
  @summary
end

#urlObject

Returns the value of attribute url.



19
20
21
# File 'lib/rockstar/album.rb', line 19

def url
  @url
end

Class Method Details

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



29
30
31
# File 'lib/rockstar/album.rb', line 29

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

.new_from_xml(xml, doc = nil) ⇒ Object



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

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']
  name             = xml.at(:title).inner_html                   if name.nil? && (xml).at(:title)
  artist           = (xml).at(:artist).at(:name).inner_html      if (xml).at(:artist) && (xml).at(:artist).at(:name)
  artist           = (xml).at(:artist).inner_html                if artist.nil? && (xml).at(:artist)
  artist           = doc.root['artist']                          if artist.nil? && doc && doc.root['artist']

  album = Album.new(artist, name)
  album.load_info(xml)
  album
end

Instance Method Details

#image(which = :small) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
# File 'lib/rockstar/album.rb', line 100

def image(which=:small)
  which = which.to_s
  raise ArgumentError unless ['small', 'medium', 'large', 'extralarge'].include?(which)  
  if (self.images.nil?)
    load_info
  end    
  self.images[which]
end

#load_info(xml = nil) ⇒ Object



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

def load_info(xml=nil)
  unless xml
    doc = self.class.fetch_and_parse("album.getInfo", {:artist => @artist, :album =>@name})
    xml = (doc / :album).first
  end

  return self if xml.nil?

  self.artist_mbid    = (xml).at(:artist)['mbid']                   if (xml).at(:artist) && (xml).at(:artist)['mbid']
  self.artist_mbid    = (xml).at(:artist).at(:mbid).inner_html      if artist_mbid.nil? && (xml).at(:artist) && (xml).at(:artist).at(:mbid)
  self.mbid           = (xml).at(:mbid).inner_html                  if (xml).at(:mbid)
  self.playcount      = (xml).at(:playcount).inner_html             if (xml).at(:playcount)
  self.rank           = xml['rank']                                 if xml['rank']
  self.rank           = (xml).at(:rank).inner_html                  if (xml).at(:rank) if rank.nil?
  self.url            = Base.fix_url((xml).at(:url).inner_html)     if (xml).at(:url)

  self.summary        = (xml).at(:summary).to_plain_text            if (xml).at(:summary)
  self.content        = (xml).at(:content).to_plain_text            if (xml).at(:content)

  self.release_date   = Base.parse_time((xml).at(:releasedate).inner_html.strip) if (xml).at(:releasedate)
  self.chartposition  = rank
  
  self.images = {}
  (xml/'image').each {|image|
    self.images[image['size']] = image.inner_html if self.images[image['size']].nil?
  }
  
  self.image_large    = images['large']
  self.image_medium   = images['medium']
  self.image_small    = images['small']
  
  # needed on top albums for tag
  self.count          = xml['count'] if xml['count']
  self.streamable     = xml['streamable'] if xml['streamable']

  self
end

#tracksObject



95
96
97
98
# File 'lib/rockstar/album.rb', line 95

def tracks
  warn "[DEPRECATION] `tracks` is deprecated. The current api doesn't offer this function"
  []
end