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



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

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



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

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:



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

def status
  @status
end

Class Method Details

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

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:

  • (Array)

    rack compatible array



242
243
244
245
246
# File 'lib/response.rb', line 242

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)


219
220
221
# File 'lib/response.rb', line 219

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)


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

def content_type
  headers['Content-Type']
end

#last_modifiedTime?

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:

  • (Time)

    if last modified header is present

  • (nil)

    otherwise



207
208
209
210
# File 'lib/response.rb', line 207

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

#merge_headers(headers) ⇒ Response

Return response with merged headers

Examples:


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

Parameters:

  • headers (Hash)

    the headers to merge

Returns:

  • (Response)

    returns new response with merged header



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

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

#rack_arrayArray

Return rack compatible array

Examples:


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

Returns:

  • (Array)

    rack compatible array



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

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

#to_rack_responseArray

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)

    rack compatible array

Raises:

  • InvalidError raises InvalidError when request containts undefined components



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

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



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

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



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

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_header({'Foo' => 'Bar'})
response.headers # => {'Foo' => 'Bar'}

Parameters:

  • headers (Hash)

    the header for the response

Returns:

  • (Response)

    new response with headers set



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

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



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

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