Module: LatoCore::Interface::Apihelpers

Included in:
LatoCore::Interface
Defined in:
lib/lato_core/interfaces/apihelpers.rb

Overview

This module contains a list of functions used to write application or modules api. They permit to have a nice standard on api structure.

Instance Method Summary collapse

Instance Method Details

#core__send_bad_request_error(error) ⇒ Object

This function render an error for a bad request with an error message.



28
29
30
# File 'lib/lato_core/interfaces/apihelpers.rb', line 28

def core__send_bad_request_error(error)
  render json: { result: false, error_message: error }, status: 400
end

#core__send_entity_error(entity) ⇒ Object

This function render an error message with the errors contained on an entity.



23
24
25
# File 'lib/lato_core/interfaces/apihelpers.rb', line 23

def core__send_entity_error(entity)
  render_request_fail(entity.errors.full_messages.to_sentence)
end

#core__send_request_fail(error, payload: nil) ⇒ Object

This function render an error message with a possible custom payload.



15
16
17
18
19
# File 'lib/lato_core/interfaces/apihelpers.rb', line 15

def core__send_request_fail(error, payload: nil)
  response = { result: false, error_message: error }
  response[:payload] = payload if payload
  render json: response
end

#core__send_request_success(payload) ⇒ Object

This function render a normal success response with a custom payload.



8
9
10
11
12
# File 'lib/lato_core/interfaces/apihelpers.rb', line 8

def core__send_request_success(payload)
  response = { result: true, error_message: nil }
  response[:payload] = payload if payload
  render json: response
end

#core__send_unauthorized_request_error(error) ⇒ Object

Tis function render an error for an unauthorized request with an error message.



34
35
36
# File 'lib/lato_core/interfaces/apihelpers.rb', line 34

def core__send_unauthorized_request_error(error)
  render json: { result: false, error_message: error }, status: 401
end