Class: EpoOps::RateLimit
- Inherits:
-
Object
- Object
- EpoOps::RateLimit
- Defined in:
- lib/epo_ops/rate_limit.rb
Constant Summary collapse
- WEEKLY_QUOTA_RESET_TIME =
604_800- HOURLY_QUOTA_RESET_TIME =
600- BASE_RESET_TIME =
60
Instance Attribute Summary collapse
-
#attr ⇒ Object
readonly
Returns the value of attribute attr.
Instance Method Summary collapse
- #hourly_quota ⇒ Object
-
#initialize(http_header) ⇒ RateLimit
constructor
A new instance of RateLimit.
- #limit_reached? ⇒ Boolean
- #rejection_reason ⇒ Object
- #reset_at ⇒ Object
- #weekly_quota ⇒ Object
Constructor Details
#initialize(http_header) ⇒ RateLimit
Returns a new instance of RateLimit.
9 10 11 12 |
# File 'lib/epo_ops/rate_limit.rb', line 9 def initialize(http_header) fail "Rate Limit data should be a Hash but is #{http_header.inspect} (#{http_header.class.name})" unless http_header.is_a?(Hash) @attr = http_header end |
Instance Attribute Details
#attr ⇒ Object (readonly)
Returns the value of attribute attr.
7 8 9 |
# File 'lib/epo_ops/rate_limit.rb', line 7 def attr @attr end |
Instance Method Details
#hourly_quota ⇒ Object
27 28 29 30 |
# File 'lib/epo_ops/rate_limit.rb', line 27 def hourly_quota quota = @attr['x-individualquotaperhour-used'] quota.to_i if quota end |
#limit_reached? ⇒ Boolean
14 15 16 |
# File 'lib/epo_ops/rate_limit.rb', line 14 def limit_reached? @attr.key?('x-rejection-reason') end |
#rejection_reason ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/epo_ops/rate_limit.rb', line 18 def rejection_reason return nil unless @attr['x-rejection-reason'] case @attr['x-rejection-reason'] when 'RegisteredQuotaPerWeek' then :weekly_quota when 'IndividualQuotaPerHour' then :hourly_quota else :unknown_reason end end |
#reset_at ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/epo_ops/rate_limit.rb', line 37 def reset_at return unless limit_reached? case rejection_reason when :weekly_quota then Time.now.to_i + WEEKLY_QUOTA_RESET_TIME when :hourly_quota then Time.now.to_i + HOURLY_QUOTA_RESET_TIME else Time.now.to_i + BASE_RESET_TIME end end |
#weekly_quota ⇒ Object
32 33 34 35 |
# File 'lib/epo_ops/rate_limit.rb', line 32 def weekly_quota quota = @attr['x-registeredquotaperweek-used'] quota.to_i if quota end |