Module: Roda::RodaPlugins::Base::ResponseMethods
- Defined in:
- lib/roda.rb
Overview
Instance methods for RodaResponse
Constant Summary collapse
- CONTENT_LENGTH =
"Content-Length".freeze
- CONTENT_TYPE =
"Content-Type".freeze
- DEFAULT_CONTENT_TYPE =
"text/html".freeze
- LOCATION =
"Location".freeze
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
The hash of response headers for the current response.
-
#status ⇒ Object
The status code to use for the response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the response header with the given key.
-
#[]=(key, value) ⇒ Object
Set the response header with the given key to the given value.
-
#default_headers ⇒ Object
The default headers to use for responses.
-
#delete_cookie(key, value = {}) ⇒ Object
Modify the headers to include a Set-Cookie value that deletes the cookie.
-
#empty? ⇒ Boolean
Whether the response body has been written to yet.
-
#finish ⇒ Object
Return the rack response array of status, headers, and body for the current response.
-
#initialize ⇒ Object
Set the default headers when creating a response.
-
#inspect ⇒ Object
Show response class, status code, response headers, and response body.
-
#redirect(path, status = 302) ⇒ Object
Set the Location header to the given path, and the status to the given status.
-
#set_cookie(key, value) ⇒ Object
Set the cookie with the given key in the headers.
-
#write(str) ⇒ Object
Write to the response body.
Instance Attribute Details
#headers ⇒ Object (readonly)
The hash of response headers for the current response.
887 888 889 |
# File 'lib/roda.rb', line 887 def headers @headers end |
#status ⇒ Object
The status code to use for the response. If none is given, will use 200 code for non-empty responses and a 404 code for empty responses.
884 885 886 |
# File 'lib/roda.rb', line 884 def status @status end |
Instance Method Details
#[](key) ⇒ Object
Return the response header with the given key. Example:
response['Content-Type'] # => 'text/html'
900 901 902 |
# File 'lib/roda.rb', line 900 def [](key) @headers[key] end |
#[]=(key, value) ⇒ Object
Set the response header with the given key to the given value.
response['Content-Type'] = 'application/json'
907 908 909 |
# File 'lib/roda.rb', line 907 def []=(key, value) @headers[key] = value end |
#default_headers ⇒ Object
The default headers to use for responses.
917 918 919 |
# File 'lib/roda.rb', line 917 def default_headers {CONTENT_TYPE => DEFAULT_CONTENT_TYPE} end |
#delete_cookie(key, value = {}) ⇒ Object
Modify the headers to include a Set-Cookie value that deletes the cookie. A value hash can be provided to override the default one used to delete the cookie. Example:
response.('foo')
response.('foo', :domain=>'example.org')
928 929 930 |
# File 'lib/roda.rb', line 928 def (key, value = {}) ::Rack::Utils.(@headers, key, value) end |
#empty? ⇒ Boolean
Whether the response body has been written to yet. Note that writing an empty string to the response body marks the response as not empty. Example:
response.empty? # => true
response.write('a')
response.empty? # => false
939 940 941 |
# File 'lib/roda.rb', line 939 def empty? @body.empty? end |
#finish ⇒ Object
Return the rack response array of status, headers, and body for the current response. Example:
response.finish # => [200, {'Content-Type'=>'text/html'}, []]
947 948 949 950 951 |
# File 'lib/roda.rb', line 947 def finish b = @body s = (@status ||= b.empty? ? 404 : 200) [s, @headers, b] end |
#initialize ⇒ Object
Set the default headers when creating a response.
890 891 892 893 894 895 |
# File 'lib/roda.rb', line 890 def initialize @status = nil @headers = default_headers @body = [] @length = 0 end |
#inspect ⇒ Object
Show response class, status code, response headers, and response body
912 913 914 |
# File 'lib/roda.rb', line 912 def inspect "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>" end |
#redirect(path, status = 302) ⇒ Object
Set the Location header to the given path, and the status to the given status. Example:
response.redirect('foo', 301)
response.redirect('bar')
958 959 960 961 |
# File 'lib/roda.rb', line 958 def redirect(path, status = 302) @headers[LOCATION] = path @status = status end |
#set_cookie(key, value) ⇒ Object
Set the cookie with the given key in the headers.
response.('foo', 'bar')
response.('foo', :value=>'bar', :domain=>'example.org')
967 968 969 |
# File 'lib/roda.rb', line 967 def (key, value) ::Rack::Utils.(@headers, key, value) end |
#write(str) ⇒ Object
Write to the response body. Updates Content-Length header with the size of the string written. Returns nil. Example:
response.write('foo')
response['Content-Length'] # =>'3'
976 977 978 979 980 981 982 983 |
# File 'lib/roda.rb', line 976 def write(str) s = str.to_s @length += s.bytesize @headers[CONTENT_LENGTH] = @length.to_s @body << s nil end |