Class: Gamesdb::Client

Inherits:
Object
  • Object
show all
Includes:
Developers, Games, Genres, Platforms, Publishers
Defined in:
lib/thegamesdb.rb

Overview

Client for TheGamesDB API (thegamesdb.net)

Constant Summary collapse

BASE_URL =
'https://api.thegamesdb.net/v1/'
IMAGES_BASE_URL =
'https://legacy.thegamesdb.net/banners/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Publishers

#publishers

Methods included from Platforms

#platform_api_response, #platform_images, #platforms, #platforms_by_id, #platforms_by_name

Methods included from Genres

#genres

Methods included from Games

#games_by_id, #games_by_name, #games_by_platform_id, #games_images, #games_update

Methods included from Developers

#developers

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



28
29
30
# File 'lib/thegamesdb.rb', line 28

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#allowance_refresh_timerObject (readonly)

Returns the value of attribute allowance_refresh_timer.



26
27
28
# File 'lib/thegamesdb.rb', line 26

def allowance_refresh_timer
  @allowance_refresh_timer
end

#extra_allowanceObject (readonly)

Returns the value of attribute extra_allowance.



26
27
28
# File 'lib/thegamesdb.rb', line 26

def extra_allowance
  @extra_allowance
end

#remaining_monthly_allowanceObject (readonly)

Returns the value of attribute remaining_monthly_allowance.



26
27
28
# File 'lib/thegamesdb.rb', line 26

def remaining_monthly_allowance
  @remaining_monthly_allowance
end

Instance Method Details

#perform_request(url, params = {}) ⇒ Hash

Perform request

Used by every API endpoint, but can also be used manually.

Parameters:

  • url (String)

    Required

  • params (Hash) (defaults to: {})

    optional

Returns:

  • (Hash)

    Parsed JSON response



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/thegamesdb.rb', line 40

def perform_request(url, params = {})
  raise ArgumentError, 'You need to set the API KEY to use the GamesDB API' unless @api_key

  uri = prepare_uri(params, url)
  response = Net::HTTP.get_response(uri)
  check_http_errors(response)
  json_response = JSON.parse(response.body)
  refresh_allowances(json_response)
  json_response
rescue StandardError => e
  raise e
end