Class: Response

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/response.rb,
lib/response/xml.rb,
lib/response/html.rb,
lib/response/json.rb,
lib/response/text.rb,
lib/response/status.rb,
lib/response/redirect.rb

Overview

Library to build rack compatible responses in a functional style

Direct Known Subclasses

HTML, JSON, Redirect, Text, XML

Defined Under Namespace

Classes: HTML, InvalidError, JSON, Redirect, Status, Text, XML

Constant Summary collapse

TEXT_PLAIN =
'text/plain; charset=UTF-8'.freeze
Undefined =

Undefined response component

A class to get nice #inspect behavior ootb

Class.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#body#each, undefined (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return status code

Returns:

  • (#each)

    if set

  • (undefined)

    otherwise



59
60
61
# File 'lib/response.rb', line 59

def body
  @body
end

#headersHash, undefined (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return headers code

Returns:

  • (Hash)

    if set

  • (undefined)

    otherwise



47
48
49
# File 'lib/response.rb', line 47

def headers
  @headers
end

#statusResponse::Status, undefined (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return status code

Returns:



35
36
37
# File 'lib/response.rb', line 35

def status
  @status
end

Class Method Details

.build(status = Undefined, headers = {}, body = Undefined) ⇒ Response

Build response with a dsl like interface

Examples:


response = Response.build(200) do |response|
  response.
    with_headers('Foo' => 'Bar').
    with_body('Hello')
end

response.status  # => 200
response.headers # => {'Foo' => 'Bar' }
response.body    # => 'Hello'

Returns:



246
247
248
249
250
# File 'lib/response.rb', line 246

def self.build(status = Undefined, headers = {}, body = Undefined)
  response = new(status, headers, body)
  response = yield response if block_given?
  response
end

Instance Method Details

#cache_controlString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return contents of cache control header

Returns:

  • (String)


224
225
226
# File 'lib/response.rb', line 224

def cache_control
  headers['Cache-Control']
end

#content_typeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return contents of content type header

Returns:

  • (String)


194
195
196
# File 'lib/response.rb', line 194

def content_type
  headers['Content-Type']
end

#last_modifiedString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return last modified

Returns:

  • (String)

    if last modified header is present, a string which represents the time as rfc1123-date of HTTP-date defined by RFC 2616

  • (nil)

    otherwise

Raises:

  • (ArgumentError)

    if content of Last-Modified header is not a RFC 2616 compliant date



212
213
214
215
# File 'lib/response.rb', line 212

def last_modified
  value = headers.fetch('Last-Modified') { return }
  Time.httpdate(value)
end

#merge_headers(new_headers) ⇒ Response

Return response with merged headers

Examples:


response = Response.new(200, {'Foo' => 'Baz', 'John' => 'Doe'})
response = response.merge_headers({'Foo' => 'Bar'})
response.headers # => {'Foo' => 'Bar', 'John' => 'Doe'}

Parameters:

  • new_headers (Hash)

    the headers to merge

Returns:

  • (Response)

    returns new response with merged header



137
138
139
# File 'lib/response.rb', line 137

def merge_headers(new_headers)
  self.class.new(status, headers.merge(new_headers), body)
end

#rack_arrayArray(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>)

Return rack compatible array

Examples:


response = Response.new(200, {'Foo' => 'Bar'}, 'Hello World')
response.rack_array # => [200, {'Foo' => 'Bar'}, 'Hello World']

Returns:

  • (Array(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>))

    rack compatible array



183
184
185
# File 'lib/response.rb', line 183

def rack_array
  [status.code, headers, body]
end

#to_rack_responseArray(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return rack compatible array after asserting response is valid

Returns:

  • (Array(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>))

    a rack compatible array

Raises:

  • InvalidError raises InvalidError when request containts undefined components



151
152
153
154
# File 'lib/response.rb', line 151

def to_rack_response
  assert_valid
  rack_array
end

#valid?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test if object is a valid response

Returns:

  • (true)

    if all required fields are present

  • (false)

    otherwise



166
167
168
# File 'lib/response.rb', line 166

def valid?
  ![status, headers, body].any? { |item| item.equal?(Undefined) }
end

#with_body(body) ⇒ Response

Return response with new body

Examples:


response = Response.new
response = response.with_body('Hello')
response.body # => 'Hello'

Parameters:

  • body (Object)

    the body for the response

Returns:

  • (Response)

    new response with body set



97
98
99
# File 'lib/response.rb', line 97

def with_body(body)
  self.class.new(status, headers, body)
end

#with_headers(headers) ⇒ Response

Return response with new headers

Examples:


response = Response.new
response = response.with_headers({'Foo' => 'Bar'})
response.headers # => {'Foo' => 'Bar'}

Parameters:

  • headers (Hash)

    the header for the response

Returns:

  • (Response)

    new response with headers set



117
118
119
# File 'lib/response.rb', line 117

def with_headers(headers)
  self.class.new(status, headers, body)
end

#with_status(status) ⇒ Response

Return response with new status

Examples:


response = Response.new
response = response.with_status(200)
response.status # => 200

Parameters:

  • status (Fixnum)

    the status for the response

Returns:

  • (Response)

    new response object with status set



77
78
79
# File 'lib/response.rb', line 77

def with_status(status)
  self.class.new(status, headers, body)
end