Module: Strelka::ResponseHelpers

Extended by:
Loggability
Includes:
Constants
Included in:
App, AuthProvider, HTTPRequest, Strelka::Router::Default
Defined in:
lib/strelka/mixins.rb

Overview

A collection of functions for generating responses.

Class Method Summary collapse

Class Method Details

.finish_with(http_status, message = nil, headers = {}) ⇒ Object

Abort the current execution and return a response with the specified http_status code immediately. The specified message will be logged, and will be included in any message that is returned as part of the response. The headers hash will be used to set response headers. As a shortcut, you can call #finish_with again with the Hash that it builds to re-throw it.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/strelka/mixins.rb', line 369

def finish_with( http_status, message=nil, headers={} )
  status_info = nil

  if http_status.is_a?( Hash ) && http_status.key?(:status)
    self.log.debug "Re-finishing with a status_info struct: %p." % [ http_status ]
    status_info = http_status
  else
    message ||= HTTP::STATUS_NAME[ http_status ]
    status_info = {
      status:    http_status,
      message:   message,
      headers:   headers,
      backtrace: caller(1),
    }
  end

  # Log using the instance logger if possible, else use the global one
  logmsg = "Finishing with status %d: %s" % [ status_info[:status], status_info[:message] ]
  if status_info[:status] > 399
    self.log.info( logmsg )
  else
    self.log.error( logmsg )
  end


  throw :finish, status_info
end