Module: Raterr::Mixin

Included in:
Day, Hour, Minute
Defined in:
lib/raterr/mixin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/raterr/mixin.rb', line 4

def options
  @options
end

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'lib/raterr/mixin.rb', line 4

def request
  @request
end

Instance Method Details

#allowed?Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/raterr/mixin.rb', line 18

def allowed?
  reset_cache if Time.now > rate_period
  fetch_cache[:attempts] <= max_per_period
end

#initialize(request, options) ⇒ Object



6
7
8
9
# File 'lib/raterr/mixin.rb', line 6

def initialize(request, options)
  @request = request
  @options = options
end

#proceedObject



23
24
25
26
27
28
29
30
31
# File 'lib/raterr/mixin.rb', line 23

def proceed
  attempts = fetch_cache[:attempts] + 1
  set_cache(attempts)

  {
    status: 200,
    attempts: attempts
  }
end

#rate_limit_exceededObject



11
12
13
14
15
16
# File 'lib/raterr/mixin.rb', line 11

def rate_limit_exceeded
  {
    status: options[:code] || Raterr::DEFAULTS[:code],
    text: Raterr::DEFAULTS[:message] % { time: try_after }
  }
end