Module: Tilia::Http::MessageDecoratorTrait
- Included in:
- RequestDecorator, ResponseDecorator
- Defined in:
- lib/tilia/http/message_decorator_trait.rb
Overview
This trait contains a bunch of methods, shared by both the RequestDecorator and the ResponseDecorator.
Didn’t seem needed to create a full class for this, so we’re just implementing it as a trait.
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 ⇒ Object
Returns the message body, as it’s internal representation.
-
#body=(body) ⇒ void
Updates the body resource with a new stream.
-
#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.
-
#headers ⇒ Object
Returns all the HTTP headers as an array.
-
#http_version ⇒ String
Returns the HTTP version.
-
#http_version=(version) ⇒ void
Sets the HTTP version.
-
#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 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.
140 141 142 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 140 def add_header(name, value) @inner.add_header(name, value) 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.
150 151 152 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 150 def add_headers(headers) @inner.add_headers(headers) end |
#body ⇒ Object
Returns the message body, as it’s internal representation.
This could be either a string or a stream.
45 46 47 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 45 def body @inner.body end |
#body=(body) ⇒ void
This method returns an undefined value.
Updates the body resource with a new stream.
53 54 55 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 53 def body=(body) @inner.body = body 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.
26 27 28 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 26 def body_as_stream @inner.body_as_stream 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.
36 37 38 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 36 def body_as_string @inner.body_as_string 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.
88 89 90 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 88 def header(name) @inner.header(name) end |
#header?(name) ⇒ Boolean
Will return true or false, depending on if a HTTP header exists.
70 71 72 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 70 def header?(name) @inner.header?(name) 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.
101 102 103 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 101 def header_as_array(name) @inner.header_as_array(name) end |
#headers ⇒ Object
Returns all the HTTP headers as an array.
Every header is returned as an array, with one or more values.
62 63 64 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 62 def headers @inner.headers end |
#http_version ⇒ String
Returns the HTTP version.
178 179 180 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 178 def http_version @inner.http_version end |
#http_version=(version) ⇒ void
This method returns an undefined value.
Sets the HTTP version.
Should be 1.0 or 1.1.
171 172 173 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 171 def http_version=(version) @inner.http_version = version 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.
161 162 163 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 161 def remove_header(name) @inner.remove_header(name) 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.
114 115 116 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 114 def update_header(name, value) @inner.update_header(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.
127 128 129 |
# File 'lib/tilia/http/message_decorator_trait.rb', line 127 def update_headers(headers) @inner.update_headers(headers) end |