Module: Tilia::Http::Message

Includes:
MessageInterface
Included in:
Request, Response
Defined in:
lib/tilia/http/message.rb

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

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

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

This could be either a string or a stream.

Returns:

  • resource|string



17
18
19
# File 'lib/tilia/http/message.rb', line 17

def body
  @body
end

#headersObject

Returns all the HTTP headers as an array.

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

Returns:

  • array



22
23
24
# File 'lib/tilia/http/message.rb', line 22

def headers
  @headers
end

#http_versionString

Returns the HTTP version.

Returns:

  • (String)


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.

Parameters:

  • name (String)
  • value (String)


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.

Parameters:

  • array

    headers



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_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



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_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)


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.

Parameters:

  • name (String)

Returns:

  • (String, nil)


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.

Parameters:

  • name (String)

Returns:

  • (Boolean)

    bool



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.

Parameters:

  • name (String)

Returns:

  • (String)


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_messageObject

TODO: document



234
235
236
237
238
# File 'lib/tilia/http/message.rb', line 234

def initialize_message
  @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.

Returns:

  • bool



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.

Parameters:

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


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.

Parameters:

  • array

    headers



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