Class: GiantBombApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/giant_bomb_api/client.rb

Constant Summary collapse

API_URL =
'https://www.giantbomb.com/api/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, options: {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/giant_bomb_api/client.rb', line 13

def initialize(api_key: nil, options: {})
  @api_key = api_key
  @options = options
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/giant_bomb_api/client.rb', line 9

def api_key
  @api_key
end

Instance Method Details

#send_request(giant_bomb_api_request) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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