Class: Fbe::Middleware::RateLimit

Inherits:
Faraday::Middleware
  • Object
show all
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

Examples:

Usage in Faraday middleware stack

connection = Faraday.new do |f|
  f.use Fbe::Middleware::RateLimit
end

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RateLimit

Initializes the rate limit middleware.

Parameters:

  • app (Object)

    The next middleware in the stack



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.

Parameters:

  • env (Faraday::Env)

    The request environment

Returns:

  • (Faraday::Response)

    The response from cache or the next middleware



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