Class: JsonCrudApi::API
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- JsonCrudApi::API
- Defined in:
- lib/json-crud-api/api.rb
Instance Method Summary collapse
- #add_error(code, message, reference = nil) ⇒ Object
- #fail_forbidden ⇒ Object
- #fail_not_found ⇒ Object
- #fail_unauthorized ⇒ Object
- #fail_with_error(status, code, message, reference = nil) ⇒ Object
- #fail_with_errors(status = 422) ⇒ Object
- #logged_in? ⇒ Boolean
Instance Method Details
#add_error(code, message, reference = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/json-crud-api/api.rb', line 58 def add_error(code, , reference = nil) @errors = [] if @errors.nil? error = { :code => code, :message => , } error[:reference] = reference unless reference.nil? @errors.push error end |
#fail_forbidden ⇒ Object
89 90 91 |
# File 'lib/json-crud-api/api.rb', line 89 def fail_forbidden fail_with_error 403, 'FORBIDDEN','The user is not allowed to perform this operation on the resource.' end |
#fail_not_found ⇒ Object
81 82 83 |
# File 'lib/json-crud-api/api.rb', line 81 def fail_not_found fail_with_error 404, 'NOT_FOUND','The resource cannot be found.' end |
#fail_unauthorized ⇒ Object
85 86 87 |
# File 'lib/json-crud-api/api.rb', line 85 def fail_with_error 401, 'UNAUTHORIZED','Authorization is required to perform this operation on the resource.' end |
#fail_with_error(status, code, message, reference = nil) ⇒ Object
69 70 71 72 |
# File 'lib/json-crud-api/api.rb', line 69 def fail_with_error(status, code, , reference = nil) add_error code,,reference fail_with_errors status end |
#fail_with_errors(status = 422) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/json-crud-api/api.rb', line 74 def fail_with_errors(status = 422) halt status, JSON.fast_generate({ :success => false, :errors => @errors }) end |
#logged_in? ⇒ Boolean
54 55 56 |
# File 'lib/json-crud-api/api.rb', line 54 def logged_in? @logged_in end |