Class: Strelka::HTTPResponse

Inherits:
Mongrel2::HTTPResponse
  • Object
show all
Extended by:
Loggability
Includes:
Constants, DataUtilities
Defined in:
lib/strelka/httpresponse.rb

Overview

An HTTP response class.

Defined Under Namespace

Modules: Negotiation, Session

Constant Summary collapse

CONTENT_TYPE_CHARSET_RE =

Pattern for matching a 'charset' parameter in a media-type string, such as the Content-type header

/;\s*charset=(?<charset>\S+)\s*/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataUtilities

autovivify, deep_copy, stringify_keys, symbolify_keys

Constructor Details

#initializeHTTPResponse

Add some instance variables to new HTTPResponses.



27
28
29
30
31
32
33
34
35
# File 'lib/strelka/httpresponse.rb', line 27

def initialize( * ) # :notnew:
  @charset   = nil
  @languages = []
  @encodings = []
  @notes     = Hash.new {|h,k| h[k] = {} }
  @cookies   = nil

  super
end

Instance Attribute Details

#charsetObject

Overridden charset of the response's entity body, as either an Encoding or a String. This will be appended to the Content-type header when the response is sent to the client, replacing any charset setting in the Content-type header already. Defaults to nil, which will cause the encoding of the entity body object to be used instead unless there's already one present in the Content-type. In any case, if the encoding is Encoding::ASCII_8BIT, no charset will be appended to the content-type header.



50
51
52
# File 'lib/strelka/httpresponse.rb', line 50

def charset
  @charset
end

#encodingsObject

An Array of any encodings that have been applied to the response's entity body, in the order they were applied. These will be set as the response's Content-Encoding header when it is sent to the client. Defaults to the empty Array.



56
57
58
# File 'lib/strelka/httpresponse.rb', line 56

def encodings
  @encodings
end

#languagesObject

The natural language(s) of the response's entity body. These will be set as the response's Content-Language header when it is sent to the client. Defaults to the empty Array.



61
62
63
# File 'lib/strelka/httpresponse.rb', line 61

def languages
  @languages
end

#notesObject (readonly)

A auto-vivifying nested Hash that plugins can use to pass data amongst themselves. The notes hash is shared between the request and response objects if the request is set on the response with #request=.



66
67
68
# File 'lib/strelka/httpresponse.rb', line 66

def notes
  @notes
end

Instance Method Details

#cookiesObject

Returns a Strelka::CookieSet that can be used to manipulate the cookies that are sent with the response, creating it if necessary.



95
96
97
98
# File 'lib/strelka/httpresponse.rb', line 95

def cookies
  @cookies = Strelka::CookieSet.new unless @cookies
  return @cookies
end

#normalized_headersObject

Overridden to add charset, encodings, and languages to outgoing headers if any of them are set.



71
72
73
74
75
76
77
78
79
80
# File 'lib/strelka/httpresponse.rb', line 71

def normalized_headers
  headers = super

  self.add_content_type_charset( headers )
  headers.content_encoding ||= self.encodings.join(', ') unless self.encodings.empty?
  headers.content_language ||= self.languages.join(', ') unless self.languages.empty?
  self.add_cookie_headers( headers )

  return headers
end

#request=(request) ⇒ Object

Set the request object associated with this response to request.



102
103
104
105
# File 'lib/strelka/httpresponse.rb', line 102

def request=( request )
  super
  @notes = request.notes
end

#resetObject

Overridden to reset charset, language, and encoding data, too.



84
85
86
87
88
89
90
# File 'lib/strelka/httpresponse.rb', line 84

def reset
  super

  @charset = nil
  @languages.clear
  @encodings.clear
end