Module: NeetoCommonsBackend::ApiResponders

Defined in:
lib/neeto_commons_backend/controllers/concerns/api_responders.rb

Instance Method Summary collapse

Instance Method Details

#render_error(error, status = :unprocessable_entity, context = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/neeto_commons_backend/controllers/concerns/api_responders.rb', line 13

def render_error(error, status = :unprocessable_entity, context = {})
  error_message = error
  is_exception = error.kind_of?(StandardError)
  if is_exception
    is_having_record = error.methods.include? "record"
    error_message = is_having_record ? error.record.errors_to_sentence : error.message
  end
  error_message = error_message.first if error_message.is_a?(Array) && error_message.length == 1
  key = error_message.is_a?(Array) ? "errors" : "error"
  render_json({ key => error_message }.merge(context), status)
end

#render_json(json = {}, status = :ok) ⇒ Object



29
30
31
# File 'lib/neeto_commons_backend/controllers/concerns/api_responders.rb', line 29

def render_json(json = {}, status = :ok)
  render status:, json:
end

#render_message(notice_code, status, **context) ⇒ Object



5
6
7
# File 'lib/neeto_commons_backend/controllers/concerns/api_responders.rb', line 5

def render_message(notice_code, status, **context)
  render status:, json: { notice_code: }.merge(context)
end

#render_notice(notice, context = {}) ⇒ Object



25
26
27
# File 'lib/neeto_commons_backend/controllers/concerns/api_responders.rb', line 25

def render_notice(notice, context = {})
  render_json({ notice: }.merge(context))
end

#render_success(**context) ⇒ Object



9
10
11
# File 'lib/neeto_commons_backend/controllers/concerns/api_responders.rb', line 9

def render_success(**context)
  render_message "thumbs_up", :ok, **context
end