Class: RubyDeezer::Artist
- Inherits:
-
Object
- Object
- RubyDeezer::Artist
- Includes:
- HTTParty
- Defined in:
- lib/ruby_deezer/artist.rb
Instance Attribute Summary collapse
-
#discography ⇒ Object
(also: #albums)
Returns the value of attribute discography.
-
#id ⇒ Object
Returns the value of attribute id.
-
#image ⇒ Object
Returns the value of attribute image.
-
#name ⇒ Object
Returns the value of attribute name.
-
#similar_artists ⇒ Object
Returns the value of attribute similar_artists.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .find(id, options = []) ⇒ Object
- .init_from_hash(hash) ⇒ Object
- .search(query, options = {}) ⇒ Object
Instance Attribute Details
#discography ⇒ Object 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 |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'lib/ruby_deezer/artist.rb', line 9 def id @id end |
#image ⇒ Object
Returns the value of attribute image.
9 10 11 |
# File 'lib/ruby_deezer/artist.rb', line 9 def image @image end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/ruby_deezer/artist.rb', line 9 def name @name end |
#similar_artists ⇒ Object
Returns the value of attribute similar_artists.
9 10 11 |
# File 'lib/ruby_deezer/artist.rb', line 9 def similar_artists @similar_artists end |
#url ⇒ Object
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, = []) response = get("/lookup/artist/", {:query => {:id => id, :output => 'json', :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, = {}) per_page = .delete(:per_page) || 10 page = .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 |