class Firstfm::Artist
Attributes
images[RW]
listeners[RW]
mbid[RW]
name[RW]
playcount[RW]
streamable[RW]
url[RW]
Public Class Methods
get_correction(artist)
click to toggle source
# File lib/firstfm/artist.rb, line 46 def self.get_correction(artist) response = get("/2.0/", {:query => {:method => 'artist.getcorrection', :artist => artist, :api_key => Firstfm.config.api_key, :format => :json}}) if response["corrections"] && response["corrections"]["correction"] init_from_hash(response["corrections"]["correction"]["artist"]) rescue nil elsif response.key?("corrections") init_from_hash({"name" => artist}) end end
get_top_tracks(artist, page = 1, limit = 50)
click to toggle source
# File lib/firstfm/artist.rb, line 36 def self.get_top_tracks(artist, page = 1, limit = 50) response = get("/2.0/", {:query => {:method => 'artist.getTopTracks', :artist => artist, :page => page, :limit => limit, :api_key => Firstfm.config.api_key, :format => :json}}) tracks_array = (response and response and response["toptracks"] and response["toptracks"]["track"]) || [] tracks = Track.init_from_array(tracks_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace tracks pager.total_entries = response["toptracks"]["@attr"]["total"].to_i rescue 0 end end
init_from_array(array)
click to toggle source
# File lib/firstfm/artist.rb, line 55 def self.init_from_array(array) return [] unless array.is_a?(Array) array.inject([]) do |arr, artist| arr << init_from_hash(artist) arr end end
init_from_hash(hash)
click to toggle source
# File lib/firstfm/artist.rb, line 63 def self.init_from_hash(hash) return nil unless hash.is_a?(Hash) Artist.new.tap do |artist| artist.name = hash["name"] artist.mbid = hash["mbid"] artist.url = hash["url"] artist.listeners = hash["listeners"].to_i artist.streamable = hash["streamable"] == "1" artist.images = hash["image"] artist.playcount = hash["playcount"].to_i end end
new(params = {})
click to toggle source
# File lib/firstfm/artist.rb, line 11 def initialize(params = {}) @name = params[:name] end
search(artist, page = 1, limit = 50)
click to toggle source
# File lib/firstfm/artist.rb, line 26 def self.search(artist, page = 1, limit = 50) response = get("/2.0/", {:query => {:method => 'artist.search', :artist => artist, :page => page, :limit => limit, :api_key => Firstfm.config.api_key, :format => :json}}) artists_array = (response and response["results"] and response["results"]["artistmatches"] and response["results"]["artistmatches"]["artist"]) || [] artists = Artist.init_from_array(artists_array) WillPaginate::Collection.create(page, limit) do |pager| pager.replace artists pager.total_entries = response['results']['opensearch:totalResults'].to_i rescue 0 end end