Class: SlowWeb

Inherits:
Object
  • Object
show all
Defined in:
lib/slow_web.rb,
lib/slow_web/limit.rb,
lib/slow_web/version.rb

Defined Under Namespace

Classes: Limit

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.get_limit(host) ⇒ SlowWeb::Limit

Retrieves the limit object for a given host.

Parameters:

  • host (String)

    the host associated with the limit.

Returns:



39
40
41
# File 'lib/slow_web.rb', line 39

def self.get_limit(host)
  return @limits[host]
end

.limit(host, count, period) ⇒ SlowWeb::Limit

Limits the number of requests that can occur within a specified number of seconds.

Parameters:

  • host (String)

    the host to restrict.

  • count (Fixnum)

    the number of requests that can occur within a time period.

  • period (Fixnum)

    the number of seconds in the time period.

Returns:



26
27
28
29
30
31
32
# File 'lib/slow_web.rb', line 26

def self.limit(host, count, period)
  raise "Limit already exists for this host: #{host}" if @limits[host]
  
  limit = Limit.new(host, count, period)
  @limits[host] = limit
  return limit
end

.limit_exceeded?(host) ⇒ Boolean

A flag stating if the limit for a given host has been exceeded.

Parameters:

  • host (String)

    the host that is being limited.

Returns:

  • (Boolean)

    a flag stating if the limit has been exceeded.



48
49
50
51
# File 'lib/slow_web.rb', line 48

def self.limit_exceeded?(host)
  limit = @limits[host]
  return !limit.nil? && limit.exceeded?
end

.resetObject

Removes all limits.



55
56
57
# File 'lib/slow_web.rb', line 55

def self.reset
  @limits = {}
end