Class: RubyDeezer::Track

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#albumObject

Returns the value of attribute album.



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

def album
  @album
end

#artistObject

Returns the value of attribute artist.



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

def artist
  @artist
end

#durationObject

Returns the value of attribute duration.



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

def duration
  @duration
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#rankObject

Returns the value of attribute rank.



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

def rank
  @rank
end

#urlObject

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, options = {})
  per_page = options.delete(:per_page) || 10
  page = options.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