Module: Tilia::Http::Message
Overview
This is the abstract base class for both the Request and Response objects.
This object contains a few simple methods that are shared by both.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the message body, as it’s internal representation.
-
#headers ⇒ Object
readonly
Returns all the HTTP headers as an array.
-
#http_version ⇒ String
Returns the HTTP version.
Instance Method Summary collapse
-
#add_header(name, value) ⇒ void
Adds a HTTP header.
-
#add_headers(headers) ⇒ void
Adds a new set of HTTP headers.
-
#body_as_stream ⇒ Object
Returns the body as a readable stream resource.
-
#body_as_string ⇒ String
Returns the body as a string.
-
#header(name) ⇒ String?
Returns a specific HTTP header, based on it’s name.
-
#header?(name) ⇒ Boolean
Will return true or false, depending on if a HTTP header exists.
-
#header_as_array(name) ⇒ String
Returns a HTTP header as an array.
-
#initialize_copy(_original) ⇒ Object
TODO: document.
-
#initialize_message ⇒ Object
TODO: document.
-
#remove_header(name) ⇒ Object
Removes a HTTP header.
-
#update_header(name, value) ⇒ void
Updates a HTTP header.
-
#update_headers(headers) ⇒ void
Sets a new set of HTTP headers.
Instance Attribute Details
#body ⇒ Object
Returns the message body, as it’s internal representation.
This could be either a string or a stream.
17 18 19 |
# File 'lib/tilia/http/message.rb', line 17 def body @body end |
#headers ⇒ Object
Returns all the HTTP headers as an array.
Every header is returned as an array, with one or more values.
22 23 24 |
# File 'lib/tilia/http/message.rb', line 22 def headers @headers end |
#http_version ⇒ String
Returns the HTTP version.
27 28 29 |
# File 'lib/tilia/http/message.rb', line 27 def http_version @http_version end |
Instance Method Details
#add_header(name, value) ⇒ void
This method returns an undefined value.
Adds a HTTP header.
This method will not overwrite any existing HTTP header, but instead add another value. Individual values can be retrieved with getHeadersAsArray.
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/tilia/http/message.rb', line 179 def add_header(name, value) l_name = name.downcase value = [value] unless value.is_a?(Array) if @headers.key?(l_name) @headers[l_name][1].concat value else @headers[l_name] = [name, value] end end |
#add_headers(headers) ⇒ void
This method returns an undefined value.
Adds a new set of HTTP headers.
Any existing headers will not be overwritten.
196 197 198 199 200 |
# File 'lib/tilia/http/message.rb', line 196 def add_headers(headers) headers.each do |name, value| add_header(name, value) end end |
#body_as_stream ⇒ Object
Returns the body as a readable stream resource.
Note that the stream may not be rewindable, and therefore may only be read once.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tilia/http/message.rb', line 37 def body_as_stream body = self.body if body.is_a?(String) || body.nil? stream = StringIO.new stream.write body stream.rewind return stream end body end |
#body_as_string ⇒ String
Returns the body as a string.
Note that because the underlying data may be based on a stream, this method could only work correctly the first time.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tilia/http/message.rb', line 54 def body_as_string body = self.body if body.is_a?(String) body elsif body.nil? '' else body.readlines.join("\n") end end |
#header(name) ⇒ String?
Returns a specific HTTP header, based on it’s name.
The name must be treated as case-insensitive. If the header does not exist, this method must return null.
If a header appeared more than once in a HTTP request, this method will concatenate all the values with a comma.
Note that this not make sense for all headers. Some, such as ‘Set-Cookie` cannot be logically combined with a comma. In those cases you should use header_as_array.
116 117 118 119 120 121 122 |
# File 'lib/tilia/http/message.rb', line 116 def header(name) name = name.downcase return @headers[name][1].join(',') if @headers.key?(name) nil end |
#header?(name) ⇒ Boolean
Will return true or false, depending on if a HTTP header exists.
98 99 100 |
# File 'lib/tilia/http/message.rb', line 98 def header?(name) @headers.key? name.downcase end |
#header_as_array(name) ⇒ String
Returns a HTTP header as an array.
For every time the HTTP header appeared in the request or response, an item will appear in the array.
If the header did not exists, this method will return an empty array.
133 134 135 136 137 138 139 |
# File 'lib/tilia/http/message.rb', line 133 def header_as_array(name) name = name.downcase return @headers[name][1] if @headers.key?(name) [] end |
#initialize_copy(_original) ⇒ Object
TODO: document
241 242 243 |
# File 'lib/tilia/http/message.rb', line 241 def initialize_copy(_original) @headers = @headers.deep_dup end |
#initialize_message ⇒ Object
TODO: document
234 235 236 237 238 |
# File 'lib/tilia/http/message.rb', line 234 def @body = nil @headers = {} @http_version = '1.1' end |
#remove_header(name) ⇒ Object
Removes a HTTP header.
The specified header name must be treated as case-insenstive. This method should return true if the header was successfully deleted, and false if the header did not exist.
209 210 211 212 213 214 |
# File 'lib/tilia/http/message.rb', line 209 def remove_header(name) name = name.downcase return false unless @headers.key?(name) @headers.delete name true end |
#update_header(name, value) ⇒ void
This method returns an undefined value.
Updates a HTTP header.
The case-sensitity of the name value must be retained as-is.
If the header already existed, it will be overwritten.
150 151 152 153 |
# File 'lib/tilia/http/message.rb', line 150 def update_header(name, value) value = [value] unless value.is_a?(Array) @headers[name.downcase] = [name, value] end |
#update_headers(headers) ⇒ void
This method returns an undefined value.
Sets a new set of HTTP headers.
The headers array should contain headernames for keys, and their value should be specified as either a string or an array.
Any header that already existed will be overwritten.
164 165 166 167 168 |
# File 'lib/tilia/http/message.rb', line 164 def update_headers(headers) headers.each do |name, value| update_header(name, value) end end |