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

Instance Method Summary collapse

Instance Attribute Details

#bodyObject (readonly)

The body for the current response.



827
828
829
# File 'lib/roda.rb', line 827

def body
  @body
end

#headersObject (readonly)

The hash of response headers for the current response.



830
831
832
# File 'lib/roda.rb', line 830

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.



834
835
836
# File 'lib/roda.rb', line 834

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object

Return the response header with the given key. Example:

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


847
848
849
# File 'lib/roda.rb', line 847

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'


854
855
856
# File 'lib/roda.rb', line 854

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

#default_headersObject

The default headers to use for responses.



859
860
861
# File 'lib/roda.rb', line 859

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.



907
908
909
# File 'lib/roda.rb', line 907

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)


870
871
872
# File 'lib/roda.rb', line 870

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


886
887
888
889
890
891
892
893
# File 'lib/roda.rb', line 886

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



899
900
901
902
# File 'lib/roda.rb', line 899

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

#initializeObject

Set the default headers when creating a response.



837
838
839
840
841
842
# File 'lib/roda.rb', line 837

def initialize
  @status  = nil
  @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')


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

def redirect(path, status = 302)
  @headers[LOCATION] = path
  @status  = status
end

#roda_classObject

Return the Roda class related to this response.



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

def roda_class
  self.class.roda_class
end

#write(str) ⇒ Object

Write to the response body. Returns nil.

response.write('foo')


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

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