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:

  • README.rdoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig



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

#ip_groupsObject

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

#responseObject

Returns the value of attribute response



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

def response
  @response
end

#restrictionsObject

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

#ips_for(name, &block) ⇒ IpGroup

Sets and gets 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

#respond_with(response) ⇒ Object

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



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

def respond_with(response)
  @response = response
end

#restrict(*args) ⇒ Object

Adds a restriction



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

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