Class: Yaframework::Response
- Inherits:
-
Object
- Object
- Yaframework::Response
- Defined in:
- lib/yaframework/response.rb
Defined Under Namespace
Modules: ContentType
Constant Summary collapse
- LOCATION =
"Location"
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
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete_cookie(key, value = {}) ⇒ Object
- #finish ⇒ Object
- #html(str) ⇒ Object
-
#initialize(body = [], status = 200, headers = { "Content-Type" => "text/html; charset=utf-8" }) ⇒ Response
constructor
A new instance of Response.
- #json(str) ⇒ Object
- #redirect(target, status = 302) ⇒ Object
- #set_cookie(key, value) ⇒ Object
- #text(str) ⇒ 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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/yaframework/response.rb', line 16 def initialize(body = [], status = 200, headers = { "Content-Type" => "text/html; charset=utf-8" }) @body = [] @headers = headers @status = 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.
14 15 16 |
# File 'lib/yaframework/response.rb', line 14 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
14 15 16 |
# File 'lib/yaframework/response.rb', line 14 def headers @headers end |
#status ⇒ Object
Returns the value of attribute status.
13 14 15 |
# File 'lib/yaframework/response.rb', line 13 def status @status end |
Instance Method Details
#[](key) ⇒ Object
48 49 50 |
# File 'lib/yaframework/response.rb', line 48 def [](key) @headers[key] end |
#[]=(key, value) ⇒ Object
52 53 54 |
# File 'lib/yaframework/response.rb', line 52 def []=(key, value) @headers[key] = value end |
#delete_cookie(key, value = {}) ⇒ Object
75 76 77 |
# File 'lib/yaframework/response.rb', line 75 def (key, value = {}) Rack::Utils.(@headers, key, value) end |
#finish ⇒ Object
31 32 33 34 |
# File 'lib/yaframework/response.rb', line 31 def finish @headers[Rack::CONTENT_LENGTH] = @length.to_s unless (100..199).include?(status) || status == 204 [status, headers, body] end |
#html(str) ⇒ Object
56 57 58 59 |
# File 'lib/yaframework/response.rb', line 56 def html(str) @headers[Rack::CONTENT_TYPE] = ContentType::HTML write(str) end |
#json(str) ⇒ Object
66 67 68 69 |
# File 'lib/yaframework/response.rb', line 66 def json(str) @headers[Rack::CONTENT_TYPE] = ContentType::JSON write(str) end |
#redirect(target, status = 302) ⇒ Object
36 37 38 39 |
# File 'lib/yaframework/response.rb', line 36 def redirect(target, status = 302) @status = status @headers[LOCATION] = target end |
#set_cookie(key, value) ⇒ Object
71 72 73 |
# File 'lib/yaframework/response.rb', line 71 def (key, value) Rack::Utils.(@headers, key, value) end |
#text(str) ⇒ Object
61 62 63 64 |
# File 'lib/yaframework/response.rb', line 61 def text(str) @headers[Rack::CONTENT_TYPE] = ContentType::TEXT write(str) end |
#write(string) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/yaframework/response.rb', line 41 def write(string) s = string.to_s @length += s.bytesize @body << s nil end |