Skip to content

Commit

Permalink
Add limits tracking (#11)
Browse files Browse the repository at this point in the history
* Implement api_key

* rubocop update

* change slice with delete

* upd gemfilelock

* Compact

* Rubocop rename methods for `pro` and `locked`

* Fixed tests

* hound rubocop v

* hound test

* improvements for tests

* Style/FrozenStringLiteralComment

* Update README for Premium Tier API

* Add limits tracking
  • Loading branch information
vergilet authored Oct 1, 2019
1 parent 0fb2d00 commit 40708af
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
open_dota_api (0.4.0)
open_dota_api (0.4.1)
hashable (~> 0.1.2)
httparty (~> 0.17.1)

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ OpenDotaApi.explorer(league_id)

**Please follow the link to [Get the KEY](https://www.opendota.com/api-keys).**

#### Requests Limit:

To check the request limit for Free Tier:
```ruby
OpenDotaApi.limits
```
*Output:*
```ruby
{
per_min: "0", # request left for per minute
per_month: "49878" # request left for per month
}
```



#### Using the API_KEY

After you get you personal key you have two options:
Expand Down
2 changes: 1 addition & 1 deletion lib/open_dota_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module OpenDotaApi
extend SingleForwardable
def_delegators :client, :leagues, :teams, :matches, :heroes, :pro_players, :explorer
def_delegators :client, :limits, :leagues, :teams, :matches, :heroes, :pro_players, :explorer

def self.client
@client ||= Client.new
Expand Down
65 changes: 36 additions & 29 deletions lib/open_dota_api/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'open_dota_api/connection'
require 'open_dota_api/health'
require 'open_dota_api/league'
require 'open_dota_api/team'
require 'open_dota_api/match'
Expand All @@ -10,46 +11,41 @@ module OpenDotaApi
class Client
INTERFACE = 'api'.freeze

def leagues(attributes = {})
leagues_data = request(League::ENDPOINT, query_params: { api_key: attributes.delete(:api_key) }.compact)
return {} unless leagues_data.success?
def limits(attributes = {})
return @limits unless @limits.nil?

League.instantiate(leagues_data)
request(Health::ENDPOINT, params: attributes)
@limits
end

def teams(attributes = {})
teams_data = request(Team::ENDPOINT, query_params: { api_key: attributes.delete(:api_key) }.compact)
return {} unless teams_data.success?
def leagues(attributes = {})
data = request(League::ENDPOINT, params: attributes)
League.instantiate(data)
end

Team.instantiate(teams_data)
def teams(attributes = {})
data = request(Team::ENDPOINT, params: attributes)
Team.instantiate(data)
end

def matches(match_id = nil, attributes = {})
match_data = request(Match::ENDPOINT, match_id, query_params: { api_key: attributes.delete(:api_key) }.compact)
return {} unless match_data.success?

Match.new(match_data)
data = request(Match::ENDPOINT, match_id, params: attributes)
Match.new(data)
end

def heroes(attributes = {})
heroes_data = request(Hero::ENDPOINT, query_params: { api_key: attributes.delete(:api_key) }.compact)
return {} unless heroes_data.success?

Hero.instantiate(heroes_data)
data = request(Hero::ENDPOINT, params: attributes)
Hero.instantiate(data)
end

def pro_players(attributes = {})
pro_players_data = request(ProPlayer::ENDPOINT, query_params: { api_key: attributes.delete(:api_key) }.compact)
return {} unless pro_players_data

ProPlayer.instantiate(pro_players_data)
data = request(ProPlayer::ENDPOINT, params: attributes)
ProPlayer.instantiate(data)
end

def explorer(league_id = nil, attributes = {})
explorer_data = request(Explorer::ENDPOINT, query_params: { api_key: attributes.delete(:api_key) }.merge(Explorer.query_params(league_id)).compact)
return {} unless explorer_data.success?

Explorer.new(explorer_data)
data = request(Explorer::ENDPOINT, params: Explorer.query_params(league_id).merge(attributes))
Explorer.new(data)
end

private
Expand All @@ -58,12 +54,23 @@ def connection
@connection ||= Connection.new
end

def request(method, argument = nil, query_params: {})
params = query_params.merge({ api_key: OpenDotaApi.api_key }.compact)
argument = argument ? "/#{argument}" : nil
pathname = "/#{INTERFACE}/#{method}#{argument}"
def request(method, argument = nil, params: {})
pathname = "/#{INTERFACE}/#{method}#{("/#{argument}" if argument)}"

global_api_key = { api_key: OpenDotaApi.api_key }.compact

data = connection.get(pathname, query: global_api_key.merge(params.compact))
return {} unless data.success?

limits_fixation(data)
data
end

connection.get(pathname, query: params)
def limits_fixation(data)
@limits = {
per_min: data.headers['x-rate-limit-remaining-minute'],
per_month: data.headers['x-rate-limit-remaining-month']
}
end
end
end
5 changes: 5 additions & 0 deletions lib/open_dota_api/health.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OpenDotaApi
class Health
ENDPOINT = 'health'.freeze
end
end
2 changes: 1 addition & 1 deletion lib/open_dota_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OpenDotaApi
VERSION = '0.4.0'.freeze
VERSION = '0.4.1'.freeze
end
85 changes: 70 additions & 15 deletions spec/lib/open_dota_api/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'spec_helper'

describe OpenDotaApi::Client do
let(:client) { described_class.new }
let(:endpoint) {}
let(:match_id) { 3_149_215_336 }
let(:league_id) { 5401 }
Expand All @@ -14,7 +13,9 @@
let(:data_file) {}
let(:headers) do
{
'content-type' => ['application/json; charset=utf-8']
'content-type' => ['application/json; charset=utf-8'],
'x-rate-limit-remaining-minute' => rand(60),
'x-rate-limit-remaining-month' => rand(50000)
}
end

Expand All @@ -33,23 +34,53 @@
stub_request(:get, api_url)
.with(headers: { 'Accept': '*/*', 'Accept-Encoding': 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent': 'Ruby' })
.to_return(status: 200, body: data_file, headers: headers)

stub_request(:get, "http://api.opendota.com/api/health").
with(headers: { 'Accept': '*/*', 'Accept-Encoding': 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent': 'Ruby' }).
to_return(status: 200, body: "", headers: headers)

stub_request(:get, "http://api.opendota.com/api/health?api_key=random-value").
with(headers: { 'Accept': '*/*', 'Accept-Encoding': 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent': 'Ruby' }).
to_return(status: 200, body: "", headers: {})
end

it 'returns interface' do
expect(OpenDotaApi::Client::INTERFACE).to eq 'api'
end

describe 'default attributes' do
describe '#limits' do
let(:endpoint) { OpenDotaApi::Health::ENDPOINT }

it 'updates limits' do
expect{ subject.limits }.to change{ subject.instance_variable_get(:@limits) }.from(nil).to(Hash)
end

context 'check limit after it was set' do
before do
subject.limits
end

it 'reads limits' do
expect{subject.limits}.to_not change(subject, :limits)
end
end
end

describe '#leagues' do
let(:endpoint) { OpenDotaApi::League::ENDPOINT }
let(:data_file) { leagues_file }

it 'returns array of objects' do
expect(client.leagues.all? { |league| league.is_a? OpenDotaApi::League }).to be_truthy
expect(subject.leagues.all? { |league| league.is_a? OpenDotaApi::League }).to be_truthy
end

it 'returns list' do
expect(client.leagues.to_deep_hash).to eq expected_leagues.to_deep_hash
expect(subject.leagues.to_deep_hash).to eq expected_leagues.to_deep_hash
end

it 'updates limits' do
expect{subject.leagues}.to change{ subject.instance_variable_get(:@limits) }.from(nil).to(Hash)
end
end

Expand All @@ -58,11 +89,15 @@
let(:data_file) { teams_file }

it 'returns array of objects' do
expect(client.teams.all? { |team| team.is_a? OpenDotaApi::Team }).to be_truthy
expect(subject.teams.all? { |team| team.is_a? OpenDotaApi::Team }).to be_truthy
end

it 'returns list' do
expect(client.teams.to_deep_hash).to eq expected_teams.to_deep_hash
expect(subject.teams.to_deep_hash).to eq expected_teams.to_deep_hash
end

it 'updates limits' do
expect{subject.teams}.to change{ subject.instance_variable_get(:@limits) }.from(nil).to(Hash)
end
end

Expand All @@ -71,11 +106,15 @@
let(:data_file) { match_file }

it 'returns object' do
expect(client.matches(match_id).is_a?(OpenDotaApi::Match)).to be_truthy
expect(subject.matches(match_id).is_a?(OpenDotaApi::Match)).to be_truthy
end

it 'returns match' do
expect(client.matches(match_id).to_deep_hash).to eq expected_match.to_deep_hash
expect(subject.matches(match_id).to_deep_hash).to eq expected_match.to_deep_hash
end

it 'updates limits' do
expect{subject.matches(match_id)}.to change{ subject.instance_variable_get(:@limits) }.from(nil).to(Hash)
end
end

Expand All @@ -84,11 +123,15 @@
let(:data_file) { heroes_file }

it 'returns array of objects' do
expect(client.heroes.all? { |hero| hero.is_a? OpenDotaApi::Hero }).to be_truthy
expect(subject.heroes.all? { |hero| hero.is_a? OpenDotaApi::Hero }).to be_truthy
end

it 'returns list' do
expect(client.heroes.to_deep_hash).to eq expected_heroes.to_deep_hash
expect(subject.heroes.to_deep_hash).to eq expected_heroes.to_deep_hash
end

it 'updates limits' do
expect{subject.heroes}.to change{ subject.instance_variable_get(:@limits) }.from(nil).to(Hash)
end
end

Expand All @@ -97,22 +140,30 @@
let(:data_file) { pro_players_file }

it 'returns array of objects' do
expect(client.pro_players.all? { |hero| hero.is_a? OpenDotaApi::ProPlayer }).to be_truthy
expect(subject.pro_players.all? { |hero| hero.is_a? OpenDotaApi::ProPlayer }).to be_truthy
end

it 'returns list' do
expect(client.pro_players.to_deep_hash).to eq expected_heroes.to_deep_hash
expect(subject.pro_players.to_deep_hash).to eq expected_heroes.to_deep_hash
end

it 'updates limits' do
expect{subject.pro_players}.to change{ subject.instance_variable_get(:@limits) }.from(nil).to(Hash)
end
end

describe '#explorer' do
let(:query) { OpenDotaApi::Explorer.query_params(league_id) }
let(:endpoint) { "#{OpenDotaApi::Explorer::ENDPOINT}" }
let(:endpoint) { OpenDotaApi::Explorer::ENDPOINT }
let(:params) { "?#{query.keys[0]}=#{query.values[0]}" }
let(:data_file) { explorer_file }

it 'returns array of match ids' do
expect(client.explorer(league_id).league_matches_ids.is_a?(Array)).to be_truthy
expect(subject.explorer(league_id).league_matches_ids.is_a?(Array)).to be_truthy
end

it 'updates limits' do
expect{subject.explorer(league_id)}.to change{ subject.instance_variable_get(:@limits) }.from(nil).to(Hash)
end

context 'API_KEY' do
Expand All @@ -122,7 +173,11 @@
end

it 'returns array of match ids' do
expect(client.explorer(league_id).league_matches_ids.is_a?(Array)).to be_truthy
expect(subject.explorer(league_id).league_matches_ids.is_a?(Array)).to be_truthy
end

it 'updates limits' do
expect{subject.explorer(league_id)}.to change{ subject.instance_variable_get(:@limits) }.from(nil).to(Hash)
end
end
end
Expand Down

0 comments on commit 40708af

Please sign in to comment.