Class: Rockstar::Artist

Inherits:
Base
  • Object
show all
Defined in:
lib/rockstar/artist.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(name, o = {}) ⇒ Artist

Returns a new instance of Artist.

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
# File 'lib/rockstar/artist.rb', line 80

def initialize(name, o={})
  raise ArgumentError, "Name or mbid is required" if name.blank? && o[:mbid].blank?
  @name = name unless name.blank?
  @mbid = o[:mbid] unless o[:mbid].blank?

  options = {:include_info => false}.merge(o)
  load_info if options[:include_info]
end

Instance Attribute Details

#chartpositionObject

Returns the value of attribute chartposition.



63
64
65
# File 'lib/rockstar/artist.rb', line 63

def chartposition
  @chartposition
end

#contentObject

Returns the value of attribute content.



62
63
64
# File 'lib/rockstar/artist.rb', line 62

def content
  @content
end

#countObject

Returns the value of attribute count.



62
63
64
# File 'lib/rockstar/artist.rb', line 62

def count
  @count
end

#imagesObject

Returns the value of attribute images.



62
63
64
# File 'lib/rockstar/artist.rb', line 62

def images
  @images
end

#listenercountObject

Returns the value of attribute listenercount.



61
62
63
# File 'lib/rockstar/artist.rb', line 61

def listenercount
  @listenercount
end

#matchObject

used for similar artists



66
67
68
# File 'lib/rockstar/artist.rb', line 66

def match
  @match
end

#mbidObject

Returns the value of attribute mbid.



61
62
63
# File 'lib/rockstar/artist.rb', line 61

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



61
62
63
# File 'lib/rockstar/artist.rb', line 61

def name
  @name
end

#playcountObject

Returns the value of attribute playcount.



61
62
63
# File 'lib/rockstar/artist.rb', line 61

def playcount
  @playcount
end

#rankObject

Returns the value of attribute rank.



61
62
63
# File 'lib/rockstar/artist.rb', line 61

def rank
  @rank
end

#streamableObject

Returns the value of attribute streamable.



62
63
64
# File 'lib/rockstar/artist.rb', line 62

def streamable
  @streamable
end

#summaryObject

Returns the value of attribute summary.



62
63
64
# File 'lib/rockstar/artist.rb', line 62

def summary
  @summary
end

#tagsObject

Returns the value of attribute tags.



62
63
64
# File 'lib/rockstar/artist.rb', line 62

def tags
  @tags
end

#thumbnailObject

Returns the value of attribute thumbnail.



61
62
63
# File 'lib/rockstar/artist.rb', line 61

def thumbnail
  @thumbnail
end

#urlObject

Returns the value of attribute url.



61
62
63
# File 'lib/rockstar/artist.rb', line 61

def url
  @url
end

Class Method Details

.new_from_xml(xml, doc = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/rockstar/artist.rb', line 69

def new_from_xml(xml, doc=nil)
  # occasionally name can be found in root of artist element (<artist name="">) rather than as an element (<name>)
  name             = (xml).at(:name).inner_html           if (xml).at(:name)
  name             = xml['name']                          if name.nil? && xml['name']

  artist = Artist.new(name)
  artist.load_info(xml)
  artist
end

Instance Method Details

#events(force = false) ⇒ Object



131
132
133
# File 'lib/rockstar/artist.rb', line 131

def events(force=false)
  get_instance("artist.getEvents", :events, :event, {:artist => @name}, force)
end

#image(which = :medium) ⇒ Object

Raises:

  • (ArgumentError)


155
156
157
158
159
160
# File 'lib/rockstar/artist.rb', line 155

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

#load_info(xml = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rockstar/artist.rb', line 89

def load_info(xml=nil)
  unless xml
    params = @mbid.blank? ? {:artist => @name} : {:mbid => @mbid}

    doc = self.class.fetch_and_parse("artist.getInfo", params)
    xml = (doc / :artist).first
  end

  return self if xml.nil?

  self.name           = (xml).at(:name).inner_html              if (xml).at(:name)
  self.mbid           = (xml).at(:mbid).inner_html              if (xml).at(:mbid)
  self.listenercount  = (xml).at(:listeners).inner_html         if (xml).at(:listeners)
  self.playcount      = (xml).at(:playcount).inner_html         if (xml).at(:playcount)
  self.chartposition  = self.rank = xml['rank']                 if xml['rank']
  self.chartposition  = self.rank = (xml).at(:rank).inner_html  if (xml).at(:rank) if self.rank.nil?
  self.url            = Base.fix_url((xml).at(:url).inner_html) if (xml).at(:url)

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

  self.images = {}
  (xml/'image').each {|image|
    self.images[image['size']] = image.inner_html if self.images[image['size']].nil?
  }

  self.thumbnail      = self.images['small']
  self.match          = (xml).at(:match).inner_html          if (xml).at(:match)

  # in top artists for tag
  self.count          = xml['count']                         if xml['count']
  self.streamable     = xml['streamable']                    if xml['streamable']

  self.streamable     = (xml).at(:streamable).inner_html == '1' ? 'yes' : 'no' if self.streamable.nil? && (xml).at(:streamable)

  self.tags = (xml/'tag/name').collect(&:inner_html)

  self
end

#similar(force = false) ⇒ Object



135
136
137
# File 'lib/rockstar/artist.rb', line 135

def similar(force=false)
  get_instance("artist.getSimilar", :similar, :artist, {:artist => @name}, force)
end

#top_albums(force = false) ⇒ Object



147
148
149
# File 'lib/rockstar/artist.rb', line 147

def top_albums(force=false)
  get_instance("artist.getTopAlbums", :top_albums, :album, {:artist => @name}, force)
end

#top_fans(force = false) ⇒ Object



139
140
141
# File 'lib/rockstar/artist.rb', line 139

def top_fans(force=false)
  get_instance("artist.getTopFans", :top_fans, :user, {:artist => @name}, force)
end

#top_tags(force = false) ⇒ Object



151
152
153
# File 'lib/rockstar/artist.rb', line 151

def top_tags(force=false)
  get_instance("artist.getTopTags", :top_tags, :tag, {:artist => @name}, force)
end

#top_tracks(force = false) ⇒ Object



143
144
145
# File 'lib/rockstar/artist.rb', line 143

def top_tracks(force=false)
  get_instance("artist.getTopTracks", :top_tracks, :track, {:artist => @name}, force)
end

#user_images(opts = {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rockstar/artist.rb', line 162

def user_images(opts={})
  if mbid.nil?
    opts[:artist] = name
  else
    opts[:mbid] = mbid
  end

  images = []
  image_doc = self.class.fetch_and_parse("artist.getImages", opts, false)
  (image_doc/'image').each do |xml|
    image_sizes = {}
    xml.search('/sizes/size').each do |image|
      image_sizes[image['name']] = image.inner_html
    end

    images << image_sizes
  end

  images
end