Class: EpoOps::RateLimit

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

Instance Method Summary collapse

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

#attrObject (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_quotaObject



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

Returns:

  • (Boolean)


14
15
16
# File 'lib/epo_ops/rate_limit.rb', line 14

def limit_reached?
  @attr.key?('x-rejection-reason')
end

#rejection_reasonObject



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_atObject



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_quotaObject



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