Class: Brut::FrontEnd::HttpMethod

Inherits:
Object
  • Object
show all
Includes:
Phlex::SGML::SafeObject
Defined in:
lib/brut/front_end/http_method.rb

Overview

Wrapper around an HTTP Method, ensuring it contains only a valid value.

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ HttpMethod

Create an HTTP method from a string.

Parameters:

  • string (String|Symbol)

    a string containing an HTTP method name. Case insensitive, and can be a symbol.

Raises:

  • (ArgumentError)

    if the passed string is not a valid HTTP method



12
13
14
15
16
17
18
# File 'lib/brut/front_end/http_method.rb', line 12

def initialize(string)
  normalized = string.to_s.downcase.to_sym
  if !self.class.method_names.include?(normalized)
    raise ArgumentError,"'#{string}' is not a known HTTP method"
  end
  @method = normalized
end

Instance Method Details

#==(other) ⇒ true|false

Returns True if the other object is the same class as this and has the same string representation.

Returns:

  • (true|false)

    True if the other object is the same class as this and has the same string representation



27
28
29
# File 'lib/brut/front_end/http_method.rb', line 27

def ==(other)
  self.class.name == other.class.name && self.to_s == other.to_s
end

#get?true|false

Returns true if this is a GET.

Returns:

  • (true|false)

    true if this is a GET



32
33
# File 'lib/brut/front_end/http_method.rb', line 32

def get?  = self.to_sym == :get
# @return [true|false] true if this is a POST

#post?true|false

Returns true if this is a POST.

Returns:

  • (true|false)

    true if this is a POST



34
# File 'lib/brut/front_end/http_method.rb', line 34

def post? = self.to_sym == :post

#to_sString Also known as: to_str

Returns the method name, normalized to all lower case, as a string.

Returns:

  • (String)

    the method name, normalized to all lower case, as a string



21
22
# File 'lib/brut/front_end/http_method.rb', line 21

def to_s = @method.to_s
# @return [Symbol] the method name, normalized to all lower case, as a symbol

#to_symSymbol

Returns the method name, normalized to all lower case, as a symbol.

Returns:

  • (Symbol)

    the method name, normalized to all lower case, as a symbol



23
# File 'lib/brut/front_end/http_method.rb', line 23

def to_sym = @method.to_sym