Class: Rack::IpRestrictor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_ip_restrictor/config.rb

Overview

Configuration class for the IpRestrictor

Examples:

Rack::IpRestrictor.configure do
  respond_with [403, {'Content-Type' => 'text/html'}, '']

  ips_for :test do
    add '127.0.0.1'
    add '127.0.0.2/8'
  end

  restrict /^\/admin/, '/admin', :only => :test
end

See Also:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Config) initialize

Returns a new instance of Config



18
19
20
21
22
23
24
25
26
# File 'lib/rack_ip_restrictor/config.rb', line 18

def initialize
  @ip_groups = {}
  @restrictions = []
  @response = [
    403,
    {'Content-Type' => 'text/html'},
    ''
  ]
end

Instance Attribute Details

- (Object) ip_groups

Returns the value of attribute ip_groups



16
17
18
# File 'lib/rack_ip_restrictor/config.rb', line 16

def ip_groups
  @ip_groups
end

- (Object) response

Returns the value of attribute response



16
17
18
# File 'lib/rack_ip_restrictor/config.rb', line 16

def response
  @response
end

- (Object) restrictions

Returns the value of attribute restrictions



16
17
18
# File 'lib/rack_ip_restrictor/config.rb', line 16

def restrictions
  @restrictions
end

Instance Method Details

- (IpGroup) ips_for(name, &block)

Sets and gets IP addresses for a named group

Returns:

  • (IpGroup)

    IP addresses for a named group



38
39
40
41
42
43
44
45
46
# File 'lib/rack_ip_restrictor/config.rb', line 38

def ips_for(name, &block)
  if block_given?
    @ip_groups[name] = IpGroup.new
    @ip_groups[name].instance_eval &block
    @ip_groups[name]
  else
    @ip_groups[name]
  end
end

- (Object) respond_with(response)

Overwrites the default response. Same format as a middleware response.

Parameters:

  • response (Array<Integer, String, String>)

    status, a set of headers, body



31
32
33
# File 'lib/rack_ip_restrictor/config.rb', line 31

def respond_with(response)
  @response = response
end

- (Object) restrict(*args)

Adds a restriction



50
51
52
# File 'lib/rack_ip_restrictor/config.rb', line 50

def restrict(*args)
  @restrictions << Restriction.new(*args)
end