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.



891
892
893
# File 'lib/roda.rb', line 891

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.



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

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object

Return the response header with the given key. Example:

response['Content-Type'] # => 'text/html'


904
905
906
# File 'lib/roda.rb', line 904

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'


911
912
913
# File 'lib/roda.rb', line 911

def []=(key, value)
  @headers[key] = value
end

#default_headersObject

The default headers to use for responses.



921
922
923
# File 'lib/roda.rb', line 921

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')


932
933
934
# File 'lib/roda.rb', line 932

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)


943
944
945
# File 'lib/roda.rb', line 943

def empty?
  @body.empty?
end

#finishObject

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'},
#      []]


959
960
961
962
963
964
965
# File 'lib/roda.rb', line 959

def finish
  b = @body
  s = (@status ||= b.empty? ? 404 : 200)
  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.



971
972
973
# File 'lib/roda.rb', line 971

def finish_with_body(body)
  [@status || 200, @headers, body]
end

#initializeObject

Set the default headers when creating a response.



894
895
896
897
898
899
# File 'lib/roda.rb', line 894

def initialize
  @status  = nil
  @headers = default_headers
  @body    = []
  @length  = 0
end

#inspectObject

Show response class, status code, response headers, and response body



916
917
918
# File 'lib/roda.rb', line 916

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')


980
981
982
983
# File 'lib/roda.rb', line 980

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')


989
990
991
# File 'lib/roda.rb', line 989

def set_cookie(key, value)
  ::Rack::Utils.set_cookie_header!(@headers, key, value)
end

#write(str) ⇒ Object

Write to the response body. Returns nil.

response.write('foo')


996
997
998
999
1000
1001
# File 'lib/roda.rb', line 996

def write(str)
  s = str.to_s
  @length += s.bytesize
  @body << s
  nil
end