Class: RubyDeezer::Track
- Inherits:
-
Object
- Object
- RubyDeezer::Track
- Includes:
- HTTParty
- Defined in:
- lib/ruby_deezer/track.rb
Instance Attribute Summary collapse
-
#album ⇒ Object
Returns the value of attribute album.
-
#artist ⇒ Object
Returns the value of attribute artist.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#rank ⇒ Object
Returns the value of attribute rank.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
Instance Attribute Details
#album ⇒ Object
Returns the value of attribute album.
9 10 11 |
# File 'lib/ruby_deezer/track.rb', line 9 def album @album end |
#artist ⇒ Object
Returns the value of attribute artist.
9 10 11 |
# File 'lib/ruby_deezer/track.rb', line 9 def artist @artist end |
#duration ⇒ Object
Returns the value of attribute duration.
9 10 11 |
# File 'lib/ruby_deezer/track.rb', line 9 def duration @duration end |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'lib/ruby_deezer/track.rb', line 9 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/ruby_deezer/track.rb', line 9 def name @name end |
#rank ⇒ Object
Returns the value of attribute rank.
9 10 11 |
# File 'lib/ruby_deezer/track.rb', line 9 def rank @rank end |
#url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/ruby_deezer/track.rb', line 9 def url @url end |
Class Method Details
.find(id) ⇒ Object
11 12 13 14 |
# File 'lib/ruby_deezer/track.rb', line 11 def self.find(id) response = get("/lookup/track/", {:query => {:id => id, :output => 'json'}}) artist = response ? Track.init_from_hash(response['track']) : nil end |
.init_from_hash(hash) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ruby_deezer/track.rb', line 32 def self.init_from_hash(hash) return nil unless hash.is_a?(Hash) Track.new.tap do |track| track.id = hash["id"].to_i track.name = hash["name"] track.url = hash["url"] track.duration = hash["duration"].to_i track.rank = hash["rank"].to_i if hash["artist"] track.artist = Artist.init_from_hash(hash["artist"]) end if hash["album"] track.album = Album.init_from_hash(hash["album"]) end 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/track.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/track/", {:query => {:q => query, :output => 'json', :index => index, :nb_items => per_page}}) tracks = response && response["search"] && response["search"]["tracks"] ? response["search"]["tracks"]["track"].inject([]){|arr, hash| arr << Track.init_from_hash(hash); arr } : [] WillPaginate::Collection.create(page, per_page) do |pager| pager.replace tracks pager.total_entries = response['search']['total_results'].to_i rescue 0 end end |