Class: RubyDeezer::Album
- Inherits:
-
Object
- Object
- RubyDeezer::Album
- Includes:
- HTTParty
- Defined in:
- lib/ruby_deezer/album.rb
Instance Attribute Summary collapse
-
#artist ⇒ Object
Returns the value of attribute artist.
-
#id ⇒ Object
Returns the value of attribute id.
-
#image ⇒ Object
Returns the value of attribute image.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nb_disks ⇒ Object
Returns the value of attribute nb_disks.
-
#nb_tracks ⇒ Object
Returns the value of attribute nb_tracks.
-
#tracks ⇒ Object
Returns the value of attribute tracks.
-
#url ⇒ Object
Returns the value of attribute url.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
- .find(id, options = []) ⇒ Object
- .init_from_hash(hash) ⇒ Object
- .search(query, options = {}) ⇒ Object
Instance Attribute Details
#artist ⇒ Object
Returns the value of attribute artist.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def artist @artist end |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def id @id end |
#image ⇒ Object
Returns the value of attribute image.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def image @image end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def name @name end |
#nb_disks ⇒ Object
Returns the value of attribute nb_disks.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def nb_disks @nb_disks end |
#nb_tracks ⇒ Object
Returns the value of attribute nb_tracks.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def nb_tracks @nb_tracks end |
#tracks ⇒ Object
Returns the value of attribute tracks.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def tracks @tracks end |
#url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def url @url end |
#year ⇒ Object
Returns the value of attribute year.
9 10 11 |
# File 'lib/ruby_deezer/album.rb', line 9 def year @year end |
Class Method Details
.find(id, options = []) ⇒ Object
11 12 13 14 |
# File 'lib/ruby_deezer/album.rb', line 11 def self.find(id, = []) response = get("/lookup/album/", {:query => {:id => id, :output => 'json', :options => .join(",")}}) artist = response ? Album.init_from_hash(response['album']) : nil end |
.init_from_hash(hash) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby_deezer/album.rb', line 32 def self.init_from_hash(hash) return nil unless hash.is_a?(Hash) tracks = hash["tracks"] || {} tracks_array = tracks["track"] || [] artist = hash["artist"] || {} Album.new.tap do |album| album.id = hash["id"].to_i album.name = hash["name"] album.url = hash["url"] album.image = hash["image"] album.nb_tracks = hash["nb_tracks"].to_i album.nb_disks = hash["nb_disks"].to_i album.year = hash["year"].to_i album.tracks = tracks_array.inject([]) {|arr, track| arr << Track.init_from_hash(track); arr} album.artist = Artist.init_from_hash(artist) end end |
.search(query, options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby_deezer/album.rb', line 16 def self.search(query, = {}) per_page = .delete(:per_page) || 10 page = .delete(:page) || 1 index = (page - 1) * per_page response = get("/search/album/", {:query => {:q => query, :output => 'json', :index => index, :nb_items => per_page}}) albums = response && response["search"] && response["search"]["albums"] ? response["search"]["albums"]["album"].inject([]){|arr, hash| arr << Album.init_from_hash(hash); arr } : [] WillPaginate::Collection.create(page, per_page) do |pager| pager.replace albums pager.total_entries = response['search']['total_results'].to_i rescue 0 end end |