Created
April 6, 2020 20:20
-
-
Save amkisko/f5dd530e20a7466cbb49b66a802737de to your computer and use it in GitHub Desktop.
Basic controller for Rails app with graphql-ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
include Pundit | |
skip_forgery_protection | |
rescue_from(StandardError) do |err| | |
log_error(err) | |
if Rails.env.development? | |
render_error err | |
else | |
render_error Error::InternalServerError.new(code: err.class.to_s.sub(/:+/, "_").underscore.upcase) | |
end | |
end | |
def render_error(err) | |
data = { | |
data: nil, | |
errors: [ | |
{ | |
message: err.message, | |
extensions: { | |
code: err.try(:code) || "UNKNOWN_ERROR", | |
level: "controller" | |
} | |
} | |
] | |
} | |
render json: data | |
end | |
private def log_error(error, extra = {}) | |
ServerError.log!(error, request: request, extra: extra) | |
end | |
def root | |
@current_time = Time.current | |
end | |
def telegram_webhook | |
TelegramBot::Router.execute(params.dig("message")) | |
head :ok | |
end | |
private def current_user_session | |
return if user_session_id.blank? | |
@current_user_session ||= begin | |
token = user_session_class.active.find_by(id: user_session_id) | |
token if token.present? && | |
token.authenticated?( | |
password: user_session_password, | |
request: request | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment