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 445 def attachment(filename = nil, disposition='attachment') 446 if filename 447 param_filename = File.basename(filename) 448 encoding = param_filename.encoding 449 450 needs_encoding = param_filename.gsub!(/[^ 0-9a-zA-Z!\#$&\+\.\^_`\|~]+/, '-') 451 params = "; filename=#{param_filename.inspect}" 452 453 if needs_encoding && (encoding == UTF8_ENCODING || encoding == ISO88591_ENCODING) 454 # File name contains non attr-char characters from RFC 5987 Section 3.2.1 455 456 encoded_filename = File.basename(filename).force_encoding(BINARY_ENCODING) 457 # Similar regexp as above, but treat each byte separately, and encode 458 # space characters, since those aren't allowed in attr-char 459 encoded_filename.gsub!(/[^0-9a-zA-Z!\#$&\+\.\^_`\|~]/) do |c| 460 "%%%X" % c.ord 461 end 462 463 encoded_params = "; filename*=#{encoding.to_s}''#{encoded_filename}" 464 end 465 466 unless @headers[RodaResponseHeaders::CONTENT_TYPE] 467 ext = File.extname(filename) 468 unless ext.empty? 469 content_type(ext) 470 end 471 end 472 end 473 @headers[RodaResponseHeaders::CONTENT_DISPOSITION] = "#{disposition}#{params}#{encoded_params}" 474 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 395 def body(value = (return @body unless defined?(yield); nil), &block) 396 if block 397 @body = DelayedBody.new(&block) 398 else 399 self.body = value 400 end 401 end
Set the body to the given value.
# File lib/roda/plugins/sinatra_helpers.rb 404 def body=(body) 405 @body = DelayedBody.new{body} 406 end
Whether or not the status is set to 4xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 492 def client_error? 493 @status.between?(400, 499) if @status 494 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 427 def content_type(type = nil || (return @headers[RodaResponseHeaders::CONTENT_TYPE]), opts = OPTS) 428 unless (mime_type = mime_type(type) || opts[:default]) 429 raise RodaError, "Unknown media type: #{type}" 430 end 431 432 unless opts.empty? 433 opts.each do |key, val| 434 next if key == :default || (key == :charset && mime_type.include?('charset')) 435 val = val.inspect if val =~ /[";,]/ 436 mime_type += "#{mime_type.include?(';') ? ', ' : ';'}#{key}=#{val}" 437 end 438 end 439 440 @headers[RodaResponseHeaders::CONTENT_TYPE] = mime_type 441 end
If the body is a DelayedBody
, set the appropriate length for it.
# File lib/roda/plugins/sinatra_helpers.rb 409 def finish 410 @length = @body.length if @body.is_a?(DelayedBody) && !@headers[RodaResponseHeaders::CONTENT_LENGTH] 411 super 412 end
Set multiple response headers with Hash, or return the headers if no argument is given.
# File lib/roda/plugins/sinatra_helpers.rb 416 def headers(hash = nil || (return @headers)) 417 @headers.merge!(hash) 418 end
Whether or not the status is set to 1xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 477 def informational? 478 @status.between?(100, 199) if @status 479 end
Look up a media type by file extension in Rack’s mime registry.
# File lib/roda/plugins/sinatra_helpers.rb 421 def mime_type(type) 422 roda_class.mime_type(type) 423 end
Whether or not the status is set to 404. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 502 def not_found? 503 @status == 404 if @status 504 end
Whether or not the status is set to 3xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 487 def redirect? 488 @status.between?(300, 399) if @status 489 end
Whether or not the status is set to 5xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 497 def server_error? 498 @status.between?(500, 599) if @status 499 end
Set or retrieve the response status code.
# File lib/roda/plugins/sinatra_helpers.rb 389 def status(value = nil || (return @status)) 390 @status = value 391 end
Whether or not the status is set to 2xx. Returns nil if status not yet set.
# File lib/roda/plugins/sinatra_helpers.rb 482 def success? 483 @status.between?(200, 299) if @status 484 end