Sha256: f184ce850f0fed3a5c1f533a16a25fe92748c03618842e5f745ff94321f765fa

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

module Nearmiss

  # Class for API Rate Limit info
  #
  # @!attribute [w] limit
  #   @return [Fixnum] Max tries per rate limit period
  # @!attribute [w] remaining
  #   @return [Fixnum] Remaining tries per rate limit period
  # @!attribute [w] resets_at
  #   @return [Time] Indicates when rate limit resets
  # @!attribute [w] resets_in
  #   @return [Fixnum] Number of seconds when rate limit resets
  #
  # @see https://developer.github.com/v3/#rate-limiting
  class RateLimit < Struct.new(:limit, :remaining, :resets_at, :resets_in)

    # Get rate limit info from HTTP response
    #
    # @param response [#headers] HTTP response
    # @return [RateLimit]
    def self.from_response(response)
      info = new
      if response && !response.headers.nil?
        info.limit = (response.headers['X-RateLimit-Limit'] || 1).to_i
        info.remaining = (response.headers['X-RateLimit-Remaining'] || 1).to_i
        info.resets_at = Time.at((response.headers['X-RateLimit-Reset'] || Time.now).to_i)
        info.resets_in = [(info.resets_at - Time.now).to_i, 0].max
      end

      info
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nearmiss-ruby-1.0.4 lib/nearmiss-ruby/rate_limit.rb
nearmiss-ruby-1.0.3 lib/nearmiss-ruby/rate_limit.rb
nearmiss-ruby-1.0.2 lib/nearmiss-ruby/rate_limit.rb
nearmiss-ruby-1.0.1 lib/nearmiss-ruby/rate_limit.rb
nearmiss-ruby-1.0.0 lib/nearmiss-ruby/rate_limit.rb