Methods
Public Instance
Public Instance methods
Set or retrieve the response body. When a block is given, evaluation is deferred until the body is needed.
# File lib/roda/plugins/sinatra_helpers.rb 330 def body(value = (return @body unless defined?(yield); nil), &block) 331 if block 332 @body = DelayedBody.new(&block) 333 else 334 self.body = value 335 end 336 end
Set the body to the given value.
# File lib/roda/plugins/sinatra_helpers.rb 339 def body=(body) 340 @body = DelayedBody.new{body} 341 end
Whether or not the status is set to 4xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 394 def client_error? 395 @status.between?(400, 499) if @status 396 end
Set the Content-Type of the response body given a media type or file extension. See plugin documentation for options.
# File lib/roda/plugins/sinatra_helpers.rb 362 def content_type(type = nil || (return @headers[RodaResponseHeaders::CONTENT_TYPE]), opts = OPTS) 363 unless (mime_type = mime_type(type) || opts[:default]) 364 raise RodaError, "Unknown media type: #{type}" 365 end 366 367 unless opts.empty? 368 opts.each do |key, val| 369 next if key == :default || (key == :charset && mime_type.include?('charset')) 370 val = val.inspect if val =~ /[";,]/ 371 mime_type += "#{mime_type.include?(';') ? ', ' : ';'}#{key}=#{val}" 372 end 373 end 374 375 @headers[RodaResponseHeaders::CONTENT_TYPE] = mime_type 376 end
If the body is a DelayedBody, set the appropriate length for it.
# File lib/roda/plugins/sinatra_helpers.rb 344 def finish 345 @length = @body.length if @body.is_a?(DelayedBody) && !@headers[RodaResponseHeaders::CONTENT_LENGTH] 346 super 347 end
Set multiple response headers with Hash, or return the headers if no argument is given.
# File lib/roda/plugins/sinatra_helpers.rb 351 def headers(hash = nil || (return @headers)) 352 @headers.merge!(hash) 353 end
Whether or not the status is set to 1xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 379 def informational? 380 @status.between?(100, 199) if @status 381 end
Look up a media type by file extension in Rack’s mime registry.
# File lib/roda/plugins/sinatra_helpers.rb 356 def mime_type(type) 357 roda_class.mime_type(type) 358 end
Whether or not the status is set to 404. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 404 def not_found? 405 @status == 404 if @status 406 end
Whether or not the status is set to 3xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 389 def redirect? 390 @status.between?(300, 399) if @status 391 end
Whether or not the status is set to 5xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 399 def server_error? 400 @status.between?(500, 599) if @status 401 end
Set or retrieve the response status code.
# File lib/roda/plugins/sinatra_helpers.rb 324 def status(value = nil || (return @status)) 325 @status = value 326 end
Whether or not the status is set to 2xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 384 def success? 385 @status.between?(200, 299) if @status 386 end