Class: Jeanine::Response
- Inherits:
-
Object
- Object
- Jeanine::Response
- Defined in:
- lib/jeanine/response.rb
Instance Attribute Summary collapse
-
#action_variables ⇒ Object
Returns the value of attribute action_variables.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #complete! ⇒ Object
- #content_type=(type) ⇒ Object
-
#initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' }) ⇒ Response
constructor
A new instance of Response.
- #redirect_to(target, status = 302) ⇒ Object
- #write(string) ⇒ Object
Constructor Details
#initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' }) ⇒ Response
Returns a new instance of Response.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jeanine/response.rb', line 10 def initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' }) @body = body @status = status @headers = headers @length = 0 return if body == [] if body.respond_to? :to_str write body.to_str elsif body.respond_to? :each body.each { |i| write i.to_s } else raise TypeError, 'body must #respond_to? #to_str or #each' end end |
Instance Attribute Details
#action_variables ⇒ Object
Returns the value of attribute action_variables.
6 7 8 |
# File 'lib/jeanine/response.rb', line 6 def action_variables @action_variables end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/jeanine/response.rb', line 7 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/jeanine/response.rb', line 7 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
8 9 10 |
# File 'lib/jeanine/response.rb', line 8 def status @status end |
Instance Method Details
#complete! ⇒ Object
26 27 28 29 30 31 |
# File 'lib/jeanine/response.rb', line 26 def complete! unless (100..199).include?(status) || status == 204 headers['Content-Length'] = @length.to_s end [status, headers, body] end |
#content_type=(type) ⇒ Object
33 34 35 |
# File 'lib/jeanine/response.rb', line 33 def content_type=(type) headers['Content-Type'] = type end |
#redirect_to(target, status = 302) ⇒ Object
37 38 39 40 |
# File 'lib/jeanine/response.rb', line 37 def redirect_to(target, status = 302) self.status = status headers['Location'] = target end |
#write(string) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/jeanine/response.rb', line 42 def write(string) s = string.to_s @length += s.bytesize body << s end |