Class: Swaggable::RackRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/swaggable/rack_redirect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first_arg, second_arg = nil) ⇒ RackRedirect

Returns a new instance of RackRedirect.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/swaggable/rack_redirect.rb', line 9

def initialize(first_arg, second_arg = nil)
  if second_arg
    next_app = first_arg
    options = second_arg.dup
  else
    next_app = nil
    options = first_arg.dup
  end

  @next_app = next_app
  @from = options.delete(:from)
  @to = options.delete(:to) || raise(ArgumentError.new(":to option is mandatory"))

  if options.any?
    raise ArgumentError.new "Unsupported options #{options.keys.inspect}"
  end
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



3
4
5
# File 'lib/swaggable/rack_redirect.rb', line 3

def from
  @from
end

#toObject

Returns the value of attribute to.



3
4
5
# File 'lib/swaggable/rack_redirect.rb', line 3

def to
  @to
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/swaggable/rack_redirect.rb', line 27

def call env
  if from.nil?
   redirect
  elsif from.is_a?(String) && env['PATH_INFO'] == from
    redirect
  elsif from.is_a?(Regexp) && env['PATH_INFO'] =~ from
    redirect
  elsif next_app.nil?
    raise('No application defined to forward the request to')
  else
    next_app.call env
  end
end