Skip to content

Commit

Permalink
Adds allowances to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
picandocodigo committed Nov 30, 2020
1 parent 68d94fc commit b5d7f48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This gem requires Ruby version 2.5 or more. Add this line to your application's

gem 'thegamesdb'

And then run:
And run:

$ bundle install

Expand All @@ -49,6 +49,10 @@ To use this library, you'll need to request an API Key [here](https://forums.the
> response = client.platforms
```

**API Key allowances**

The base API Response includes the `remaining_monthly_allowance` for your API key, `extra_allowance` and `allowance_refresh_timer`. These values are updated on the client instance on every request so you can use `client.remaining_monthly_client` to check how many requests the API key has left.

## Usage

The full documentation for the API is available [here](API Documentation: https://api.thegamesdb.net/). Here are the endpoints available on the gem:
Expand Down Expand Up @@ -234,7 +238,7 @@ Pages:

### Platforms

#### /Platforms
#### Platforms

- **[RubyDoc](https://www.rubydoc.info/github/picandocodigo/gamesdb/master/Gamesdb/Platforms#platforms-instance_method)**
- **[Swagger API Documentation](https://api.thegamesdb.net/#/Platforms/Platforms)**
Expand Down
13 changes: 11 additions & 2 deletions lib/thegamesdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Client
BASE_URL = 'https://api.thegamesdb.net/v1/'
IMAGES_BASE_URL = 'https://legacy.thegamesdb.net/banners/'

attr_reader :remaining_monthly_allowance, :extra_allowance, :allowance_refresh_timer

def initialize(api_key)
@api_key = api_key
end
Expand All @@ -39,15 +41,22 @@ def perform_request(url, params = {})
params = params.merge({ apikey: @api_key })
uri = URI(BASE_URL + url)
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri).body
JSON.parse(response)
response = JSON.parse(Net::HTTP.get_response(uri).body)
refresh_allowances(response)
response
rescue StandardError => e
# TODO: Handle errors
raise e
end

private

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

def process_logo(data, id)
logo = data['images'][id.to_s].select { |a| a['type'] == 'clearlogo' }
logo.empty? ? '' : logo.first['filename']
Expand Down
16 changes: 16 additions & 0 deletions test/client_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative './test_helper'

describe 'Gamesdb - client', :vcr do
let(:client) { Gamesdb::Client.new(ENV['GAMESDB_API_KEY']) }

describe 'client' do
it 'should update allowances' do
client.games_by_id(1904)
monthly = client.remaining_monthly_allowance
client.games_by_id(1527)
expect(client.remaining_monthly_allowance).must_equal(monthly - 1)
end
end
end

0 comments on commit b5d7f48

Please sign in to comment.