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

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.

Parameters:

  • name (String)
  • value (String)


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.

Parameters:

  • array

    headers



150
151
152
# File 'lib/tilia/http/message_decorator_trait.rb', line 150

def add_headers(headers)
  @inner.add_headers(headers)
end

#bodyObject

Returns the message body, as it’s internal representation.

This could be either a string or a stream.

Returns:

  • resource|string



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.

Parameters:

  • resource

    body



53
54
55
# File 'lib/tilia/http/message_decorator_trait.rb', line 53

def body=(body)
  @inner.body = body
end

#body_as_streamObject

Returns the body as a readable stream resource.

Note that the stream may not be rewindable, and therefore may only be read once.

Returns:

  • resource



26
27
28
# File 'lib/tilia/http/message_decorator_trait.rb', line 26

def body_as_stream
  @inner.body_as_stream
end

#body_as_stringString

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.

Returns:

  • (String)


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.

Parameters:

  • name (String)

Returns:

  • (String, nil)


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.

Parameters:

  • name (String)

Returns:

  • (Boolean)

    bool



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.

Parameters:

  • name (String)

Returns:

  • (String)


101
102
103
# File 'lib/tilia/http/message_decorator_trait.rb', line 101

def header_as_array(name)
  @inner.header_as_array(name)
end

#headersObject

Returns all the HTTP headers as an array.

Every header is returned as an array, with one or more values.

Returns:

  • array



62
63
64
# File 'lib/tilia/http/message_decorator_trait.rb', line 62

def headers
  @inner.headers
end

#http_versionString

Returns the HTTP version.

Returns:

  • (String)


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.

Parameters:

  • version (String)


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.

Returns:

  • bool



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.

Parameters:

  • name (String)
  • value (String, Array<String>)


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.

Parameters:

  • array

    headers



127
128
129
# File 'lib/tilia/http/message_decorator_trait.rb', line 127

def update_headers(headers)
  @inner.update_headers(headers)
end