Class: Scrobbler::Artist

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key=, connection, get, maybe_streamable_attribute, maybe_streamable_node, post_request, request, sanitize, secret=

Constructor Details

#initialize(name, data = {}) ⇒ Artist

Returns a new instance of Artist.

Raises:

  • (ArgumentError)


106
107
108
109
110
111
# File 'lib/scrobbler/artist.rb', line 106

def initialize(name, data = {})
  super()
  raise ArgumentError, "Name is required" if name.blank?
  @name = name
  populate_data(data)
end

Instance Attribute Details

#chartpositionObject

Returns the value of attribute chartposition.



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

def chartposition
  @chartposition
end

#countObject

Returns the value of attribute count.



65
66
67
# File 'lib/scrobbler/artist.rb', line 65

def count
  @count
end

#listenersObject

Returns the value of attribute listeners.



67
68
69
# File 'lib/scrobbler/artist.rb', line 67

def listeners
  @listeners
end

#matchObject

Returns the value of attribute match.



67
68
69
# File 'lib/scrobbler/artist.rb', line 67

def match
  @match
end

#mbidObject

Returns the value of attribute mbid.



65
66
67
# File 'lib/scrobbler/artist.rb', line 65

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



65
66
67
# File 'lib/scrobbler/artist.rb', line 65

def name
  @name
end

#playcountObject

Returns the value of attribute playcount.



65
66
67
# File 'lib/scrobbler/artist.rb', line 65

def playcount
  @playcount
end

#rankObject

Returns the value of attribute rank.



65
66
67
# File 'lib/scrobbler/artist.rb', line 65

def rank
  @rank
end

#tagcountObject

Returns the value of attribute tagcount.



67
68
69
# File 'lib/scrobbler/artist.rb', line 67

def tagcount
  @tagcount
end

#urlObject

Returns the value of attribute url.



65
66
67
# File 'lib/scrobbler/artist.rb', line 65

def url
  @url
end

Class Method Details

.data_from_xml(xml, o = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/scrobbler/artist.rb', line 76

def data_from_xml(xml, o={})
  data = {}

  # Get all information from the root's children nodes
  xml.children.each do |child|
    data[:playcount] = child.content.to_i if child.name == 'playcount'
    data[:mbid] = child.content if child.name == 'mbid'
    data[:url] = child.content if child.name == 'url'
    data[:match] = child.content.to_i if child.name == 'match'
    data[:tagcount] = child.content.to_i if child.name == 'tagcount'
    data[:chartposition] = child.content if child.name == 'chartposition'
    data[:name] = child.content if child.name == 'name'
    maybe_streamable_node(data, child)
    maybe_image_node(data, child)
  end        
  
  # If we have not found anything in the content of this node yet then
  # this must be a simple artist node which has the name of the artist
  # as its content
  data[:name] = xml.content if data == {}
  
  # Get all information from the root's attributes
  data[:name] = xml['name'] if xml['name']
  data[:rank] = xml['rank'].to_i if xml['rank']
  maybe_streamable_attribute data, xml
  data[:mbid] = xml['mbid'] if xml['mbid']
  data
end

.new_from_xml(xml, o = {}) ⇒ Object



70
71
72
73
74
# File 'lib/scrobbler/artist.rb', line 70

def new_from_xml(xml, o={})
  data = data_from_xml(xml, o)
  return nil if data[:name].nil?
  Artist.new(data[:name], data)
end

Instance Method Details

#==(otherArtist) ⇒ Object



157
158
159
160
161
162
# File 'lib/scrobbler/artist.rb', line 157

def ==(otherArtist)
  if otherArtist.is_a?(Scrobbler::Artist)
    return (@name == otherArtist.name)
  end
  false
end

#current_events(format = :ics) ⇒ Object

TODO:

Use the API function and parse that into a common ruby structure

Get the URL to the ical or rss representation of the current events that a artist will play

Raises:

  • (ArgumentError)


131
132
133
134
135
# File 'lib/scrobbler/artist.rb', line 131

def current_events(format=:ics)
  format = :ics if format.to_s == 'ical'
  raise ArgumentError unless ['ics', 'rss'].include?(format.to_s)
  "#{API_URL.chop}/2.0/artist/#{CGI::escape(@name)}/events.#{format}"
end

#load_infoObject

Get the metadata



115
116
117
118
119
120
121
122
123
124
# File 'lib/scrobbler/artist.rb', line 115

def load_info
    doc = Base.request('artist.getinfo', {'artist' => @name})
    doc.root.children.each do |child|
        next unless child.name == 'artist'
        data = self.class.data_from_xml(child)
        populate_data(data)
        @info_loaded = true
        break
    end
end

#similar(force = false) ⇒ Object



137
138
139
# File 'lib/scrobbler/artist.rb', line 137

def similar(force=false)
  get_response('artist.getsimilar', :similar, 'similarartists', 'artist', {'artist' => @name}, force)
end

#top_albums(force = false) ⇒ Object



149
150
151
# File 'lib/scrobbler/artist.rb', line 149

def top_albums(force=false)
  get_response('artist.gettopalbums', :top_albums, 'topalbums', 'album', {'artist'=>@name}, force)
end

#top_fans(force = false) ⇒ Object



141
142
143
# File 'lib/scrobbler/artist.rb', line 141

def top_fans(force=false)
  get_response('artist.gettopfans', :top_fans, 'topfans', 'user', {'artist' => @name}, force)
end

#top_tags(force = false) ⇒ Object



153
154
155
# File 'lib/scrobbler/artist.rb', line 153

def top_tags(force=false)
  get_response('artist.gettoptags', :top_tags, 'toptags', 'tag', {'artist' => @name}, force)
end

#top_tracks(force = false) ⇒ Object



145
146
147
# File 'lib/scrobbler/artist.rb', line 145

def top_tracks(force=false)
  get_response('artist.gettoptracks', :top_tracks, 'toptracks', 'track', {'artist'=>@name}, force)
end