Class: Webbed::Response

Inherits:
Object
  • Object
show all
Includes:
GenericMessage, Helpers::EntityHeadersHelper, Helpers::RackResponseHelper, Helpers::ResponseHeadersHelper
Defined in:
lib/webbed/response.rb

Overview

Representation of an HTTP Response.

This class contains the absolute minimum for accessing the different parts of an HTTP Response. Helper modules provide far more functionality.

Constant Summary collapse

STATUS_CODE_REGEX =
/^(\d{3}) (.*)$/

Instance Attribute Summary collapse

Attributes included from GenericMessage

#entity_body, #headers, #http_version

Instance Method Summary collapse

Methods included from Helpers::EntityHeadersHelper

#content_length, #content_length=, #content_location, #content_location=, #content_md5, #content_md5=, #content_type, #content_type=

Methods included from Helpers::ResponseHeadersHelper

#age, #age=, #etag, #etag=, #location, #location=

Methods included from Helpers::RackResponseHelper

included

Methods included from GenericMessage

#to_s

Constructor Details

#initialize(status_code, headers, entity_body, options = {}) ⇒ Response

Creates a new Response.

The Status Code may be passed in as a string with a Reason Phrase or just a number. If a number is provided, the default Reason Phrase for that Status Code is used.

The method converts the values passed in to their proper types.

Examples:

Webbed::Response.new(200, {}, '')
Webbed::Response.new('404 Missing File', {}, '')

Parameters:

  • status_code (Fixnum, String)
  • headers (Headers, Hash)
  • entity_body (#to_s)
  • options (Array) (defaults to: {})

    the options to create the Response with @option



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/webbed/response.rb', line 53

def initialize(status_code, headers, entity_body, options = {})
  if STATUS_CODE_REGEX =~ status_code.to_s
    self.status_code = $1
    self.reason_phrase = $2
  else
    self.status_code = status_code
  end
  
  self.headers = headers
  self.entity_body = entity_body
  
  self.http_version = options.delete(:http_version) || 1.1
end

Instance Attribute Details

#reason_phraseString

The Reason Phrase of the Response.

The method returns the Status Code's default reason phrase if the Reason Phrase has not already been set.

Returns:

  • (String)


29
30
31
# File 'lib/webbed/response.rb', line 29

def reason_phrase
  defined?(@reason_phrase) ? @reason_phrase : @status_code.default_reason_phrase
end

#status_code#to_i

The Status Code of the Response.

The method automatically converts the new value to an instance of StatusCode if it is not already one.

Returns:

  • (#to_i)


17
18
19
# File 'lib/webbed/response.rb', line 17

def status_code
  @status_code
end

Instance Method Details

#status_lineString Also known as: start_line

The Status-Line of the Response.

Examples:

response = Webbed::Response.new([200, {}, ''])
response.status_line # => "HTTP/1.1 200 OK\r\n"

Returns:

  • (String)


74
75
76
# File 'lib/webbed/response.rb', line 74

def status_line
  "#{http_version} #{status_code} #{reason_phrase}\r\n"
end