module OneRoster::ResponseMethods
  extend ActiveSupport::Concern

  def not_found_response(exception)
    model = exception.to_s[/find (.*?) with/, 1]
    render_error :not_found, {
      statusInfoSet:[{
        msx_codeMajor: :failure,
        imsx_severity: :error,
        imsx_description: "Sycamore School: Invalid #{model} object",
        imsx_codeMinor: :unknown_object
      }]
    }
  end

  def error_response(exception)
    render_error exception.http_status, exception.to_hash
  end

  def render_success(status, options={})
    @status_info_set ||= nil
    json = @status_info_set ? options[:json].merge(statusInfoSet: @status_info_set) : options[:json]
    render status: status, json: json
  end

  def render_error(status, json)
    render status: status, json: json
  end
end
