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) ⇒ Response
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 ⇒ String?
private
Return last modified.
-
#merge_headers(new_headers) ⇒ Response
Return response with merged headers.
-
#rack_array ⇒ Array(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>)
Return rack compatible array.
-
#to_rack_response ⇒ Array(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>)
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
59 60 61 |
# File 'lib/response.rb', line 59 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
47 48 49 |
# File 'lib/response.rb', line 47 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
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
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_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
224 225 226 |
# File 'lib/response.rb', line 224 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
194 195 196 |
# File 'lib/response.rb', line 194 def content_type headers['Content-Type'] end |
#last_modified ⇒ 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 last modified
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
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_array ⇒ Array(Fixnum, Enumerable(Hash{String => String}), Enumerable<String>)
Return rack compatible array
183 184 185 |
# File 'lib/response.rb', line 183 def rack_array [status.code, headers, body] end |
#to_rack_response ⇒ Array(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
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
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
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
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
77 78 79 |
# File 'lib/response.rb', line 77 def with_status(status) self.class.new(status, headers, body) end |