lib/lol/request.rb in ruby-lol-0.11.4 vs lib/lol/request.rb in ruby-lol-0.11.5
- old
+ new
@@ -60,33 +60,34 @@
# Returns just a path from a full api url
# @return [String]
def clean_url(url)
uri = URI.parse(url)
- uri.query = CGI.parse(uri.query).reject { |k| k == 'api_key' }.to_query
+ uri.query = CGI.parse(uri.query || '').reject { |k| k == 'api_key' }.to_query
uri.to_s
end
# Calls the API via HTTParty and handles errors
# @param url [String] the url to call
# @param verb [Symbol] HTTP verb to use. Defaults to :get
# @param body [Hash] Body for POST request
# @param options [Hash] Options passed to HTTParty
# @return [String] raw response of the call
def perform_request url, verb = :get, body = {}, options = {}
- if cached? && result = store.get("#{clean_url(url)}#{options.inspect}")
+ can_cache = [:post, :put].include?(verb) ? false : cached?
+ if can_cache && result = store.get("#{clean_url(url)}#{options.inspect}")
return JSON.parse(result)
end
- params = verb == :post ? [url, options.merge({body: body.to_json})] : url
+ params = [:post, :put].include?(verb) ? [url, options.merge({body: body.to_json})] : url
response = self.class.send(verb, *params)
if response.respond_to?(:code) && !(200...300).include?(response.code)
raise NotFound.new("404 Not Found") if response.not_found?
raise TooManyRequests.new('429 Rate limit exceeded') if response.code == 429
raise InvalidAPIResponse.new(url, response)
end
- store.setex "#{clean_url(url)}#{options.inspect}", ttl, response.to_json if cached?
+ store.setex "#{clean_url(url)}#{options.inspect}", ttl, response.to_json if can_cache
response
end
# @return [Redis] returns the cache store