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

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

Instance Method Summary collapse

Instance Method Details

#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.



330
331
332
333
334
335
336
# File 'lib/roda/plugins/sinatra_helpers.rb', line 330

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.



339
340
341
# File 'lib/roda/plugins/sinatra_helpers.rb', line 339

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)


394
395
396
# File 'lib/roda/plugins/sinatra_helpers.rb', line 394

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.



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/roda/plugins/sinatra_helpers.rb', line 362

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

#finishObject

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



344
345
346
347
# File 'lib/roda/plugins/sinatra_helpers.rb', line 344

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.



351
352
353
# File 'lib/roda/plugins/sinatra_helpers.rb', line 351

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.

Returns:

  • (Boolean)


379
380
381
# File 'lib/roda/plugins/sinatra_helpers.rb', line 379

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.



356
357
358
# File 'lib/roda/plugins/sinatra_helpers.rb', line 356

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)


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

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)


389
390
391
# File 'lib/roda/plugins/sinatra_helpers.rb', line 389

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)


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

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

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

Set or retrieve the response status code.



324
325
326
# File 'lib/roda/plugins/sinatra_helpers.rb', line 324

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.

Returns:

  • (Boolean)


384
385
386
# File 'lib/roda/plugins/sinatra_helpers.rb', line 384

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