Class: Hanami::Http::Status Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/http/status.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

An HTTP status

Since:

  • 0.1.0

Constant Summary collapse

ALL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

A set of standard codes and messages for HTTP statuses

Since:

  • 0.1.0

::Rack::Utils::HTTP_STATUS_CODES
SYMBOLS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Symbolic names for status codes

Since:

  • 2.0.2

::Rack::Utils::SYMBOL_TO_STATUS_CODE

Class Method Summary collapse

Class Method Details

.for_code(code) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a status for the given code

Examples:

Integer HTTP Status

require "hanami/http/status"

Hanami::Http::Status.for_code(401)
  # => [401, "Unauthorized"]

Symbol HTTP Status

require "hanami/http/status"

Hanami::Http::Status.for_code(:unauthorized)
  # => [401, "Unauthorized"]

Unknown HTTP Status

require "hanami/http/status"

Hanami::Http::Status.for_code(999)
  # => raise Hanami::Action::UnknownHttpStatusError

Hanami::Http::Status.for_code(:foo)
  # => raise Hanami::Action::UnknownHttpStatusError

Parameters:

  • code (Integer, Symbol)

    a valid HTTP code

Returns:

  • (Array)

    a pair of code and message for an HTTP status

Raises:

See Also:

Since:

  • 0.1.0



60
61
62
63
64
65
66
67
# File 'lib/hanami/http/status.rb', line 60

def self.for_code(code)
  case code
  when Integer
    ALL.assoc(code)
  when Symbol
    ALL.assoc(SYMBOLS[code])
  end or raise ::Hanami::Action::UnknownHttpStatusError.new(code)
end

.lookup(code) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a status code for the given code

Examples:

Integer HTTP Status

require "hanami/http/status"

Hanami::Http::Status.lookup(401)
  # => 401

Symbol HTTP Status

require "hanami/http/status"

Hanami::Http::Status.lookup(:unauthorized)
  # => 401

Unknown HTTP Status

require "hanami/http/status"

Hanami::Http::Status.lookup(999)
  # => raise Hanami::Action::UnknownHttpStatusError

Hanami::Http::Status.lookup(:foo)
  # => raise Hanami::Action::UnknownHttpStatusError

Parameters:

  • code (Integer, Symbol)

    a valid HTTP code

Returns:

  • (Integer)

    a message for the given status code

Raises:

See Also:

Since:

  • 2.0.2



103
104
105
# File 'lib/hanami/http/status.rb', line 103

def self.lookup(code)
  for_code(code)[0]
end

.message_for(code) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a message for the given status code

Examples:

Integer HTTP Status

require "hanami/http/status"

Hanami::Http::Status.message_for(401)
  # => "Unauthorized"

Symbol HTTP Status

require "hanami/http/status"

Hanami::Http::Status.message_for(:unauthorized)
  # => "Unauthorized"

Unknown HTTP Status

require "hanami/http/status"

Hanami::Http::Status.message_for(999)
  # => raise Hanami::Action::UnknownHttpStatusError

Hanami::Http::Status.message_for(:foo)
  # => raise Hanami::Action::UnknownHttpStatusError

Parameters:

  • code (Integer, Symbol)

    a valid HTTP code

Returns:

  • (String)

    a message for the given status code

Raises:

See Also:

Since:

  • 0.3.2



141
142
143
# File 'lib/hanami/http/status.rb', line 141

def self.message_for(code)
  for_code(code)[1]
end