Class: Landline::Response
- Inherits:
-
Object
- Object
- Landline::Response
- Defined in:
- lib/landline/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.
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#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_cookie(cookie) ⇒ Object
Add a cookie to the response.
-
#add_header(key, value) ⇒ Object
Add a header to the headers hash.
-
#delete_cookie(key, value) ⇒ Object
Delete a cookie If no value is provided, deletes all cookies with the same key.
-
#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 ⇒ Landline::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 22 |
# File 'lib/landline/response.rb', line 11 def initialize(response = nil) = {} if response @status = response[0] @headers = response[1] @body = response[2] else @status = 200 @headers = {} @body = [] end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
99 100 101 |
# File 'lib/landline/response.rb', line 99 def body @body end |
#chunk_size ⇒ Object
Returns the value of attribute chunk_size.
8 9 10 |
# File 'lib/landline/response.rb', line 8 def chunk_size @chunk_size end |
#cookies ⇒ Object
Returns the value of attribute cookies.
99 100 101 |
# File 'lib/landline/response.rb', line 99 def end |
#headers ⇒ Object
Returns the value of attribute headers.
99 100 101 |
# File 'lib/landline/response.rb', line 99 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
99 100 101 |
# File 'lib/landline/response.rb', line 99 def status @status end |
Class Method Details
.chunk_body(text) ⇒ Array(String)
Turn body into array of chunks
124 125 126 |
# File 'lib/landline/response.rb', line 124 def self.chunk_body(text) text.chars.each_slice(@chunk_size).map(&:join) end |
.convert(obj) ⇒ Object
Ensure response correctness
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/landline/response.rb', line 104 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_cookie(cookie) ⇒ Object
Add a cookie to the response
54 55 56 57 58 59 60 |
# File 'lib/landline/response.rb', line 54 def () if [.key] [.key].append() else [.key] = [] end end |
#add_header(key, value) ⇒ Object
Add a header to the headers hash
77 78 79 80 81 82 83 84 85 |
# File 'lib/landline/response.rb', line 77 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_cookie(key, value) ⇒ Object
Delete a cookie If no value is provided, deletes all cookies with the same key
66 67 68 69 70 71 72 |
# File 'lib/landline/response.rb', line 66 def (key, value) if value [key].delete(value) else .delete(key) 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
91 92 93 94 95 96 97 |
# File 'lib/landline/response.rb', line 91 def delete_header(key, value = nil) if value and @headers[key].is_a? Array @headers[key].delete(value) else @headers.delete(key) end end |
#finalize ⇒ Array(Integer,Hash,Array)
Return internal representation of Rack response
26 27 28 29 30 31 32 33 |
# File 'lib/landline/response.rb', line 26 def finalize .each do |_, | .each do || add_header("set-cookie", .finalize) end end [@status, @headers, @body] end |
#validate ⇒ Landline::Response
Make internal representation conformant
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/landline/response.rb', line 37 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 |