Class: Webbed::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/webbed/method.rb

Overview

Representation of an HTTP Method.

Constant Summary collapse

DEFAULTS =
{
  :safe => false,
  :idempotent => false,
  :allowable_entities => [:request, :response]
}
OPTIONS =
new('OPTIONS', :safe => true,  :idempotent => true,  :allowable_entities => [:response])
GET =
new('GET',     :safe => true,  :idempotent => true,  :allowable_entities => [:response])
HEAD =
new('HEAD',    :safe => true,  :idempotent => true,  :allowable_entities => [])
POST =
new('POST',    :safe => false, :idempotent => false, :allowable_entities => [:request, :response])
PUT =
new('PUT',     :safe => false, :idempotent => true,  :allowable_entities => [:request, :response])
DELETE =
new('DELETE',  :safe => false, :idempotent => true,  :allowable_entities => [:response])
TRACE =
new('TRACE',   :safe => true,  :idempotent => true,  :allowable_entities => [:response])
CONNECT =
new('CONNECT', :safe => false, :idempotent => false, :allowable_entities => [:request, :response])
PATCH =
new('PATCH',   :safe => false, :idempotent => false, :allowable_entities => [:request, :response])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Method

Creates a new Method.

Parameters:

  • name (String)

    the name of the Method to create

  • options (Hash) (defaults to: {})

    the options to create the Method with

Options Hash (options):

  • :safe (Boolean) — default: false

    whether or not the Method is safe

  • :idempotent (Boolean) — default: false

    whether or not the Method is idempotent

  • :allowable_entities (Array<:request, :response>) — default: [:request, :response]

    the allowable entites of a message



45
46
47
48
49
50
51
# File 'lib/webbed/method.rb', line 45

def initialize(name, options = {})
  options = DEFAULTS.merge(options)
  @name = name
  @safe = options[:safe]
  @idempotent = options[:safe] || options[:idempotent]
  @allowable_entities = options[:allowable_entities]
end

Instance Attribute Details

#allowable_entitiesArray<:request, :response> (readonly)

The allowable entities of a message.

It can contain any combination of :request and :response.

Returns:

  • (Array<:request, :response>)


15
16
17
# File 'lib/webbed/method.rb', line 15

def allowable_entities
  @allowable_entities
end

Class Method Details

.new(value, options = {}) ⇒ Method

Checks for a cached Method or creates a new one.

It caches the standard HTTP Methods that are in RFC 2616 as well as the new method PATCH. If the Method cannot be found, it calls #initialize.

Examples:

Webbed::Method.new('GET') # => Webbed::Method::GET

Parameters:

  • name (String)

    the name of the Method to create

  • options (Hash) (defaults to: {})

    the options to create the Method with

Returns:

  • (Method)

    the new or cached Method

See Also:



28
29
30
31
32
33
34
# File 'lib/webbed/method.rb', line 28

def self.new(value, options = {})
  if const_defined?(value)
    const_get(value)
  else
    super(value, options)
  end
end

Instance Method Details

#==(other_method) ⇒ Boolean

Compares the Method to another Method.

Two Methods are equal if they have the same name.

Parameters:

  • other_method (#to_s)

    the other Method

Returns:

  • (Boolean)


80
81
82
# File 'lib/webbed/method.rb', line 80

def ==(other_method)
  to_s == other_method.to_s
end

#idempotent?Boolean

Whether or not the Method is idempotent.

Returns:

  • (Boolean)


63
64
65
# File 'lib/webbed/method.rb', line 63

def idempotent?
  @idempotent
end

#safe?Boolean

Whether or not the Method is safe.

Returns:

  • (Boolean)


56
57
58
# File 'lib/webbed/method.rb', line 56

def safe?
  @safe
end

#to_sString

Converts the Method to a string.

Returns:

  • (String)


70
71
72
# File 'lib/webbed/method.rb', line 70

def to_s
  @name
end