Module: Jets::Controller::Redirection

Included in:
Rendering
Defined in:
lib/jets/controller/redirection.rb

Instance Method Summary collapse

Instance Method Details

#ensure_protocol(url) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jets/controller/redirection.rb', line 39

def ensure_protocol(url)
  return url if url.starts_with?('http')

  # annoying but the request payload is different with localhost/rack vs
  # api gateway
  # check out:
  #   spec/fixtures/dumps/api_gateway/posts/create.json
  #   spec/fixtures/dumps/rack/posts/create.json
  protocol = if headers["x-forwarded-proto"] # API Gateway
      headers["x-forwarded-proto"]
    elsif headers["origin"] # Rack / localhost
      URI.parse(headers["origin"]).scheme
    else
      "http" # fallback. Capybara / Selenium tests
    end

  raise "Invalid protocol #{protocol}" unless protocol.starts_with?('http')

  "#{protocol}://#{url}"
end

#redirect_back(fallback_location: '/') ⇒ Object



34
35
36
37
# File 'lib/jets/controller/redirection.rb', line 34

def redirect_back(fallback_location: '/')
  location = request.headers["referer"] || fallback_location
  redirect_to(location)
end

#redirect_to(url, options = {}) ⇒ Object

redirect_to “/posts”, :status => 301 redirect_to :action=>‘atom’, :status => 302



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jets/controller/redirection.rb', line 5

def redirect_to(url, options={})
  unless url.is_a?(String)
    raise "redirect_to url parameter must be a String. Please pass in a string"
  end

  uri = URI.parse(url)
  # if no location.host, we been provided a relative host
  if !uri.host && actual_host
    url = "/#{url}" unless url.starts_with?('/')
    url = add_stage_name(url)
    redirect_url = actual_host + url
  else
    redirect_url = url
  end

  redirect_url = ensure_protocol(redirect_url)

  aws_proxy = Rendering::RackRenderer.new(self,
    status: options[:status] || 302,
    headers: { "Location" => redirect_url },
    body: "",
    isBase64Encoded: false,
  )
  resp = aws_proxy.render
  # redirect is a type of rendering
  @rendered = true
  @rendered_data = resp
end