Class: Spotify::API::Track

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

Constant Summary collapse

TRACKS_URL =

API endpoint for tracks.

"#{BASE_URL}tracks"

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 tracks 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/track.rb', line 20

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

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

Gets a track.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:

  • (Full::Track)

    the extracted track.



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

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

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

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

Gets several 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 tracks.



55
56
57
58
59
60
61
62
# File 'lib/spotify/api/track.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, :market)

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

Instance Method Details

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

Gets the tracks related to the given parameters.

Parameters:

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

    the search arguments.

  • [Fixnum] (Hash)

    a customizable set of options

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/spotify/api/track.rb', line 73

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

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

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

Gets a track.

Parameters:

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

    the search arguments.

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Full::Track)

    the extracted track.



97
98
99
100
101
102
103
# File 'lib/spotify/api/track.rb', line 97

def search_by_id(args = {})
  get(TRACKS_URL + '/' + args[:id].to_s, args.slice(:market))

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

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

Gets several 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 tracks.



115
116
117
118
119
120
121
122
123
# File 'lib/spotify/api/track.rb', line 115

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

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