Sha256: f9a6375f3e36e583b49b312da9d03de8779f996296c321266a0264ae4117e6aa

Contents?: true

Size: 1.67 KB

Versions: 64

Compression:

Stored size: 1.67 KB

Contents

class Jets::Controller
  module Redirection
    # redirect_to "/posts", :status => 301
    # redirect_to :action=>'atom', :status => 302
    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 = Renderers::AwsProxyRenderer.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

    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 actual_host.include?("amazonaws.com") # 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
  end
end

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
jets-1.0.18 lib/jets/controller/redirection.rb
jets-1.0.17 lib/jets/controller/redirection.rb
jets-1.0.16 lib/jets/controller/redirection.rb
jets-1.0.15 lib/jets/controller/redirection.rb
jets-1.0.13 lib/jets/controller/redirection.rb
jets-1.0.12 lib/jets/controller/redirection.rb
jets-1.0.11 lib/jets/controller/redirection.rb
jets-1.0.10 lib/jets/controller/redirection.rb
jets-1.0.9 lib/jets/controller/redirection.rb
jets-1.0.8 lib/jets/controller/redirection.rb
jets-1.0.7 lib/jets/controller/redirection.rb
jets-1.0.6 lib/jets/controller/redirection.rb
jets-1.0.5 lib/jets/controller/redirection.rb
jets-1.0.4 lib/jets/controller/redirection.rb
jets-1.0.3 lib/jets/controller/redirection.rb
jets-1.0.2 lib/jets/controller/redirection.rb
jets-1.0.1 lib/jets/controller/redirection.rb
jets-1.0.0 lib/jets/controller/redirection.rb
jets-0.10.4 lib/jets/controller/redirection.rb
jets-0.10.3 lib/jets/controller/redirection.rb