Sha256: 4168657ccefc4bc312bb7ae6672bfc9a7bd4045e655b1553129c7079d8c46886

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

class Response
  # Abstract redirect response
  class Redirect < self
    include AbstractType

    # Build redirect response
    #
    # @param [String] location
    #   the location to redirect to
    #
    # @return [Response::Redirect]
    #
    # @example
    #
    #   # 302 response
    #   response = Response::Redirect::Found.build('http://example.com')
    #   response.status # => 302
    #   response.headers # => { "Location" => "http://example.com", "Content-Type" => "text/plain" }
    #   response.body # => "You are beeing redirected to: http://example.com"
    #
    #   # 301 response
    #   response = Response::Redirect::Permanent.build('http://example.com')
    #   response.status # => 301
    #   response.headers # => { "Location" => "http://example.com", "Content-Type" => "text/plain" }
    #   response.body # => "You are beeing redirected to: http://example.com"
    #
    #   # Overriding defaults
    #
    #   response = Response::Redirect::Found.build('http://example.com') do |response|
    #     response.with_body("Redirection")
    #   end
    #
    #   response[2] # => "Redirection"
    #
    # @api public
    #
    def self.build(location)
      super(
        self::STATUS,
        {
          'Location'      => location,
          'Content-Type' => TEXT_PLAIN
        },
        "You are beeing redirected to: #{location}"
      )
    end

    # Response with default status code of 302
    class Found < self
      STATUS = Status::FOUND
    end

    # Response with default status code of 301
    class Permanent < self
      STATUS = Status::MOVED_PERMANENTLY
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
response-0.0.5 lib/response/redirect.rb