Class: Spotify::API::Base
- Inherits:
-
Object
- Object
- Spotify::API::Base
- Defined in:
- lib/spotify/api/base.rb
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
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#body ⇒ Hash
Parses the response to JSON to get more flexible.
- #define_response(&block) ⇒ Object
-
#get(url, params = {}) ⇒ Object
Performs a request on the given url with its arguments.
-
#initialize(args = {}) ⇒ Base
constructor
Initializes the optional arguments.
-
#make_request(attempts = 0) ⇒ Object
Handles the request behavior.
-
#prepare_request ⇒ Object
Prepares the Proc instructions to perform the request.
-
#run_with_timeout(&block) ⇒ Object
Performs the request respecting the given timeout.
-
#set_response(error, force = false) ⇒ Object
Sets the response in case of something goes wrong during the extraction process.
Constructor Details
#initialize(args = {}) ⇒ Base
Initializes the optional arguments.
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
#params ⇒ Object (readonly)
Returns the value of attribute params.
10 11 12 |
# File 'lib/spotify/api/base.rb', line 10 def params @params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
10 11 12 |
# File 'lib/spotify/api/base.rb', line 10 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
10 11 12 |
# File 'lib/spotify/api/base.rb', line 10 def response @response end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
10 11 12 |
# File 'lib/spotify/api/base.rb', line 10 def retries @retries end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/spotify/api/base.rb', line 10 def timeout @timeout end |
#url ⇒ Object (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
#body ⇒ Hash
Parses the response to JSON to get more flexible.
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.
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.
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. puts e.backtrace set_response(Spotify::Models::Error.unexpected_error) end end end |
#prepare_request ⇒ Object
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.
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.
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 |