Module: Repia::Helper::Base

Included in:
Controller::Base
Defined in:
lib/repia/helper/base.rb

Overview

This helper module includes methods that are essential for building a RESTful API.

Instance Method Summary collapse

Instance Method Details

#exceptions_appObject

Use this as an action triggered by exceptions_app to return a JSON response to any middleware level exceptions.

For example,

config.exceptions_app = lambda { |env|

ApplicationController.action(:exceptions_app).call(env)

}



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/repia/helper/base.rb', line 21

def exceptions_app
  ex_wrapper = ActionDispatch::ExceptionWrapper.new(Rails.env, @exception)
  status = ex_wrapper.status_code.to_i
  error = Errors::STATUS_CODE_TO_ERROR[status]
  if error
    message = error::MESSAGE
  else
    # :nocov:
    status = 500
    message = "Unknown error"
    # :nocov:
  end
  render_error status, message
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.



72
73
74
75
76
77
78
79
# File 'lib/repia/helper/base.rb', line 72

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

#optionsObject

Renders a generic OPTIONS response. The actual controller must override this action if desired to have specific OPTIONS handling logic.



41
42
43
44
45
46
47
48
# File 'lib/repia/helper/base.rb', line 41

def options
  # 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.



53
54
55
# File 'lib/repia/helper/base.rb', line 53

def render_error(status, msg)
  render json: {errors: [msg]}, status: status
end

#render_errors(status, msgs) ⇒ Object

Renders multiple errors



60
61
62
# File 'lib/repia/helper/base.rb', line 60

def render_errors(status, msgs)
  render json: {errors: msgs}, status: status
end