Module: Virtuatable::API::Responses

Included in:
Controllers::Base
Defined in:
lib/virtuatable/api/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/virtuatable/api/responses.rb', line 23

def api_created(item)
  halt 201, enhanced_json(item)
end

#api_emptyObject

Displays an empty body with a 204 status code



40
41
42
# File 'lib/virtuatable/api/responses.rb', line 40

def api_empty
  halt 204, ''
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/virtuatable/api/responses.rb', line 29

def api_item(item)
  halt 200, enhanced_json(item)
end

#api_list(items, mapper = :to_h) ⇒ 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/virtuatable/api/responses.rb', line 13

def api_list(items, mapper = :to_h)
  halt 200, {
    count: items.count,
    items: items.map { |item| enhanced_h(item, mapper.to_sym) }
  }.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/virtuatable/api/responses.rb', line 35

def api_ok(message)
  api_item message: message
end