Methods
Public Instance
Public Instance methods
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.
# File lib/roda/plugins/sinatra_helpers.rb 443 def attachment(filename = nil, disposition='attachment') 444 if filename 445 param_filename = File.basename(filename) 446 encoding = param_filename.encoding 447 448 needs_encoding = param_filename.gsub!(/[^ 0-9a-zA-Z!\#$&\+\.\^_`\|~]+/, '-') 449 params = "; filename=#{param_filename.inspect}" 450 451 if needs_encoding && (encoding == UTF8_ENCODING || encoding == ISO88591_ENCODING) 452 # File name contains non attr-char characters from RFC 5987 Section 3.2.1 453 454 encoded_filename = File.basename(filename).force_encoding(BINARY_ENCODING) 455 # Similar regexp as above, but treat each byte separately, and encode 456 # space characters, since those aren't allowed in attr-char 457 encoded_filename.gsub!(/[^0-9a-zA-Z!\#$&\+\.\^_`\|~]/) do |c| 458 "%%%X" % c.ord 459 end 460 461 encoded_params = "; filename*=#{encoding.to_s}''#{encoded_filename}" 462 end 463 464 unless @headers[RodaResponseHeaders::CONTENT_TYPE] 465 ext = File.extname(filename) 466 unless ext.empty? 467 content_type(ext) 468 end 469 end 470 end 471 @headers[RodaResponseHeaders::CONTENT_DISPOSITION] = "#{disposition}#{params}#{encoded_params}" 472 end
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 393 def body(value = (return @body unless defined?(yield); nil), &block) 394 if block 395 @body = DelayedBody.new(&block) 396 else 397 self.body = value 398 end 399 end
Set the body to the given value.
# File lib/roda/plugins/sinatra_helpers.rb 402 def body=(body) 403 @body = DelayedBody.new{body} 404 end
Whether or not the status is set to 4xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 490 def client_error? 491 @status.between?(400, 499) if @status 492 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 425 def content_type(type = nil || (return @headers[RodaResponseHeaders::CONTENT_TYPE]), opts = OPTS) 426 unless (mime_type = mime_type(type) || opts[:default]) 427 raise RodaError, "Unknown media type: #{type}" 428 end 429 430 unless opts.empty? 431 opts.each do |key, val| 432 next if key == :default || (key == :charset && mime_type.include?('charset')) 433 val = val.inspect if val =~ /[";,]/ 434 mime_type += "#{mime_type.include?(';') ? ', ' : ';'}#{key}=#{val}" 435 end 436 end 437 438 @headers[RodaResponseHeaders::CONTENT_TYPE] = mime_type 439 end
If the body is a DelayedBody
, set the appropriate length for it.
# File lib/roda/plugins/sinatra_helpers.rb 407 def finish 408 @length = @body.length if @body.is_a?(DelayedBody) && !@headers[RodaResponseHeaders::CONTENT_LENGTH] 409 super 410 end
Set multiple response headers with Hash, or return the headers if no argument is given.
# File lib/roda/plugins/sinatra_helpers.rb 414 def headers(hash = nil || (return @headers)) 415 @headers.merge!(hash) 416 end
Whether or not the status is set to 1xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 475 def informational? 476 @status.between?(100, 199) if @status 477 end
Look up a media type by file extension in Rack’s mime registry.
# File lib/roda/plugins/sinatra_helpers.rb 419 def mime_type(type) 420 roda_class.mime_type(type) 421 end
Whether or not the status is set to 404. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 500 def not_found? 501 @status == 404 if @status 502 end
Whether or not the status is set to 3xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 485 def redirect? 486 @status.between?(300, 399) if @status 487 end
Whether or not the status is set to 5xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 495 def server_error? 496 @status.between?(500, 599) if @status 497 end
Set or retrieve the response status code.
# File lib/roda/plugins/sinatra_helpers.rb 387 def status(value = nil || (return @status)) 388 @status = value 389 end
Whether or not the status is set to 2xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 480 def success? 481 @status.between?(200, 299) if @status 482 end