lib/triviacrack/api/common.rb in triviacrack-0.5.1 vs lib/triviacrack/api/common.rb in triviacrack-0.6.0
- old
+ new
@@ -1,5 +1,6 @@
+require "json"
require "faraday"
require "triviacrack/errors/request_error"
# Internal: Common methods used for the Trivia Crack API.
@@ -14,12 +15,11 @@
# parameters - The parameters to send with the request.
#
# Returns a Faraday Response object with the server's response.
# Raises TriviaCrack:Errors::RequestError if the request fails.
def get(url, parameters: nil)
- response = Faraday.get "#{API_HOST}#{url}", params: parameters,
- headers: default_headers
+ response = Faraday.get "#{API_HOST}#{url}", parameters, default_headers
check_response url, response
end
# Internal: Makes a POST request to the Trivia Crack API using the set of
@@ -29,12 +29,11 @@
# parameters - The parameters to send with the request.
#
# Returns a Faraday Response object with the server's response.
# Raises TriviaCrack:Errors::RequestError if the request fails.
def post(url, parameters: nil)
- response = Faraday.post "#{API_HOST}#{url}", body: parameters,
- headers: default_headers
+ response = Faraday.post "#{API_HOST}#{url}", parameters, default_headers
check_response url, response
end
private
@@ -59,18 +58,18 @@
# Internal: Checks the response's code to see if the request was
# successful
#
# response - Faraday response returned by the API.
#
- # Returns the response object.
+ # Returns the response body parsed from JSON
# Raises TriviaCrack:Errors::RequestError if the request failed.
def check_response(url, response)
if not response.status.between? 200, 299
raise TriviaCrack::Errors::RequestError.new(response.status, url, response.body),
"Request to #{API_HOST}#{url} failed with code #{response.status}."
end
- response
+ JSON.parse(response.body)
end
end
end
end