Class: Spotify::API::Base

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

Direct Known Subclasses

Album, Artist, Track, User

Constant Summary collapse

BASE_URL =

The API base URL.

"https://api.spotify.com/v1/"
SEARCH_URL =

The API search endpoint.

"#{BASE_URL}search"
MAX_RETRIES =

The max retries limit.

5
FROM_TOKEN =

Restrict value for market variable on search methods.

:from_token

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

Initializes the optional arguments.

Parameters:

  • the (Hash)

    optional arguments.

  • [Fixnum] (Hash)

    a customizable set of options



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

def initialize(args = {})
  @args    = args.except(:timeout, :retries)
  @timeout = args[:timeout].to_i
  @retries = args[:retries].to_i
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/spotify/api/base.rb', line 10

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



10
11
12
# File 'lib/spotify/api/base.rb', line 10

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/spotify/api/base.rb', line 10

def response
  @response
end

#retriesObject (readonly)

Returns the value of attribute retries.



10
11
12
# File 'lib/spotify/api/base.rb', line 10

def retries
  @retries
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



10
11
12
# File 'lib/spotify/api/base.rb', line 10

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/spotify/api/base.rb', line 10

def url
  @url
end

Instance Method Details

#bodyHash

Parses the response to JSON to get more flexible.

Returns:

  • (Hash)

    the parsed response.



132
133
134
# File 'lib/spotify/api/base.rb', line 132

def body
  @response = JSON.parse(response)
end

#define_response(&block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/spotify/api/base.rb', line 136

def define_response(&block)
  response = body

  # The if statement covers a case for Users requests.
  if response.class != Spotify::Models::Error
    if response['error']
      response = Spotify::Models::Error.default(response['error'])
    else
      response = yield
    end
  end

  response
end

#get(url, params = {}) ⇒ Object

Performs a request on the given url with its arguments.

Parameters:

  • url (String)

    the request API url.

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

    the request arguments.



51
52
53
54
55
56
57
# File 'lib/spotify/api/base.rb', line 51

def get(url, params = {})
  @url     = url
  @params  = params
  @request = prepare_request

  make_request
end

#make_request(attempts = 0) ⇒ Object

Handles the request behavior.

Parameters:

  • attempts (Fixnum) (defaults to: 0)

    the current attempt number.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/spotify/api/base.rb', line 87

def make_request(attempts = 0)
  @attempts = attempts

  if attempts > MAX_RETRIES || attempts > @retries
    set_response(Spotify::Models::Error.max_retries, true)

  else
    begin
      if @timeout > 0
        run_with_timeout { request.call }
      else
        request.call
      end

    rescue Timeout::Error
      set_response(Spotify::Models::Error.timeout)
    rescue Exception => e
      puts e.message
      puts e.backtrace
      set_response(Spotify::Models::Error.unexpected_error)
    end
  end

end

#prepare_requestObject

Prepares the Proc instructions to perform the request.



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

def prepare_request
  lambda do
    sleep(3)
    uri       = URI(@url)
    uri.query = URI.encode_www_form(@params)
    @response = Net::HTTP.get(uri)
  end
end

#run_with_timeout(&block) ⇒ Object

Performs the request respecting the given timeout.

Parameters:

  • block (Proc)

    a block of code to perform the request.



64
65
66
67
68
# File 'lib/spotify/api/base.rb', line 64

def run_with_timeout(&block)
  Timeout.timeout(@timeout) do
    yield
  end
end

#set_response(error, force = false) ⇒ Object

Sets the response in case of something goes wrong during the extraction process.

Parameters:

  • error (String)

    the raised error during the extraction.

  • force (Boolean) (defaults to: false)

    if should return error independent of retries.



119
120
121
122
123
124
125
# File 'lib/spotify/api/base.rb', line 119

def set_response(error, force = false)
  if force == false && @retries > 0
    make_request(@attempts + 1)
  else
    @response = { error: error }.to_json
  end
end