Module: Core::Helpers::Responses

Included in:
Controllers::Base
Defined in:
lib/core/helpers/responses.rb

Overview

Modules holding the responses that are NOT errors.

Author:

Instance Method Summary collapse

Instance Method Details

#api_created(item) ⇒ Object

Displays a creation standard response, returning the informations about the created item.

Parameters:

  • item (Object)

    any object that responds to #to_h to display to the user.



23
24
25
# File 'lib/core/helpers/responses.rb', line 23

def api_created(item)
  halt 201, item.to_json
end

#api_item(item) ⇒ Object

Displays an item with the standards of the API.

Parameters:

  • item (Object)

    the item to display as a JSON formatted hash.



29
30
31
# File 'lib/core/helpers/responses.rb', line 29

def api_item(item)
  halt 200, item.to_json
end

#api_list(items) ⇒ Object

Builds a list of items as a standard API response. The response will be a JSON hash containing two keys :

  • :count will hold the number of items displayed in the list

  • :items will hold the list of items.

Parameters:

  • items (Array)

    the items to format as a standard API response.



13
14
15
16
17
18
# File 'lib/core/helpers/responses.rb', line 13

def api_list(items)
  halt 200, {
    count: items.count,
    items: items.map { |item| item.to_h }
  }.to_json
end

#api_ok(message) ⇒ Object

Displays a message with a 200 status code

Parameters:

  • message (String)

    the message to display with the API standards.



35
36
37
# File 'lib/core/helpers/responses.rb', line 35

def api_ok(message)
  api_item message: message
end