Skip to content

Commit

Permalink
Refactors perform_request, renames http_error to check_http_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
picandocodigo committed Jan 20, 2023
1 parent 60734a3 commit 4991449
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/thegamesdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ def initialize(api_key)
def perform_request(url, params = {})
raise ArgumentError, 'You need to set the API KEY to use the GamesDB API' unless @api_key

params = params.merge({ apikey: @api_key })
uri = URI(BASE_URL + url)
uri.query = URI.encode_www_form(params)
uri = prepare_uri(params, url)
response = Net::HTTP.get_response(uri)
http_error(response)
check_http_errors(response)
json_response = JSON.parse(response.body)

refresh_allowances(json_response)
json_response
rescue StandardError => e
Expand All @@ -55,14 +52,21 @@ def perform_request(url, params = {})

private

def prepare_uri(params, url)
params = params.merge({ apikey: @api_key })
uri = URI(BASE_URL + url)
uri.query = URI.encode_www_form(params)
uri
end

def refresh_allowances(response)
@remaining_monthly_allowance = response['remaining_monthly_allowance']
@extra_allowance = response['extra_allowance']
@allowance_refresh_timer = response['allowance_refresh_timer']
end

# TODO: More granular errors
def http_error(response)
def check_http_errors(response)
raise Gamesdb::Error.new(response.code, response.message) if response.code.to_i >= 300
end

Expand Down

0 comments on commit 4991449

Please sign in to comment.