Class: Fbe::Middleware::RateLimit
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Fbe::Middleware::RateLimit
- Defined in:
- lib/fbe/middleware/rate_limit.rb
Overview
Faraday middleware that caches GitHub API rate limit information.
This middleware intercepts calls to the /rate_limit endpoint and caches the results locally. It tracks the remaining requests count and decrements it for each API call. Every 100 requests, it refreshes the cached data by allowing the request to pass through to the GitHub API.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Zerocracy
- License
-
MIT
Instance Method Summary collapse
-
#call(env) ⇒ Faraday::Response
Processes the HTTP request and handles rate limit caching.
-
#initialize(app) ⇒ RateLimit
constructor
Initializes the rate limit middleware.
Constructor Details
#initialize(app) ⇒ RateLimit
Initializes the rate limit middleware.
30 31 32 33 34 35 |
# File 'lib/fbe/middleware/rate_limit.rb', line 30 def initialize(app) super @cached_response = nil @remaining_count = nil @request_counter = 0 end |
Instance Method Details
#call(env) ⇒ Faraday::Response
Processes the HTTP request and handles rate limit caching.
41 42 43 44 45 46 47 48 |
# File 'lib/fbe/middleware/rate_limit.rb', line 41 def call(env) if env.url.path == '/rate_limit' handle_rate_limit_request(env) else track_request @app.call(env) end end |