Class: Spotify::API::Album

Inherits:
Base
  • Object
show all
Defined in:
lib/spotify/api/album.rb

Constant Summary collapse

ALBUMS_URL =

API endpoint for albums.

"#{BASE_URL}albums"

Constants inherited from Base

Base::BASE_URL, Base::FROM_TOKEN, Base::MAX_RETRIES, Base::SEARCH_URL

Instance Attribute Summary

Attributes inherited from Base

#params, #request, #response, #retries, #timeout, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#body, #define_response, #get, #initialize, #make_request, #prepare_request, #run_with_timeout, #set_response

Constructor Details

This class inherits a constructor from Spotify::API::Base

Class Method Details

.search(args = {}) ⇒ Spotify::Models::Paging

Gets the albums related to the given parameters.

Parameters:

  • args (Hash) (defaults to: {})

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:



20
21
22
23
24
25
26
27
# File 'lib/spotify/api/album.rb', line 20

def self.search(args = {})
  args[:type] = :album

  service_params = args.slice(:timeout, :retries)
  args           = args.slice(:q, :market, :type, :limit, :offset)

  self.new(service_params).search(args)
end

.search_by_id(args = {}) ⇒ Full::Album

Gets an album.

Parameters:

  • args (Hash) (defaults to: {})

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Full::Album)

    the extracted album.



38
39
40
41
42
43
# File 'lib/spotify/api/album.rb', line 38

def self.search_by_id(args = {})
  service_params = args.slice(:timeout, :retries)
  args           = args.slice(:id)

  self.new(service_params).search_by_id(args)
end

.search_by_ids(args = {}) ⇒ Array<Full::Album>

Gets an album.

Parameters:

  • args (Hash) (defaults to: {})

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Array<Full::Album>)

    the extracted album.



54
55
56
57
58
59
60
61
# File 'lib/spotify/api/album.rb', line 54

def self.search_by_ids(args = {})
  args[:ids] = Array(args[:ids]).join(',')

  service_params = args.slice(:timeout, :retries)
  args           = args.slice(:ids)

  self.new(service_params).search_by_ids(args)
end

.tracks(args = {}) ⇒ Full::Album

Gets an album.

Parameters:

  • args (Hash) (defaults to: {})

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Full::Album)

    the extracted album.



72
73
74
75
76
77
# File 'lib/spotify/api/album.rb', line 72

def self.tracks(args = {})
  service_params = args.slice(:timeout, :retries)
  args           = args.slice(:id, :limit, :offset, :market)

  self.new(service_params).tracks(args)
end

Instance Method Details

#search(args = {}) ⇒ Spotify::Models::Paging

Gets the albums related to the given parameters.

Parameters:

  • args (Hash) (defaults to: {})

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/spotify/api/album.rb', line 88

def search(args = {})
  if args[:market].to_s.to_sym == FROM_TOKEN
    # TODO: Authorization.

    return Spotify::API::Errors::NOT_AVAILABLE
  end

  get(SEARCH_URL, args)

  define_response do
    klass = Spotify::Models::Simplified::Album

    Spotify::Models::Paging.new(response["albums"], klass)
  end
end

#search_by_id(args = {}) ⇒ Full::Album

Gets an album.

Parameters:

  • args (Hash) (defaults to: {})

    the search arguments.

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Full::Album)

    the extracted album.



111
112
113
114
115
116
117
118
119
# File 'lib/spotify/api/album.rb', line 111

def search_by_id(args = {})
  url = ALBUMS_URL + '/' + args[:id].to_s

  get(url)

  define_response do
    Spotify::Models::Full::Album.new(response)
  end
end

#search_by_ids(args = {}) ⇒ Array<Full::Album>

Gets several albums.

Parameters:

  • args (Hash) (defaults to: {})

    the search arguments.

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Array<Full::Album>)

    an array containing the extracted albums.



130
131
132
133
134
135
136
137
138
# File 'lib/spotify/api/album.rb', line 130

def search_by_ids(args = {})
  get(ALBUMS_URL, args)

  define_response do
    response["albums"].map do |album|
      Spotify::Models::Full::Album.new(album)
    end
  end
end

#tracks(args = {}) ⇒ Paging

Gets the album’s tracks.

Parameters:

  • args (Hash) (defaults to: {})

    the search arguments.

Returns:

  • (Paging)

    an array containing album’s tracks.



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/spotify/api/album.rb', line 147

def tracks(args = {})
  url    = ALBUMS_URL + '/' + args[:id] + '/tracks'
  params = args.slice(:limit, :offset, :market)

  get(url, params)

  define_response do
    klass = Spotify::Models::Simplified::Track

    Spotify::Models::Paging.new(response, klass)
  end
end