Module: JwtAuthenticable::Responses
- Included in:
- Auth
- Defined in:
- lib/jwt_authenticable/responses.rb
Overview
Provides a suite of methods to return standardized JSON Http responses
Instance Method Summary collapse
-
#accepted ⇒ Object
Method to render accepted status in a consistent way.
-
#access_denied(resource_errors) ⇒ Object
Method to render access denied errors in a consistent way.
-
#created(instance = nil) ⇒ Object
Method to render created status in a consistent way.
-
#destroyed ⇒ Object
Method to render destroyed status in a consistent way.
-
#no_content ⇒ Object
Method to render no content in a consistent way.
-
#not_found(resource_errors) ⇒ Object
Method to render not found errors in a consistent way.
- #payload_too_large(_details) ⇒ Object
- #render_page(paginated_query, _config = {}) ⇒ Object
-
#render_resource(resource) ⇒ Object
renders a resource (eg json file) or rendes an error of there are errors in the resource.
-
#render_success_resource(resource) ⇒ Object
Method to render success status in a consistent way.
- #unauthorized(detail) ⇒ Object
-
#unprocessable_entity(resource_errors) ⇒ Object
Method to render unprocessable entity errors in a consistent way.
-
#validation_error(resource_errors) ⇒ Object
Method to render validation errors in a consistent way.
Instance Method Details
#accepted ⇒ Object
Method to render accepted status in a consistent way
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jwt_authenticable/responses.rb', line 90 def accepted render json: { result: [ { status: '202', title: 'Accepted', detail: 'Your request will be processed', code: '100' } ] }, status: :accepted end |
#access_denied(resource_errors) ⇒ Object
Method to render access denied errors in a consistent way
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/jwt_authenticable/responses.rb', line 32 def access_denied(resource_errors) render json: { errors: [ { status: '403', title: 'Access Denied', detail: resource_errors, code: '100' } ] }, status: :forbidden end |
#created(instance = nil) ⇒ Object
Method to render created status in a consistent way
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jwt_authenticable/responses.rb', line 61 def created(instance = nil) render json: { result: [ { status: '201', title: 'created', detail: 'resource created', code: '100', instance: instance } ] }, status: :created end |
#destroyed ⇒ Object
Method to render destroyed status in a consistent way
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/jwt_authenticable/responses.rb', line 104 def destroyed render json: { result: [ { status: '200', title: 'destroyed', detail: 'resource destroyed', code: '100' } ] }, status: :ok end |
#no_content ⇒ Object
Method to render no content in a consistent way
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/jwt_authenticable/responses.rb', line 133 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
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/jwt_authenticable/responses.rb', line 47 def not_found(resource_errors) render json: { errors: [ { status: '404', title: 'Not Found', detail: resource_errors, code: '100' } ] }, status: :not_found end |
#payload_too_large(_details) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/jwt_authenticable/responses.rb', line 172 def payload_too_large(_details) render json: { result: [ { status: '413', title: 'payload too large', details: nil, code: '100' } ] }, status: :payload_too_large end |
#render_page(paginated_query, _config = {}) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/jwt_authenticable/responses.rb', line 159 def render_page(paginated_query, _config = {}) render json: { data: paginated_query, page: { current_page: paginated_query.current_page, next_page: paginated_query.next_page, total_count: paginated_query.total_count, total_pages: paginated_query.total_pages, has_next: !paginated_query.last_page? && paginated_query.size.positive? } } end |
#render_resource(resource) ⇒ Object
renders a resource (eg json file) or rendes an error of there are errors in the resource
7 8 9 10 11 12 13 |
# File 'lib/jwt_authenticable/responses.rb', line 7 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
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/jwt_authenticable/responses.rb', line 76 def render_success_resource(resource) render json: { result: [ { status: '200', title: 'Success', detail: resource, code: '100' } ] }, status: :ok end |
#unauthorized(detail) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/jwt_authenticable/responses.rb', line 146 def (detail) render json: { result: [ { status: '401', title: 'unauthorized', detail: detail || 'invalid or missing credentials', code: '100' } ] }, status: :unauthorized end |
#unprocessable_entity(resource_errors) ⇒ Object
Method to render unprocessable entity errors in a consistent way
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/jwt_authenticable/responses.rb', line 119 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
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jwt_authenticable/responses.rb', line 17 def validation_error(resource_errors) render json: { errors: [ { status: '400', title: 'Bad Request', detail: resource_errors, code: '100' } ] }, status: :bad_request end |