Module: Roda::RodaPlugins::Base::ResponseMethods
- Defined in:
- lib/roda.rb
Overview
Instance methods for RodaResponse
Constant Summary collapse
- CONTENT_LENGTH =
"Content-Length".freeze
- DEFAULT_HEADERS =
{"Content-Type" => "text/html".freeze}.freeze
- LOCATION =
"Location".freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
The body for the current response.
-
#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
-
#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.
-
#finish_with_body(body) ⇒ Object
Return the rack response array using a given body.
-
#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.
-
#roda_class ⇒ Object
Return the Roda class related to this response.
- #set_cookie(key, value) ⇒ Object
-
#write(str) ⇒ Object
Write to the response body.
Instance Attribute Details
#body ⇒ Object (readonly)
The body for the current response.
926 927 928 |
# File 'lib/roda.rb', line 926 def body @body end |
#headers ⇒ Object (readonly)
The hash of response headers for the current response.
929 930 931 |
# File 'lib/roda.rb', line 929 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.
933 934 935 |
# File 'lib/roda.rb', line 933 def status @status end |
Instance Method Details
#[](key) ⇒ Object
Return the response header with the given key. Example:
response['Content-Type'] # => 'text/html'
946 947 948 |
# File 'lib/roda.rb', line 946 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'
953 954 955 |
# File 'lib/roda.rb', line 953 def []=(key, value) @headers[key] = value end |
#default_headers ⇒ Object
The default headers to use for responses.
958 959 960 |
# File 'lib/roda.rb', line 958 def default_headers DEFAULT_HEADERS end |
#delete_cookie(key, value = {}) ⇒ Object
962 963 964 965 |
# File 'lib/roda.rb', line 962 def (key, value = {}) RodaPlugins.deprecate("RodaResponse#delete_cookie is deprecated and will be removed in Roda 2. It has been moved to the cookies plugin.") ::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
974 975 976 |
# File 'lib/roda.rb', line 974 def empty? @body.empty? end |
#finish ⇒ Object
Return the rack response array of status, headers, and body for the current response. If the status has not been set, uses a 200 status if the body has been written to, otherwise uses a 404 status. Adds the Content-Length header to the size of the response body.
Example:
response.finish
# => [200,
# {'Content-Type'=>'text/html', 'Content-Length'=>'0'},
# []]
990 991 992 993 994 995 996 997 |
# File 'lib/roda.rb', line 990 def finish b = @body s = (@status ||= b.empty? ? 404 : 200) set_default_headers h = @headers h[CONTENT_LENGTH] ||= @length.to_s [s, h, b] end |
#finish_with_body(body) ⇒ Object
Return the rack response array using a given body. Assumes a 200 response status unless status has been explicitly set, and doesn’t add the Content-Length header or use the existing body.
1003 1004 1005 1006 |
# File 'lib/roda.rb', line 1003 def finish_with_body(body) set_default_headers [@status || 200, @headers, body] end |
#initialize ⇒ Object
Set the default headers when creating a response.
936 937 938 939 940 941 |
# File 'lib/roda.rb', line 936 def initialize @status = nil @headers = {} @body = [] @length = 0 end |
#inspect ⇒ Object
Show response class, status code, response headers, and response body
1009 1010 1011 |
# File 'lib/roda.rb', line 1009 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')
1018 1019 1020 1021 |
# File 'lib/roda.rb', line 1018 def redirect(path, status = 302) @headers[LOCATION] = path @status = status end |
#roda_class ⇒ Object
Return the Roda class related to this response.
1024 1025 1026 |
# File 'lib/roda.rb', line 1024 def roda_class self.class.roda_class end |
#set_cookie(key, value) ⇒ Object
1028 1029 1030 1031 |
# File 'lib/roda.rb', line 1028 def (key, value) RodaPlugins.deprecate("RodaResponse#set_cookie is deprecated and will be removed in Roda 2. It has been moved to the cookies plugin.") ::Rack::Utils.(@headers, key, value) end |
#write(str) ⇒ Object
Write to the response body. Returns nil.
response.write('foo')
1036 1037 1038 1039 1040 1041 |
# File 'lib/roda.rb', line 1036 def write(str) s = str.to_s @length += s.bytesize @body << s nil end |