Class: ActionDispatch::Routing::Redirect

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/coverband/utils/rails6_ext.rb

Instance Method Summary collapse

Instance Method Details

#build_response(req) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/coverband/utils/rails6_ext.rb', line 29

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",
    "content-length" => body.length.to_s
  }

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

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coverband/utils/rails6_ext.rb', line 16

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