123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::Routing::Redirect

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Endpoint
Instance Chain:
self, Endpoint
Inherits: ActionDispatch::Routing::Endpoint
Defined in: actionpack/lib/action_dispatch/routing/redirection.rb

Class Method Summary

Instance Attribute Summary

Endpoint - Inherited

Instance Method Summary

Constructor Details

.new(status, block) ⇒ Redirect

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 15

def initialize(status, block)
  @status = status
  @block  = block
end

Instance Attribute Details

#block (readonly) Also known as: #options

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 13

attr_reader :status, :block

#redirect?Boolean (readonly)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 20

def redirect?; true; end

#status (readonly)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 13

attr_reader :status, :block

Instance Method Details

#build_response(req)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 35

def build_response(req)
  uri = URI.parse(path(req.path_parameters, req))

  unless uri.host
    if relative_path?(uri.path)
      uri.path = "#{req.script_name}/#{uri.path}"
    elsif uri.path.empty?
      uri.path = req.script_name.empty? ? "/" : req.script_name
    end
  end

  uri.scheme ||= req.scheme
  uri.host   ||= req.host
  uri.port   ||= req.port unless req.standard_port?

  req.commit_flash

  body = ""

  headers = {
    "Location" => uri.to_s,
    "Content-Type" => "text/html; charset=#{ActionDispatch::Response.default_charset}",
    "Content-Length" => body.length.to_s
  }

  ActionDispatch::Response.new(status, headers, body)
end

#call(env)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 22

def call(env)
  ActiveSupport::Notifications.instrument("redirect.action_dispatch") do |payload|
    request = Request.new(env)
    response = build_response(request)

    payload[:status] = @status
    payload[:location] = response.headers["Location"]
    payload[:request] = request

    response.to_a
  end
end

#escape(params) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 76

def escape(params)
  params.transform_values { |v| Rack::Utils.escape(v) }
end

#escape_fragment(params) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 80

def escape_fragment(params)
  params.transform_values { |v| Journey::Router::Utils.escape_fragment(v) }
end

#escape_path(params) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 84

def escape_path(params)
  params.transform_values { |v| Journey::Router::Utils.escape_path(v) }
end

#inspect

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 67

def inspect
  "redirect(#{status})"
end

#path(params, request)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 63

def path(params, request)
  block.call params, request
end

#relative_path?(path) ⇒ Boolean (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/redirection.rb', line 72

def relative_path?(path)
  path && !path.empty? && !path.start_with?("/")
end