Class: Hyde::Response
- Inherits:
-
Object
- Object
- Hyde::Response
- Defined in:
- lib/hyde/response.rb
Overview
Rack protocol response wrapper.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#chunk_size ⇒ Object
Returns the value of attribute chunk_size.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
-
.chunk_body(text) ⇒ Array(String)
Turn body into array of chunks.
-
.convert(obj) ⇒ Object
Ensure response correctness.
Instance Method Summary collapse
-
#add_header(key, value) ⇒ Object
Add a header to the headers hash.
-
#delete_header(key, value = nil) ⇒ Object
Delete a header value from the headers hash If no value is provided, deletes all key entries.
-
#finalize ⇒ Array(Integer,Hash,Array)
Return internal representation of Rack response.
-
#initialize(response = nil) ⇒ Response
constructor
A new instance of Response.
-
#validate ⇒ Hyde::Response
Make internal representation conformant.
Constructor Details
#initialize(response = nil) ⇒ Response
Returns a new instance of Response.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/hyde/response.rb', line 11 def initialize(response = nil) if response @status = response[0] @headers = response[1] @body = response[2] else @status = 404 @headers = {} @body = [] end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
71 72 73 |
# File 'lib/hyde/response.rb', line 71 def body @body end |
#chunk_size ⇒ Object
Returns the value of attribute chunk_size.
8 9 10 |
# File 'lib/hyde/response.rb', line 8 def chunk_size @chunk_size end |
#headers ⇒ Object
Returns the value of attribute headers.
71 72 73 |
# File 'lib/hyde/response.rb', line 71 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
71 72 73 |
# File 'lib/hyde/response.rb', line 71 def status @status end |
Class Method Details
.chunk_body(text) ⇒ Array(String)
Turn body into array of chunks
96 97 98 |
# File 'lib/hyde/response.rb', line 96 def self.chunk_body(text) text.chars.each_slice(@chunk_size).map(&:join) end |
.convert(obj) ⇒ Object
Ensure response correctness
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/hyde/response.rb', line 76 def self.convert(obj) case obj when Response obj.validate when Array Response.new(obj).validate when String, File, IO Response.new([200, { "content-type" => "text/html" }, obj]).validate else Response.new([404, {}, []]) end end |
Instance Method Details
#add_header(key, value) ⇒ Object
Add a header to the headers hash
49 50 51 52 53 54 55 56 57 |
# File 'lib/hyde/response.rb', line 49 def add_header(key, value) if @headers[key].is_a? String @headers[key] = [@headers[key], value] elsif @headers[key].is_a? Array @headers[key].append(value) else @headers[key] = value end end |
#delete_header(key, value = nil) ⇒ Object
Delete a header value from the headers hash If no value is provided, deletes all key entries
63 64 65 66 67 68 69 |
# File 'lib/hyde/response.rb', line 63 def delete_header(key, value = nil) if value and @response[key] @response[key].delete(value) else @response.delete(key) end end |
#finalize ⇒ Array(Integer,Hash,Array)
Return internal representation of Rack response
25 26 27 |
# File 'lib/hyde/response.rb', line 25 def finalize [@status, @headers, @body] end |
#validate ⇒ Hyde::Response
Make internal representation conformant
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hyde/response.rb', line 31 def validate if [204, 304].include?(@status) or (100..199).include?(@status) @headers.delete "content-length" @headers.delete "content-type" @body = [] elsif @headers.empty? @headers = { "content-length" => content_size, "content-type" => "text/html" } end @body = self.class.chunk_body(@body) if @body.is_a? String self end |