Module: QAuthRubyClient::ApiHelper
- Includes:
- ActionController::HttpAuthentication::Token::ControllerMethods
- Defined in:
- app/helpers/q_auth_ruby_client/api_helper.rb
Instance Method Summary collapse
- #current_user ⇒ Object
- #embed_stack_in_json_response? ⇒ Boolean
-
#render_json_response(proc_code) ⇒ Object
This method will accept a proc, execute it and render the json.
- #require_admin_auth_token ⇒ Object
- #require_auth_token ⇒ Object
- #require_super_admin_auth_token ⇒ Object
Instance Method Details
#current_user ⇒ Object
5 6 7 8 |
# File 'app/helpers/q_auth_ruby_client/api_helper.rb', line 5 def current_user # Return if @current_user is already initialized else check if the user exists with the auth token present in request header @current_user ||= authenticate_with_http_token { |token, | QAuthRubyClient::User.find_by(auth_token: token)} end |
#embed_stack_in_json_response? ⇒ Boolean
46 47 48 |
# File 'app/helpers/q_auth_ruby_client/api_helper.rb', line 46 def ["true", "t", "1", "yes"].include?(params[:debug].to_s.downcase.strip) # || Rails.env == "development" end |
#render_json_response(proc_code) ⇒ Object
This method will accept a proc, execute it and render the json
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/helpers/q_auth_ruby_client/api_helper.rb', line 64 def render_json_response(proc_code) begin proc_code.call @status ||= 200 @success = @success == false ? (false) : (true) rescue ValidationError, InvalidLoginError, FailedToCreateError, FailedToUpdateError, FailedToDeleteError, AuthenticationError => e @status = 200 @success = false @data = { errors: { name: e., description: I18n.translate("response.#{e..underscore}") } } @data[:errors][:details] = @errors unless @errors.blank? @data[:errors][:stack] = e.backtrace if rescue Exception => e @data = { errors: { name: e., description: I18n.translate("response.#{e..underscore}"), details: @errors } } @data[:errors][:stack] = e.backtrace if end response_hash = {success: @success} response_hash[:alert] = @alert unless @alert.blank? response_hash[:total_data] = @total_data unless @total_data.blank? response_hash[:per_page] = @per_page unless @per_page.blank? response_hash[:current_page] = @current_page unless @current_page.blank? response_hash[:data] = @data unless @data.blank? render status: @status, json: response_hash return end |
#require_admin_auth_token ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/helpers/q_auth_ruby_client/api_helper.rb', line 34 def require_admin_auth_token current_user unless @current_user && @current_user.is_admin? proc_code = Proc.new do (I18n.t("response.error"), I18n.t("response.authentication_error"), :error) raise AuthenticationError end render_json_response(proc_code) return end end |
#require_auth_token ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/helpers/q_auth_ruby_client/api_helper.rb', line 10 def require_auth_token current_user unless @current_user proc_code = Proc.new do (I18n.t("response.error"), I18n.t("response.authentication_error"), :error) raise AuthenticationError end render_json_response(proc_code) return end end |
#require_super_admin_auth_token ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/helpers/q_auth_ruby_client/api_helper.rb', line 22 def require_super_admin_auth_token current_user unless @current_user && @current_user.is_super_admin? proc_code = Proc.new do (I18n.t("response.error"), I18n.t("response.authentication_error"), :error) raise AuthenticationError end render_json_response(proc_code) return end end |