Class: OpenAPIRest::RestRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_rest/rest_renderer.rb

Overview

Rest Response Renderer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ RestRenderer

Returns a new instance of RestRenderer.



9
10
11
12
# File 'lib/openapi_rest/rest_renderer.rb', line 9

def initialize(args)
  @controller = args[:controller]
  @response = args[:response]
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



6
7
8
# File 'lib/openapi_rest/rest_renderer.rb', line 6

def controller
  @controller
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/openapi_rest/rest_renderer.rb', line 7

def response
  @response
end

Instance Method Details

#renderObject



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

def render
  unless response.is_a?(OpenAPIRest::QueryResponse)
    controller.render(response)
    return
  end

  return controller.render(error_response) if response.errors?

  method = controller.request.request_method_symbol
  return controller.render(nothing: true, status: 204) if [:delete, :patch, :put].include?(method)

  if method == :post
    path = "#{controller.openapi_path[:namespace]}#{response.resource}_path"
    controller.render(json: to_json_response, status: 201, location: controller.send(path, response.results))
    return
  end

  controller.render(json: to_json_response)
end