Class: X::RateLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/x/rate_limit.rb

Constant Summary collapse

RATE_LIMIT_TYPE =
"rate-limit".freeze
APP_LIMIT_TYPE =
"app-limit-24hour".freeze
USER_LIMIT_TYPE =
"user-limit-24hour".freeze
TYPES =
[RATE_LIMIT_TYPE, APP_LIMIT_TYPE, USER_LIMIT_TYPE].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, response:) ⇒ RateLimit

Returns a new instance of RateLimit.



10
11
12
13
# File 'lib/x/rate_limit.rb', line 10

def initialize(type:, response:)
  @type = type
  @response = response
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/x/rate_limit.rb', line 8

def response
  @response
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/x/rate_limit.rb', line 8

def type
  @type
end

Instance Method Details

#limitObject



15
16
17
# File 'lib/x/rate_limit.rb', line 15

def limit
  Integer(response.fetch("x-#{type}-limit"))
end

#remainingObject



19
20
21
# File 'lib/x/rate_limit.rb', line 19

def remaining
  Integer(response.fetch("x-#{type}-remaining"))
end

#reset_atObject



23
24
25
# File 'lib/x/rate_limit.rb', line 23

def reset_at
  Time.at(Integer(response.fetch("x-#{type}-reset")))
end

#reset_inObject Also known as: retry_after



27
28
29
# File 'lib/x/rate_limit.rb', line 27

def reset_in
  [(reset_at - Time.now).ceil, 0].max
end