docbrown/app/controllers/api/v0/api_controller.rb
Andy Zhao 3e89796d9d Add API Analytics Endpoints (#2293)
* Add API endpoints for analytics

* Remove comment and spacing

* Use proper time formatting

* Refactor and clean up some logic

* Add pro and org member traits

* Use shared examples for analytics API spec

* Add analytics authorization specs

* Remove accidental include Pundit

* Raise not_authorized for invalid requests

* Make attributes and time methods private

* Add custom UnauthorizedError

* Raise and use rescue_from to handle 4xx requests

* Enforce date parameters to follow a specific format

* Use current user to allow same origin requests

* Use custom headers and not params for token auth

* Default to current user for same origin requests

* Use correct boolean for choosing user

* Update tests to use request headers and add stricter tests
2019-04-04 10:52:45 -04:00

29 lines
1 KiB
Ruby

class Api::V0::ApiController < ApplicationController
def cors_set_access_control_headers
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS"
headers["Access-Control-Allow-Headers"] = "Origin, Content-Type, Accept, Authorization, Token"
headers["Access-Control-Max-Age"] = "1728000"
end
def cors_preflight_check
return unless request.method == "OPTIONS"
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS"
headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-Prototype-Version, Token"
headers["Access-Control-Max-Age"] = "1728000"
render text: "", content_type: "text/plain"
end
def unprocessable_entity(exception)
render json: { error: exception, status: 422 },
status: :unprocessable_entity
end
def not_authorized
render json: { error: "Not authorized", status: 401 },
status: :unauthorized
end
end