Sha256: 5591030ef0fb85bf8ce423aced4a4a0762774a8bb707b11a5381de2236b59dbc

Contents?: true

Size: 1.87 KB

Versions: 25

Compression:

Stored size: 1.87 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

      redirect_url = add_stage(url)
      redirect_url = ensure_protocol(redirect_url)

      default = {
        headers: { "Location" => redirect_url },
        isBase64Encoded: false,
      }
      if request.xhr?
        options[:content_type] ||= "application/json"
        options[:status] ||= 200
        options[:body] ||= JSON.dump(success: true, location: redirect_url)
      else
        options[:status] ||= 302
        options[:body] ||= ""
      end
      Jets.logger.info("redirect_to options #{options}")
      options = default.merge(options)

      aws_proxy = Rendering::RackRenderer.new(self, options)
      resp = aws_proxy.render
      # redirect is a type of rendering
      @rendered = true
      @rendered_data = resp
    end

    def redirect_back(fallback_location: '/')
      location = request.headers["referer"] || fallback_location
      redirect_to(location)
    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 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
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
jets-4.0.12 lib/jets/controller/redirection.rb
jets-4.0.11 lib/jets/controller/redirection.rb
jets-4.0.10 lib/jets/controller/redirection.rb
jets-4.0.9 lib/jets/controller/redirection.rb
jets-4.0.8 lib/jets/controller/redirection.rb
jets-4.0.7 lib/jets/controller/redirection.rb
jets-4.0.6 lib/jets/controller/redirection.rb
jets-4.0.5 lib/jets/controller/redirection.rb
jets-4.0.4 lib/jets/controller/redirection.rb
jets-4.0.3 lib/jets/controller/redirection.rb
jets-4.0.2 lib/jets/controller/redirection.rb
jets-4.0.1 lib/jets/controller/redirection.rb
jets-4.0.0 lib/jets/controller/redirection.rb
jets-3.2.2 lib/jets/controller/redirection.rb
jets-3.2.1 lib/jets/controller/redirection.rb
jets-3.2.0 lib/jets/controller/redirection.rb
jets-3.1.5 lib/jets/controller/redirection.rb
jets-3.1.4 lib/jets/controller/redirection.rb
jets-3.1.3 lib/jets/controller/redirection.rb
jets-3.1.2 lib/jets/controller/redirection.rb