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

- (Album) initialize(artist, name, o = {})

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

- (Object) artist

Returns the value of attribute artist



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

def artist
  @artist
end

- (Object) artist_mbid

Returns the value of attribute artist_mbid



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

def artist_mbid
  @artist_mbid
end

- (Object) chartposition

needed for weekly album charts



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

def chartposition
  @chartposition
end

- (Object) count

needed on top albums for tag



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

def count
  @count
end

- (Object) image_large

Returns the value of attribute image_large



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

def image_large
  @image_large
end

- (Object) image_medium

Returns the value of attribute image_medium



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

def image_medium
  @image_medium
end

- (Object) image_small

Returns the value of attribute image_small



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

def image_small
  @image_small
end

- (Object) mbid

Returns the value of attribute mbid



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

def mbid
  @mbid
end

- (Object) name

Returns the value of attribute name



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

def name
  @name
end

- (Object) playcount

Returns the value of attribute playcount



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

def playcount
  @playcount
end

- (Object) rank

Returns the value of attribute rank



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

def rank
  @rank
end

- (Object) reach

Returns the value of attribute reach



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

def reach
  @reach
end

- (Object) release_date

Returns the value of attribute release_date



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

def release_date
  @release_date
end

- (Object) streamable

needed on top albums for tag



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

def streamable
  @streamable
end

- (Object) tracks



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

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

- (Object) url

Returns the value of attribute url



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

def url
  @url
end

Class Method Details

+ (Object) find(artist, name, o = {})



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

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

+ (Object) new_from_xml(xml, doc = nil)



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

- (Object) api_path



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

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

- (Object) image(which = :small)

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

- (Object) load_info



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