Sha256: 9262a928fc3a1e2bf9b5b7c2cc4b1278a848e1389cba75c2f0e918b5d4483d58

Contents?: true

Size: 1.42 KB

Versions: 28

Compression:

Stored size: 1.42 KB

Contents

module Hanami
  module Action
    # HTTP redirect API
    #
    # @since 0.1.0
    module Redirect
      # The HTTP header for redirects
      #
      # @since 0.2.0
      # @api private
      LOCATION = 'Location'.freeze

      private

      # Redirect to the given URL and halt the request
      #
      # @param url [String] the destination URL
      # @param status [Fixnum] the http code
      #
      # @since 0.1.0
      #
      # @see Hanami::Action::Throwable#halt
      #
      # @example With default status code (302)
      #   require 'hanami/controller'
      #
      #   class Create
      #     include Hanami::Action
      #
      #     def call(params)
      #       # ...
      #       redirect_to 'http://example.com/articles/23'
      #     end
      #   end
      #
      #   action = Create.new
      #   action.call({}) # => [302, {'Location' => '/articles/23'}, '']
      #
      # @example With custom status code
      #   require 'hanami/controller'
      #
      #   class Create
      #     include Hanami::Action
      #
      #     def call(params)
      #       # ...
      #       redirect_to 'http://example.com/articles/23', status: 301
      #     end
      #   end
      #
      #   action = Create.new
      #   action.call({}) # => [301, {'Location' => '/articles/23'}, '']
      def redirect_to(url, status: 302)
        headers[LOCATION] = ::String.new(url)
        halt(status)
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
hanami-controller-1.0.0.beta2 lib/hanami/action/redirect.rb
hanami-controller-1.0.0.beta1 lib/hanami/action/redirect.rb
hanami-controller-0.8.1 lib/hanami/action/redirect.rb
hanami-controller-0.8.0 lib/hanami/action/redirect.rb
hanami-controller-0.7.1 lib/hanami/action/redirect.rb
hanami-controller-0.7.0 lib/hanami/action/redirect.rb
hanami-controller-0.6.1 lib/hanami/action/redirect.rb
hanami-controller-0.6.0 lib/hanami/action/redirect.rb