class RubyDeezer::Artist
Attributes
albums[RW]
discography[RW]
id[RW]
image[RW]
name[RW]
similar_artists[RW]
url[RW]
Public Class Methods
find(id, options = [])
click to toggle source
# File lib/ruby_deezer/artist.rb, line 13 def self.find(id, options = []) response = get("/lookup/artist/", {:query => {:id => id, :output => 'json', :options => options.join(",")}}) artist = response ? Artist.init_from_hash(response['artist']) : nil end
init_from_hash(hash)
click to toggle source
# File lib/ruby_deezer/artist.rb, line 34 def self.init_from_hash(hash) return nil unless hash.is_a?(Hash) similar_artists = hash["similar_artists"] || {} similar_artists_array = similar_artists["artist"] || [] discography = hash["discography"] || {} discography_array = discography["album"] || [] Artist.new.tap do |artist| artist.id = hash["id"].to_i artist.name = hash["name"] artist.url = hash["url"] artist.image = hash["image"] artist.similar_artists = similar_artists_array.inject([]) {|arr, hash| arr << init_from_hash(hash); arr} artist.discography = discography_array.inject([]) {|arr, hash| arr << Album.init_from_hash(hash); arr} end end
search(query, options = {})
click to toggle source
# File lib/ruby_deezer/artist.rb, line 18 def self.search(query, options = {}) per_page = options.delete(:per_page) || 10 page = options.delete(:page) || 1 index = (page - 1) * per_page response = get("/search/artist/", {:query => {:q => query, :output => 'json', :index => index, :nb_items => per_page}}) artists = response && response["search"] && response["search"]["artists"] ? response["search"]["artists"]["artist"].inject([]){|arr, hash| arr << Artist.init_from_hash(hash); arr } : [] WillPaginate::Collection.create(page, per_page) do |pager| pager.replace artists pager.total_entries = response['search']['total_results'].to_i rescue 0 end end