-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from alexanderdavidpan/rate_limit
(feature) Create Cryptocompare::Stats module
- Loading branch information
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'faraday' | ||
require 'json' | ||
|
||
module Cryptocompare | ||
module Stats | ||
RATE_LIMIT_API_URL = 'https://min-api.cryptocompare.com/stats/rate/limit'.freeze | ||
private_constant :RATE_LIMIT_API_URL | ||
|
||
# Find out how many calls you have left in the current month, day, hour, minute and second | ||
# | ||
# ==== Returns | ||
# | ||
# [Hash] Returns a hash containing data about calls_made and calls_left | ||
# | ||
# ==== Examples | ||
# | ||
# Find out how calls you have made and have left in the current month, day, hour, minute and second | ||
# | ||
# Cryptocompare::Stats.rate_limit | ||
# | ||
# Sample response | ||
# | ||
# { | ||
# "Response" => "Success", | ||
# "Message" => "", | ||
# "HasWarning" => false, | ||
# "Type" => 100, | ||
# "RateLimit" => {}, | ||
# "Data" => { | ||
# "calls_made" => { | ||
# "second" => 1, | ||
# "minute" => 2, | ||
# "hour" => 2, | ||
# "day" => 2, | ||
# "month" => 33 | ||
# }, | ||
# "calls_left" => { | ||
# "second" => 19, | ||
# "minute" => 298, | ||
# "hour" => 2998, | ||
# "day" => 7498, | ||
# "month" => 49967 | ||
# } | ||
# } | ||
# } | ||
def self.rate_limit | ||
api_resp = Faraday.get(RATE_LIMIT_API_URL) | ||
JSON.parse(api_resp.body) | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'test_helper' | ||
|
||
class TestStats < Minitest::Test | ||
RATE_LIMIT_KEYS = %w[ | ||
Response | ||
Message | ||
HasWarning | ||
Type | ||
RateLimit | ||
Data | ||
].freeze | ||
|
||
RATE_LIMIT_DATA_KEYS = %w[ | ||
calls_made | ||
calls_left | ||
].freeze | ||
|
||
RATE_LIMIT_METADATA_KEYS = %w[ | ||
second | ||
minute | ||
hour | ||
day | ||
month | ||
].freeze | ||
|
||
def test_rate_limit | ||
VCR.use_cassette('rate_limit') do | ||
rate_limit_resp = Cryptocompare::Stats.rate_limit | ||
assert_equal 'Success', rate_limit_resp['Response'] | ||
assert rate_limit_resp.kind_of?(Hash) | ||
|
||
rate_limit_resp.each do |rate_limit_key, _| | ||
assert RATE_LIMIT_KEYS.include?(rate_limit_key) | ||
end | ||
|
||
rate_limit_resp_data = rate_limit_resp.fetch('Data') | ||
rate_limit_resp_data.each do |rate_limit_data_key, _| | ||
assert RATE_LIMIT_DATA_KEYS.include?(rate_limit_data_key) | ||
end | ||
|
||
calls_made_data = rate_limit_resp_data.fetch('calls_made') | ||
calls_made_data.each do |calls_made_key, calls_made_value| | ||
assert RATE_LIMIT_METADATA_KEYS.include?(calls_made_key) | ||
assert calls_made_value.kind_of?(Integer) | ||
end | ||
|
||
calls_left_data = rate_limit_resp_data.fetch('calls_left') | ||
calls_left_data.each do |calls_left_key, calls_left_value| | ||
assert RATE_LIMIT_METADATA_KEYS.include?(calls_left_key) | ||
assert calls_left_value.kind_of?(Integer) | ||
end | ||
end | ||
end | ||
end |