Class: Response
- Inherits:
-
Object
- Object
- Response
- 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
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
-
#body ⇒ #each, undefined
readonly
private
Return status code.
-
#headers ⇒ Hash, undefined
readonly
private
Return headers code.
-
#status ⇒ Response::Status, undefined
readonly
private
Return status code.
Class Method Summary collapse
-
.build(status = Undefined, headers = {}, body = Undefined) ⇒ Array
Build response with a dsl like interface.
Instance Method Summary collapse
-
#cache_control ⇒ String
private
Return contents of cache control header.
-
#content_type ⇒ String
private
Return contents of content type header.
-
#last_modified ⇒ Time?
private
Return last modified.
-
#merge_headers(headers) ⇒ Response
Return response with merged headers.
-
#rack_array ⇒ Array
Return rack compatible array.
-
#to_rack_response ⇒ Array
private
Return rack compatible array after asserting response is valid.
-
#valid? ⇒ true, false
private
Test if object is a valid response.
-
#with_body(body) ⇒ Response
Return response with new body.
-
#with_headers(headers) ⇒ Response
Return response with new headers.
-
#with_status(status) ⇒ Response
Return response with new status.
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
58 59 60 |
# File 'lib/response.rb', line 58 def body @body end |
#headers ⇒ Hash, 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
46 47 48 |
# File 'lib/response.rb', line 46 def headers @headers end |
#status ⇒ Response::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
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
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_control ⇒ 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 contents of cache control header
219 220 221 |
# File 'lib/response.rb', line 219 def cache_control headers['Cache-Control'] end |
#content_type ⇒ 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 contents of content type header
193 194 195 |
# File 'lib/response.rb', line 193 def content_type headers['Content-Type'] end |
#last_modified ⇒ Time?
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
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
136 137 138 |
# File 'lib/response.rb', line 136 def merge_headers(headers) self.class.new(status, self.headers.merge(headers), body) end |
#rack_array ⇒ Array
Return rack compatible array
182 183 184 |
# File 'lib/response.rb', line 182 def rack_array [status.code, headers, body] end |
#to_rack_response ⇒ Array
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
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
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
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
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
76 77 78 |
# File 'lib/response.rb', line 76 def with_status(status) self.class.new(status, headers, body) end |