Module: QAuthRubyClient::ApiHelper

Includes:
ActionController::HttpAuthentication::Token::ControllerMethods
Defined in:
app/helpers/q_auth_ruby_client/api_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



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, options| QAuthRubyClient::User.find_by(auth_token: token)}
end

#embed_stack_in_json_response?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/helpers/q_auth_ruby_client/api_helper.rb', line 46

def embed_stack_in_json_response?
  ["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.message,
                description: I18n.translate("response.#{e.message.underscore}")
              }
            }
    @data[:errors][:details] = @errors unless @errors.blank?
    @data[:errors][:stack] = e.backtrace if embed_stack_in_json_response?

  rescue Exception => e

    @data = {
              errors: {
                name:  e.message,
                description: I18n.translate("response.#{e.message.underscore}"),
                details: @errors
              }
            }
    @data[:errors][:stack] = e.backtrace if embed_stack_in_json_response?

  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_tokenObject



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
      set_notification_messages(I18n.t("response.error"), I18n.t("response.authentication_error"), :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  end
end

#require_auth_tokenObject



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
      set_notification_messages(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_tokenObject



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
      set_notification_messages(I18n.t("response.error"), I18n.t("response.authentication_error"), :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  end
end