Module: CubaApi::ResponseStatus

Defined in:
lib/cuba_api/response_status.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



26
27
28
# File 'lib/cuba_api/response_status.rb', line 26

def self.included( base )
  base.prepend_aspect :response_status
end

Instance Method Details

#response_status(obj, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cuba_api/response_status.rb', line 3

def response_status( obj, options = {})
  if options[:response_status] != false
    if obj.respond_to?( :errors ) && obj.errors.size > 0
      res.status = 412 # Precondition Failed
      obj = obj.errors
      if obj.respond_to? :to_hash
        warn "[CubaApi::ResponseStatus] #{obj.to_hash.values.join( "\n" )}"
      else
        warn "[CubaApi::ResponseStatus] #{obj.inspect}"
      end
    elsif req.post?
      res.status = 201 # Created
      if obj.respond_to?( :id ) && ! res[ 'Location' ]
        res[ 'Location' ] = env[ 'SCRIPT_NAME' ].to_s + "/#{obj.id}"
      end
    elsif req.delete?
      res.status = 204 # No Content
      obj = ''
    end
  end
  obj
end