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

Returns a new instance of RedirectInterface.



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

def initialize(request, old_host, old_path, &block)
  @status = 301
  @scheme = nil
  @request_query = request.query_string
  @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.



7
8
9
# File 'lib/rack/joint/redirect_interface.rb', line 7

def old_host
  @old_host
end

#old_pathObject (readonly)

Returns the value of attribute old_path.



7
8
9
# File 'lib/rack/joint/redirect_interface.rb', line 7

def old_path
  @old_path
end

#request_queryObject (readonly)

Returns the value of attribute request_query.



7
8
9
# File 'lib/rack/joint/redirect_interface.rb', line 7

def request_query
  @request_query
end

#request_schemeObject (readonly)

Returns the value of attribute request_scheme.



7
8
9
# File 'lib/rack/joint/redirect_interface.rb', line 7

def request_scheme
  @request_scheme
end

#schemeObject (readonly)

Returns the value of attribute scheme.



7
8
9
# File 'lib/rack/joint/redirect_interface.rb', line 7

def scheme
  @scheme
end

Instance Method Details

#apply!Array

Return response given parameters in config.ru.

Returns:

  • (Array)

    Return response given parameters in config.ru.



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

def apply!
  @scheme ||= request_scheme
  @new_host ||= old_host
  @new_path ||= old_path
  old_url = UrlBuilder.new(request_scheme, request_query, old_host, old_path).build
  new_location = UrlBuilder.new(scheme, request_query, @new_host, @new_path).build
  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