Skip to content

Commit

Permalink
Merge pull request #24 from alexanderdavidpan/rate_limit
Browse files Browse the repository at this point in the history
(feature) Create Cryptocompare::Stats module
  • Loading branch information
alexanderdavidpan authored Nov 25, 2021
2 parents 354b6a1 + 51fa8c6 commit e64479a
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,41 @@ Cryptocompare::Exchanges.all

If no exchange option is specified, then the default 'CCCAGG' is used. This is cryptocompare's aggregated data.

### Stats

Get stats such as rate limit.

**Examples:**

Find out how many calls you have left in the current month, day, hour, minute and second.

```ruby
Cryptocompare::Stats.rate_limit
# => {
# "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
# }
# }
# }
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
1 change: 1 addition & 0 deletions lib/cryptocompare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative "cryptocompare/price_historical"
require_relative "cryptocompare/helpers/query_param_helper"
require_relative "cryptocompare/top_pairs"
require_relative "cryptocompare/stats"

module Cryptocompare
end
51 changes: 51 additions & 0 deletions lib/cryptocompare/stats.rb
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
53 changes: 53 additions & 0 deletions test/fixtures/rate_limit.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions test/test_stats.rb
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

0 comments on commit e64479a

Please sign in to comment.