Module: Repia::BaseHelper
- Included in:
- BaseController
- Defined in:
- lib/repia/core.rb
Instance Method Summary collapse
-
#exceptions_app ⇒ Object
Use this as an action triggered by exceptions_app to return a JSON response to any middleware level exceptions.
-
#find_object(model, uuid, error: Errors::NotFound) ⇒ Object
Finds an object by model and UUID and throws an error (which will be caught and re-thrown as an HTTP error) if the object does not exist.
-
#options ⇒ Object
Renders a generic OPTIONS response.
-
#render_error(status, msg) ⇒ Object
Renders a single error.
-
#render_errors(status, msgs) ⇒ Object
Renders multiple errors.
Instance Method Details
#exceptions_app ⇒ Object
Use this as an action triggered by exceptions_app to return a JSON response to any middleware level exceptions
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/repia/core.rb', line 37 def exceptions_app status = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code.to_i error = Errors::STATUS_CODE_TO_ERROR[status] if error = error::MESSAGE else # :nocov: status = 500 = "Unknown error" # :nocov: end render_error status, end |
#find_object(model, uuid, error: Errors::NotFound) ⇒ Object
Finds an object by model and UUID and throws an error (which will be caught and re-thrown as an HTTP error) if the object does not exist. The error can be optionally suppresed by specifying nil to error.
An Repia::Errors::NotFound is raised if specified to do so when the object could not be found using the uuid.
87 88 89 90 91 92 93 94 |
# File 'lib/repia/core.rb', line 87 def find_object(model, uuid, error: Errors::NotFound) logger.debug("Attempting to get #{model.name} #{uuid}") obj = model.find_by_uuid(uuid) if obj.nil? && !error.nil? raise error, "#{model.name} #{uuid} cannot be found" end return obj end |
#options ⇒ Object
Renders a generic OPTIONS response. The actual controller must override this action if desired to have specific OPTIONS handling logic.
56 57 58 59 60 61 62 63 |
# File 'lib/repia/core.rb', line 56 def # echo back access-control-request-headers if request.headers["Access-Control-Request-Headers"] response["Access-Control-Allow-Headers"] = request.headers["Access-Control-Request-Headers"] end render body: "", status: 200 end |
#render_error(status, msg) ⇒ Object
Renders a single error.
68 69 70 |
# File 'lib/repia/core.rb', line 68 def render_error(status, msg) render json: {errors: [msg]}, status: status end |
#render_errors(status, msgs) ⇒ Object
Renders multiple errors
75 76 77 |
# File 'lib/repia/core.rb', line 75 def render_errors(status, msgs) render json: {errors: msgs}, status: status end |