lib/triviacrack/api/common.rb in triviacrack-0.6.0 vs lib/triviacrack/api/common.rb in triviacrack-0.7.0
- old
+ new
@@ -1,15 +1,16 @@
-require "json"
-require "faraday"
+# frozen_string_literal: true
-require "triviacrack/errors/request_error"
+require 'json'
+require 'faraday'
+require 'triviacrack/errors/request_error'
+
# Internal: Common methods used for the Trivia Crack API.
module TriviaCrack
module API
module Common
-
# Internal: Makes a GET request to the Trivia Crack API using the set of
# default headers.
#
# url - The URL of the TriviaCrack API resource.
# parameters - The parameters to send with the request.
@@ -37,22 +38,20 @@
end
private
# Internal: The host name of the Trivia Crack API.
- API_HOST = "https://api.preguntados.com"
+ API_HOST = 'https://api.preguntados.com'
# Internal: Constructs the set of headers needed to make requests to the
# Trivia Crack API.
#
# Returns a hash of headers.
def default_headers
- headers = { "Content-Type": "application/json; charset=utf-8" }
+ headers = { 'Content-Type': 'application/json; charset=utf-8' }
- if @session
- headers["Cookie"] = "ap_session=#{@session.session_id}"
- end
+ headers['Cookie'] = "ap_session=#{@session.session_id}" if @session
headers
end
# Internal: Checks the response's code to see if the request was
@@ -61,12 +60,12 @@
# response - Faraday response returned by the API.
#
# 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
+ unless 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}."
+ "Request to #{API_HOST}#{url} failed with code #{response.status}."
end
JSON.parse(response.body)
end
end