module CubaApi::ResponseStatus

Public Class Methods

included( base ) click to toggle source
# File lib/cuba_api/aspects/response_status.rb, line 11
def self.included( base )
  base.prepend_aspect :response_status
end

Public Instance Methods

response_status( obj, options = {}) click to toggle source
# File lib/cuba_api/aspects/response_status.rb, line 3
def response_status( obj, options = {})
  if options[ :response_status ] == false
    obj
  else
    handle_status( obj )
  end
end

Private Instance Methods

handle_status( obj ) click to toggle source
# File lib/cuba_api/aspects/response_status.rb, line 17
def handle_status( obj )
  if obj.respond_to?( :errors ) && obj.errors.size > 0
    res.status = 412 # Precondition Failed
    log_errors( obj.errors )
    obj.errors
  elsif req.post?
    res.status = 201 # Created
    set_location( obj )
    obj
  elsif req.delete?
    res.status = 204 # No Content
    ''
  else
    obj
  end
end
log_errors( errors ) click to toggle source
# File lib/cuba_api/aspects/response_status.rb, line 40
def log_errors( errors )
  status_logger.info do
    if errors.respond_to? :to_hash
      errors.to_hash.values.join( "\n" )
    else
      errors.inspect
    end
  end
end
set_location( obj ) click to toggle source
# File lib/cuba_api/aspects/response_status.rb, line 34
def set_location( obj )
  if obj.respond_to?( :id ) && ! res[ 'Location' ]
    res[ 'Location' ] = env[ 'SCRIPT_NAME' ].to_s + "/#{obj.id}"
  end
end
status_logger() click to toggle source
# File lib/cuba_api/aspects/response_status.rb, line 50
def status_logger
  logger_factory.logger( "CubaApi::ResponseStatus" )
end