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

Instance Method Summary collapse

Instance Attribute Details

#headersObject (readonly)

The hash of response headers for the current response.



887
888
889
# File 'lib/roda.rb', line 887

def headers
  @headers
end

#statusObject

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_headersObject

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

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.delete_cookie('foo')
response.delete_cookie('foo', :domain=>'example.org')


928
929
930
# File 'lib/roda.rb', line 928

def delete_cookie(key, value = {})
  ::Rack::Utils.delete_cookie_header!(@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

Returns:

  • (Boolean)


939
940
941
# File 'lib/roda.rb', line 939

def empty?
  @body.empty?
end

#finishObject

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

#initializeObject

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

#inspectObject

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 the cookie with the given key in the headers.

response.set_cookie('foo', 'bar')
response.set_cookie('foo', :value=>'bar', :domain=>'example.org')


967
968
969
# File 'lib/roda.rb', line 967

def set_cookie(key, value)
  ::Rack::Utils.set_cookie_header!(@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