Class: Repia::BaseController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
lib/repia/core.rb

Overview

This controller is a base controller for RESTful API. Two primary features:

  • Error (exception) handling

  • Options request handling

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



58
59
60
61
62
63
# File 'lib/repia/core.rb', line 58

def exceptions_app
  status = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code.to_i
  error = Errors::STATUS_CODE_TO_ERROR[status]
  message = error ? error::MESSAGE : "Unknown error"
  render_error status, message
end

#optionsObject

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



70
71
72
73
74
75
76
77
# File 'lib/repia/core.rb', line 70

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.



82
83
84
# File 'lib/repia/core.rb', line 82

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

#render_errors(status, msgs) ⇒ Object

Renders multiple errors



89
90
91
# File 'lib/repia/core.rb', line 89

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