Class: RubyDeezer::Artist

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ruby_deezer/artist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#discographyObject Also known as: albums

Returns the value of attribute discography.



9
10
11
# File 'lib/ruby_deezer/artist.rb', line 9

def discography
  @discography
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/ruby_deezer/artist.rb', line 9

def id
  @id
end

#imageObject

Returns the value of attribute image.



9
10
11
# File 'lib/ruby_deezer/artist.rb', line 9

def image
  @image
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/ruby_deezer/artist.rb', line 9

def name
  @name
end

#similar_artistsObject

Returns the value of attribute similar_artists.



9
10
11
# File 'lib/ruby_deezer/artist.rb', line 9

def similar_artists
  @similar_artists
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/ruby_deezer/artist.rb', line 9

def url
  @url
end

Class Method Details

.find(id, options = []) ⇒ Object



13
14
15
16
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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 = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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