lib/triviacrack/api/common.rb in triviacrack-0.4.0 vs lib/triviacrack/api/common.rb in triviacrack-0.5.0
- old
+ new
@@ -1,6 +1,6 @@
-require "unirest"
+require "faraday"
require "triviacrack/errors/request_error"
# Internal: Common methods used for the Trivia Crack API.
module TriviaCrack
@@ -11,14 +11,14 @@
# default headers.
#
# url - The URL of the TriviaCrack API resource.
# parameters - The parameters to send with the request.
#
- # Returns a Unirest Response object with the server's response.
+ # Returns a Faraday Response object with the server's response.
# Raises TriviaCrack:Errors::RequestError if the request fails.
def get(url, parameters: nil)
- response = Unirest.get "#{API_HOST}#{url}", parameters: parameters,
+ response = Faraday.get "#{API_HOST}#{url}", params: parameters,
headers: default_headers
check_response url, response
end
@@ -26,14 +26,14 @@
# default headers.
#
# url - The URL of the TriviaCrack API resource.
# parameters - The parameters to send with the request.
#
- # Returns a Unirest Response object with the server's response.
+ # Returns a Faraday Response object with the server's response.
# Raises TriviaCrack:Errors::RequestError if the request fails.
def post(url, parameters: nil)
- response = Unirest.post "#{API_HOST}#{url}", parameters: parameters,
+ response = Faraday.post "#{API_HOST}#{url}", body: parameters,
headers: default_headers
check_response url, response
end
@@ -57,17 +57,17 @@
end
# Internal: Checks the response's code to see if the request was
# successful
#
- # response - Unirest response returned by the API.
+ # response - Faraday response returned by the API.
#
# Returns the response object.
# Raises TriviaCrack:Errors::RequestError if the request failed.
def check_response(url, response)
- if not response.code.between? 200, 299
- raise TriviaCrack::Errors::RequestError.new(response.code, url, response.body),
- "Request to #{API_HOST}#{url} failed with code #{response.code}."
+ 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
end
end