diff --git a/README.md b/README.md index fb89a52..08c57f4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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)** diff --git a/lib/thegamesdb.rb b/lib/thegamesdb.rb index c793e11..c338040 100644 --- a/lib/thegamesdb.rb +++ b/lib/thegamesdb.rb @@ -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 @@ -39,8 +41,9 @@ 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 @@ -48,6 +51,12 @@ def perform_request(url, params = {}) 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'] diff --git a/test/client_test.rb b/test/client_test.rb new file mode 100644 index 0000000..f5fe3ee --- /dev/null +++ b/test/client_test.rb @@ -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