Module: Responsable

Extended by:
Dry::Configurable
Defined in:
lib/responsable.rb,
lib/responsable/version.rb

Overview

The Responsable module provides a consistent way to render json responses across all Researchable’s services

Constant Summary collapse

VERSION =
'1.4.0'

Instance Method Summary collapse

Instance Method Details

#access_denied(resource_errors) ⇒ Object

Method to render access denied errors in a consistent way

Parameters:

  • resource_errors

    the errors to render



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/responsable.rb', line 38

def access_denied(resource_errors)
  render json: {
    errors: [
      {
        status: '403',
        title: 'Access Denied',
        detail: resource_errors,
        code: '100'
      }
    ]
  }, status: :forbidden
end

#created(resource = nil) ⇒ Object

Method to render created status in a consistent way

Parameters:

  • resource (defaults to: nil)

    the resource to render



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/responsable.rb', line 68

def created(resource = nil)
  render json: {
    result: [
      {
        status: '201',
        title: 'created',
        detail: 'resource created',
        code: '100',
        instance: Responsable.config.serializer.call(resource)
      }
    ]
  }, status: :created
end

#destroyedObject

Method to render destroyed status in a consistent way



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/responsable.rb', line 98

def destroyed
  render json: {
    result: [
      {
        status: '200',
        title: 'destroyed',
        detail: 'resource destroyed',
        code: '100'
      }
    ]
  }, status: :ok
end

#no_contentObject

Method to render no content in a consistent way



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/responsable.rb', line 127

def no_content
  render json: {
    result: [
      {
        status: '204',
        title: 'no content',
        detail: 'no content provided',
        code: '100'
      }
    ]
  }, status:  :no_content
end

#not_found(resource_errors) ⇒ Object

Method to render not found errors in a consistent way

Parameters:

  • resource_errors

    the errors to render



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/responsable.rb', line 53

def not_found(resource_errors)
  render json: {
    errors: [
      {
        status: '404',
        title: 'Not Found',
        detail: resource_errors,
        code: '100'
      }
    ]
  }, status: :not_found
end

#not_implemented(detail) ⇒ Object

Method to render not implemented in a consistent way

Parameters:

  • detail

    the description of the functionality that is not supported



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/responsable.rb', line 142

def not_implemented(detail)
  render json: {
    result: [
      {
        status: '501',
        title: 'Not Implemented',
        detail: detail,
        code: '100'
      }
    ]
  }, status: :not_implemented
end

#render_page(paginated_query, serialized_data) ⇒ Object

Method to render paginated resources in a consistent way

Parameters:

  • paginated_query

    the query that has been paginated with Kaminari

  • serialized_data

    the serialized resource (should not be a hash ‘…` to prevent double nesting)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/responsable.rb', line 158

def render_page(paginated_query, serialized_data)
  if paginated_query.respond_to? :current_page
    render json: {
      data: serialized_data,
      page: {
        current_page: paginated_query.current_page,
        next_page: paginated_query.next_page,
        prev_page: paginated_query.prev_page,
        total_count: paginated_query.total_count,
        total_pages: paginated_query.total_pages,
        has_next: !paginated_query.last_page? && paginated_query.size.positive?
      }
    }
  else
    not_implemented('a pagination gem that implements #current_page is needed')
  end
end

#render_resource(resource) ⇒ Object

Method to render a resource in a consistent way. If the resource is not valid it will render a validation error

Parameters:

  • resource

    the resource to render



13
14
15
16
17
18
19
# File 'lib/responsable.rb', line 13

def render_resource(resource)
  if resource.errors.empty?
    render json: resource
  else
    validation_error(resource.errors)
  end
end

#render_success_resource(resource) ⇒ Object

Method to render success status in a consistent way

Parameters:

  • resource

    the resource to render



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/responsable.rb', line 84

def render_success_resource(resource)
  render json: {
    result: [
      {
        status: '200',
        title: 'Success',
        detail: resource,
        code: '100'
      }
    ]
  }, status: :ok
end

#unprocessable_entity(resource_errors) ⇒ Object

Method to render unprocessable entity errors in a consistent way

Parameters:

  • resource_errors

    the errors to render



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/responsable.rb', line 113

def unprocessable_entity(resource_errors)
  render json: {
    errors: [
      {
        status: '422',
        title: 'unprocessable',
        detail: resource_errors,
        code: '100'
      }
    ]
  }, status: :unprocessable_entity
end

#validation_error(resource_errors) ⇒ Object

Method to render validation errors in a consistent way

Parameters:

  • resource_errors

    the errors to render



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/responsable.rb', line 23

def validation_error(resource_errors)
  render json: {
    errors: [
      {
        status: '400',
        title: 'Bad Request',
        detail: resource_errors,
        code: '100'
      }
    ]
  }, status: :bad_request
end