Module: Roda::RodaPlugins::Base::ResponseMethods

Defined in:
lib/roda.rb

Overview

Instance methods for RodaResponse

Constant Summary collapse

DEFAULT_HEADERS =
{"Content-Type" => "text/html".freeze}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject (readonly)

The body for the current response.



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

def body
  @body
end

#headersObject (readonly)

The hash of response headers for the current response.



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

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.



935
936
937
# File 'lib/roda.rb', line 935

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object

Return the response header with the given key. Example:

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


948
949
950
# File 'lib/roda.rb', line 948

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'


955
956
957
# File 'lib/roda.rb', line 955

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

#default_headersObject

The default headers to use for responses.



960
961
962
# File 'lib/roda.rb', line 960

def default_headers
  DEFAULT_HEADERS
end

#default_statusObject

Return the default response status to be used when the body has been written to. This is split out to make overriding easier in plugins.



1015
1016
1017
# File 'lib/roda.rb', line 1015

def default_status
  200
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)


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

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 the return value of default_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'},
#      []]


987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/roda.rb', line 987

def finish
  b = @body
  empty = b.empty?
  s = (@status ||= empty ? 404 : default_status)
  set_default_headers
  h = @headers

  if empty && (s == 304 || s == 204 || s == 205 || (s >= 100 && s <= 199))
    h.delete("Content-Type")
  else
    h["Content-Length"] ||= @length.to_s
  end

  [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.



1007
1008
1009
1010
# File 'lib/roda.rb', line 1007

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

#initializeObject

Set the default headers when creating a response.



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

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

#inspectObject

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



1020
1021
1022
# File 'lib/roda.rb', line 1020

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


1029
1030
1031
1032
1033
# File 'lib/roda.rb', line 1029

def redirect(path, status = 302)
  @headers["Location"] = path
  @status  = status
  nil
end

#roda_classObject

Return the Roda class related to this response.



1036
1037
1038
# File 'lib/roda.rb', line 1036

def roda_class
  self.class.roda_class
end

#write(str) ⇒ Object

Write to the response body. Returns nil.

response.write('foo')


1043
1044
1045
1046
1047
1048
# File 'lib/roda.rb', line 1043

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