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.



876
877
878
# File 'lib/roda.rb', line 876

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.



873
874
875
# File 'lib/roda.rb', line 873

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object

Return the response header with the given key. Example:

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


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

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'


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

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

#default_headersObject

The default headers to use for responses.



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

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


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

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)


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

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


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

def finish
  b = @body
  s = (@status ||= b.empty? ? 404 : 200)
  [s, @headers, b]
end

#initializeObject

Set the default headers when creating a response.



879
880
881
882
883
884
# File 'lib/roda.rb', line 879

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

#inspectObject

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



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

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


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

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


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

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'


965
966
967
968
969
970
971
972
# File 'lib/roda.rb', line 965

def write(str)
  s = str.to_s

  @length += s.bytesize
  @headers[CONTENT_LENGTH] = @length.to_s
  @body << s
  nil
end