Class: Hobbit::Response
- Inherits:
-
Object
- Object
- Hobbit::Response
- Extended by:
- Forwardable
- Defined in:
- lib/hobbit/response.rb
Instance Attribute Summary collapse
-
#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
- #finish ⇒ Object
-
#initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' }) ⇒ Response
constructor
A new instance of Response.
- #redirect(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 |
# File 'lib/hobbit/response.rb', line 10 def initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' }) @body, @headers, @status = [], headers, status @length = 0 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
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'lib/hobbit/response.rb', line 6 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/hobbit/response.rb', line 6 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
5 6 7 |
# File 'lib/hobbit/response.rb', line 5 def status @status end |
Instance Method Details
#finish ⇒ Object
23 24 25 26 27 28 |
# File 'lib/hobbit/response.rb', line 23 def finish unless (100..199).include?(status) || status == 204 headers['Content-Length'] = @length.to_s end [status, headers, body] end |
#redirect(target, status = 302) ⇒ Object
30 31 32 33 |
# File 'lib/hobbit/response.rb', line 30 def redirect(target, status = 302) self.status = status headers['Location'] = target end |
#write(string) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/hobbit/response.rb', line 35 def write(string) s = string.to_s @length += s.bytesize body << s end |