Class: Epo::Ops::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.



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

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.



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

def attr
  @attr
end

Instance Method Details

#hourly_quotaObject



28
29
30
31
# File 'lib/epo/ops/rate_limit.rb', line 28

def hourly_quota
  quota = @attr['x-individualquotaperhour-used']
  quota.to_i if quota
end

#limit_reached?Boolean

Returns:

  • (Boolean)


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

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

#rejection_reasonObject



19
20
21
22
23
24
25
26
# File 'lib/epo/ops/rate_limit.rb', line 19

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



38
39
40
41
42
43
44
45
46
# File 'lib/epo/ops/rate_limit.rb', line 38

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



33
34
35
36
# File 'lib/epo/ops/rate_limit.rb', line 33

def weekly_quota
  quota = @attr['x-registeredquotaperweek-used']
  quota.to_i if quota
end