Class: MiniApi::DefaultResponder

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_api/default_responder.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller, resource, options = {}) ⇒ DefaultResponder

Returns a new instance of DefaultResponder.



7
8
9
10
11
# File 'lib/mini_api/default_responder.rb', line 7

def initialize(controller, resource, options = {})
  @controller = controller
  @resource = resource
  @options = options
end

Instance Method Details

#respondObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mini_api/default_responder.rb', line 13

def respond
  success = @options[:success] != false

  data = transform_keys

  body =
    if success
      { data: data }
    else
      { errors: data }
    end

  @controller.render(
    json: {
      success: success,
      message: @options[:message] || nil,
      **body
    },
    status: @options[:status] || :ok
  )
end