class Response
Library to build rack compatible responses in a functional style
Constants
- TEXT_PLAIN
- Undefined
Undefined
response componentA class to get nice inspect behavior ootb
Attributes
Return status code
@return [#each]
if set
@return [undefined]
otherwise
@api private
Return headers code
@return [Hash]
if set
@return [undefined]
otherwise
@api private
Return status code
@return [Response::Status]
if set
@return [undefined]
otherwise
@api private
Public Class Methods
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
Return contents of cache control header
@return [String]
@api private
# File lib/response.rb, line 224 def cache_control headers['Cache-Control'] end
Return contents of content type header
@return [String]
@api private
# File lib/response.rb, line 194 def content_type headers['Content-Type'] end
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
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
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
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
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
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
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
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
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