class Response

Library to build rack compatible responses in a functional style

Constants

TEXT_PLAIN
Undefined

Undefined response component

A class to get nice inspect behavior ootb

Attributes

body[R]

Return status code

@return [#each]

if set

@return [undefined]

otherwise

@api private

headers[R]

Return headers code

@return [Hash]

if set

@return [undefined]

otherwise

@api private

status[R]

Return status code

@return [Response::Status]

if set

@return [undefined]

otherwise

@api private

Public Class Methods

build(status = Undefined, headers = {}, body = Undefined) { |response| ... } click to toggle source

Build response with a dsl like interface

@example

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'

@return [Response]

@api public

# 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

Public Instance Methods

cache_control() click to toggle source

Return contents of cache control header

@return [String]

@api private

# File lib/response.rb, line 224
def cache_control
  headers['Cache-Control']
end
content_type() click to toggle source

Return contents of content type header

@return [String]

@api private

# File lib/response.rb, line 194
def content_type
  headers['Content-Type']
end
last_modified() click to toggle source

Return last modified

@raise [ArgumentError]

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

@return [String]

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

@return [nil]

otherwise

@api private

# File lib/response.rb, line 212
def last_modified
  value = headers.fetch('Last-Modified') { return }
  Time.httpdate(value)
end
merge_headers(new_headers) click to toggle source

Return response with merged headers

@param [Hash] new_headers

the headers to merge

@example

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

@api public

@return [Response]

returns new response with merged header
# File lib/response.rb, line 137
def merge_headers(new_headers)
  self.class.new(status, headers.merge(new_headers), body)
end
rack_array() click to toggle source

Return rack compatible array

@return [Array(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>)]

rack compatible array

@example

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

@api public

# File lib/response.rb, line 183
def rack_array
  [status.code, headers, body]
end
to_rack_response() click to toggle source

Return rack compatible array after asserting response is valid

@return [Array(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>)]

a rack compatible array

@raise InvalidError

raises InvalidError when request containts undefined components

@api private

# File lib/response.rb, line 151
def to_rack_response
  assert_valid
  rack_array
end
valid?() click to toggle source

Test if object is a valid response

@return [true]

if all required fields are present

@return [false]

otherwise

@api private

# File lib/response.rb, line 166
def valid?
  ![status, headers, body].any? { |item| item.equal?(Undefined) }
end
with_body(body) click to toggle source

Return response with new body

@param [Object] body

the body for the response

@return [Response]

new response with body set

@example

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

@api public

# File lib/response.rb, line 97
def with_body(body)
  self.class.new(status, headers, body)
end
with_headers(headers) click to toggle source

Return response with new headers

@param [Hash] headers

the header for the response

@return [Response]

new response with headers set

@example

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

@api public

# File lib/response.rb, line 117
def with_headers(headers)
  self.class.new(status, headers, body)
end
with_status(status) click to toggle source

Return response with new status

@param [Fixnum] status

the status for the response

@return [Response]

new response object with status set

@example

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

@api public

# File lib/response.rb, line 77
def with_status(status)
  self.class.new(status, headers, body)
end

Private Instance Methods

assert_valid() click to toggle source

Raise error when request contains undefined components

@raise InvalidError

raises InvalidError when request contains undefined components

@return [undefined]

@api private

# File lib/response.rb, line 263
def assert_valid
  raise InvalidError, "Not a valid response: #{inspect}" unless valid?
end