Module: Roda::RodaPlugins::SinatraHelpers::ResponseMethods

Defined in:
lib/roda/plugins/sinatra_helpers.rb

Instance Method Summary collapse

Instance Method Details

#attachment(filename = nil, disposition = 'attachment') ⇒ Object

Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.



433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/roda/plugins/sinatra_helpers.rb', line 433

def attachment(filename = nil, disposition='attachment')
  if filename
    params = "; filename=#{File.basename(filename).inspect}"
    unless @headers["Content-Type"]
      ext = File.extname(filename)
      unless ext.empty?
        content_type(ext)
      end
    end
  end
  @headers["Content-Disposition"] = "#{disposition}#{params}"
end

#body(value = (return @body unless block_given?; nil), &block) ⇒ Object

Set or retrieve the response body. When a block is given, evaluation is deferred until the body is needed.



383
384
385
386
387
388
389
# File 'lib/roda/plugins/sinatra_helpers.rb', line 383

def body(value = (return @body unless block_given?; nil), &block)
  if block
    @body = DelayedBody.new(&block)
  else
    self.body = value
  end
end

#body=(body) ⇒ Object

Set the body to the given value.



392
393
394
# File 'lib/roda/plugins/sinatra_helpers.rb', line 392

def body=(body)
  @body = DelayedBody.new{body}
end

#client_error?Boolean

Whether or not the status is set to 4xx. Returns nil if status not yet set.

Returns:

  • (Boolean)


462
463
464
# File 'lib/roda/plugins/sinatra_helpers.rb', line 462

def client_error?
  @status.between?(400, 499) if @status
end

#content_type(type = (return @headers["Content-Type"]; nil), opts = OPTS) ⇒ Object

Set the Content-Type of the response body given a media type or file extension. See plugin documentation for options.



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/roda/plugins/sinatra_helpers.rb', line 415

def content_type(type = (return @headers["Content-Type"]; nil), opts = OPTS)
  unless (mime_type = mime_type(type) || opts[:default])
    raise RodaError, "Unknown media type: #{type}"
  end

  unless opts.empty?
    opts.each do |key, val|
      next if key == :default || (key == :charset && mime_type.include?('charset'))
      val = val.inspect if val =~ /[";,]/
      mime_type += "#{mime_type.include?(';') ? ', ' : ';'}#{key}=#{val}"
    end
  end

  @headers["Content-Type"] = mime_type
end

#finishObject

If the body is a DelayedBody, set the appropriate length for it.



397
398
399
400
# File 'lib/roda/plugins/sinatra_helpers.rb', line 397

def finish
  @length = @body.length if @body.is_a?(DelayedBody) && !@headers["Content-Length"]
  super
end

#headers(hash = (return @headers; nil)) ⇒ Object

Set multiple response headers with Hash, or return the headers if no argument is given.



404
405
406
# File 'lib/roda/plugins/sinatra_helpers.rb', line 404

def headers(hash = (return @headers; nil))
  @headers.merge!(hash)
end

#informational?Boolean

Whether or not the status is set to 1xx. Returns nil if status not yet set.

Returns:

  • (Boolean)


447
448
449
# File 'lib/roda/plugins/sinatra_helpers.rb', line 447

def informational?
  @status.between?(100, 199) if @status
end

#mime_type(type) ⇒ Object

Look up a media type by file extension in Rack’s mime registry.



409
410
411
# File 'lib/roda/plugins/sinatra_helpers.rb', line 409

def mime_type(type)
  roda_class.mime_type(type)
end

#not_found?Boolean

Whether or not the status is set to 404. Returns nil if status not yet set.

Returns:

  • (Boolean)


472
473
474
# File 'lib/roda/plugins/sinatra_helpers.rb', line 472

def not_found?
  @status == 404 if @status
end

#redirect?Boolean

Whether or not the status is set to 3xx. Returns nil if status not yet set.

Returns:

  • (Boolean)


457
458
459
# File 'lib/roda/plugins/sinatra_helpers.rb', line 457

def redirect?
  @status.between?(300, 399) if @status
end

#server_error?Boolean

Whether or not the status is set to 5xx. Returns nil if status not yet set.

Returns:

  • (Boolean)


467
468
469
# File 'lib/roda/plugins/sinatra_helpers.rb', line 467

def server_error?
  @status.between?(500, 599) if @status
end

#status(value = (return @status; nil)) ⇒ Object

Set or retrieve the response status code.



377
378
379
# File 'lib/roda/plugins/sinatra_helpers.rb', line 377

def status(value = (return @status; nil))
  @status = value
end

#success?Boolean

Whether or not the status is set to 2xx. Returns nil if status not yet set.

Returns:

  • (Boolean)


452
453
454
# File 'lib/roda/plugins/sinatra_helpers.rb', line 452

def success?
  @status.between?(200, 299) if @status
end