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