Class: Rack::Joint::RedirectInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/joint/redirect_interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, old_host, old_path, &block) ⇒ RedirectInterface



10
11
12
13
14
15
16
17
# File 'lib/rack/joint/redirect_interface.rb', line 10

def initialize(request, old_host, old_path, &block)
  @status = 301
  @scheme = nil
  @request_scheme = request.scheme
  @old_host = old_host
  @old_path = old_path || request.path_info
  instance_exec(&block)
end

Instance Attribute Details

#old_hostObject (readonly)

Returns the value of attribute old_host.



9
10
11
# File 'lib/rack/joint/redirect_interface.rb', line 9

def old_host
  @old_host
end

#old_pathObject (readonly)

Returns the value of attribute old_path.



9
10
11
# File 'lib/rack/joint/redirect_interface.rb', line 9

def old_path
  @old_path
end

#request_schemeObject (readonly)

Returns the value of attribute request_scheme.



9
10
11
# File 'lib/rack/joint/redirect_interface.rb', line 9

def request_scheme
  @request_scheme
end

#schemeObject (readonly)

Returns the value of attribute scheme.



9
10
11
# File 'lib/rack/joint/redirect_interface.rb', line 9

def scheme
  @scheme
end

Instance Method Details

#apply!Array



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/joint/redirect_interface.rb', line 20

def apply!
  @scheme ||= request_scheme
  @new_host ||= old_host
  @new_path ||= old_path
  old_url = build_uri(request_scheme, old_host, old_path)
  new_location = build_uri(scheme, @new_host, @new_path)
  if old_url == new_location
    raise BadRedirectError.new('Redirect URL has been declared the same as current URL.')
  end
  [@status, { 'Location' => new_location, 'Content-Type' => 'text/html', 'Content-Language' => '0' }, ["Redirect from: #{old_url}"]]
end