Class: Ant::Server::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/ant/server/format.rb

Overview

See Exceptions module, since this is based on the exceptions too. This will wrap a json object into a standard format, where the response will contain some metadata about the status of the request

Constant Summary collapse

INTERNAL_SERVER_ERROR_CODE =
'INTERNAL_SERVER_ERROR'.freeze
INTERNAL_SERVER_ERROR_MESSAGE =
'Unexpected error ocurred!'.freeze

Instance Method Summary collapse

Instance Method Details

#error(response) ⇒ Object

an error found while resolving the request.



27
28
29
# File 'lib/ant/server/format.rb', line 27

def error(response)
  error_format(:error, response.code, response.message, response.data)
end

#error_format(level, code, message, data) ⇒ Object

helper to sumarize fatal and error status



40
41
42
# File 'lib/ant/server/format.rb', line 40

def error_format(level, code, message, data)
  { status: level, code: code, message: message, data: data }
end

#fail(response) ⇒ Object

an error on the request. It gives the details of the error.



21
22
23
# File 'lib/ant/server/format.rb', line 21

def fail(response)
  error_format(:fail, response.code, response.message, response.data)
end

#fatal(_data) ⇒ Object

an unhandled error ocurred during the execution of the request.



33
34
35
36
# File 'lib/ant/server/format.rb', line 33

def fatal(_data)
  error_format(:fatal, INTERNAL_SERVER_ERROR_CODE,
               INTERNAL_SERVER_ERROR_MESSAGE, {})
end

#success(response) ⇒ Object

success means there were no errors during the execution of the request it sends the result in the data field.



15
16
17
# File 'lib/ant/server/format.rb', line 15

def success(response)
  { status: :success, data: response.result || response.data }
end