Class: Spotify::API::Artist

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

Constant Summary collapse

ARTISTS_URL =

API endpoint for artists.

"#{BASE_URL}artists"

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

.albums(args = {}) ⇒ Paging

Get an artist’s albums.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Paging)

    an array containing the extracted artist’s albums.



107
108
109
110
111
112
113
114
# File 'lib/spotify/api/artist.rb', line 107

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

  args           = args.slice(:id, :album_type, :market, :limit, :offset)
  service_params = args.slice(:timeout, :retries)

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

Get an artist’s related artists.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Array<Full::Artist>)

    an array containing the extracted artist’s related artists.



91
92
93
94
95
96
# File 'lib/spotify/api/artist.rb', line 91

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

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

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

Gets the artists 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/artist.rb', line 20

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

  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::Artist

Gets an artist.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Full::Artist)

    the extracted artist.



38
39
40
41
42
43
# File 'lib/spotify/api/artist.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::Artist>

Gets several artists.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Array<Full::Artist>)

    an array containing the extracted artists.



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

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

.top_tracks(args = {}) ⇒ Array<Full::Track>

Get an artist’s top tracks.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Array<Full::Track>)

    an array containing the extracted artist’s top tracks.



74
75
76
77
78
79
# File 'lib/spotify/api/artist.rb', line 74

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

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

Instance Method Details

#albums(args = {}) ⇒ Paging

Get an artist’s albums.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Paging)

    an array containing the extracted artist’s related artists.



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/spotify/api/artist.rb', line 232

def albums(args = {})
  url    = ARTISTS_URL + '/' + args[:id].to_s + '/albums'
  params = args.slice(:album_type, :market, :limit, :offset)

  get(url, params)

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

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

Get an artist’s related artists.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Array<Full::Artist>)

    an array containing the extracted artist’s related artists.



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/spotify/api/artist.rb', line 210

def related_artists(args = {})
  url = ARTISTS_URL + '/' + args[:id].to_s + '/related-artists'

  get(url)

  define_response do
    response["artists"].map do |artist|
      Spotify::Models::Full::Artist.new(artist)
    end
  end
end

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

Gets the artists related to the given parameters.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/spotify/api/artist.rb', line 125

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::Full::Artist

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

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

Gets an artist.

Parameters:

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

    the search arguments.

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Full::Artist)

    the extracted artist.



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

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

  get(url)

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

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

Gets several artists.

Parameters:

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

    the search arguments.

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Array<Full::Artist>)

    an array containing the extracted artists.



167
168
169
170
171
172
173
174
175
# File 'lib/spotify/api/artist.rb', line 167

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

  define_response do
    response["artists"].map do |artist|
      Spotify::Models::Full::Artist.new(artist)
    end
  end
end

#top_tracks(args = {}) ⇒ Array<Full::Track>

Get an artist’s top tracks.

Parameters:

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

    the search arguments.

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Array<Full::Track>)

    an array containing the extracted artist’s top tracks.



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/spotify/api/artist.rb', line 187

def top_tracks(args = {})
  url    = ARTISTS_URL + '/' + args[:id].to_s + '/top-tracks'
  params = args.slice(:country)

  get(url, params)

  define_response do
    response["tracks"].map do |track|
      Spotify::Models::Full::Track.new(track)
    end
  end
end