class GiantBombApi::Client

Constants

API_URL

Attributes

api_key[RW]

Public Class Methods

new(api_key: nil, options: {}) click to toggle source
# File lib/giant_bomb_api/client.rb, line 13
def initialize(api_key: nil, options: {})
  @api_key = api_key
  @options = options
end

Public Instance Methods

send_request(giant_bomb_api_request) click to toggle source
# File lib/giant_bomb_api/client.rb, line 18
def send_request(giant_bomb_api_request)
  connection = Faraday.new(url: API_URL) do |faraday|
    faraday.request  :url_encoded
    faraday.use      FaradayMiddleware::FollowRedirects
    faraday.adapter  Faraday.default_adapter
  end

  response = connection.get do |req|
    req.url giant_bomb_api_request.end_point
    req.options[:timeout] = 5
    req.options[:open_timeout] = 2
    req.params = giant_bomb_api_request.params.merge(format: 'json', api_key: api_key)
  end 

  response_json = JSON.parse(response.body)

  if(response_json["status_code"] == 1)
    response = handle_success_response(response_json)
    response.request = giant_bomb_api_request
    response
  else
    handle_error_response(response_json)
  end
end

Private Instance Methods

handle_error_response(response_json) click to toggle source
# File lib/giant_bomb_api/client.rb, line 49
def handle_error_response(response_json)
  raise Exception::ApiError.new(response_json["status_code"], response_json["error"])
end
handle_success_response(response_json) click to toggle source
# File lib/giant_bomb_api/client.rb, line 45
def handle_success_response(response_json)
  GiantBombApi::Response.new response_json
end