Class: RubyDeezer::Album

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#artistObject

Returns the value of attribute artist.



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

def artist
  @artist
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#imageObject

Returns the value of attribute image.



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

def image
  @image
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#nb_disksObject

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_tracksObject

Returns the value of attribute nb_tracks.



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

def nb_tracks
  @nb_tracks
end

#tracksObject

Returns the value of attribute tracks.



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

def tracks
  @tracks
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#yearObject

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, options = [])
  response = get("/lookup/album/", {:query => {:id => id, :output => 'json', :options => 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, options = {})
  per_page = options.delete(:per_page) || 10
  page = options.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