Module: Usman::ApiHelper

Included in:
Api::V1::RegistrationsController
Defined in:
app/helpers/usman/api_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_userObject



4
5
6
7
# File 'app/helpers/usman/api_helper.rb', line 4

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

#embed_stack_in_json_response?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/helpers/usman/api_helper.rb', line 45

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/usman/api_helper.rb', line 50

def render_json_response(proc_code)

  begin
    proc_code.call
    @success = @success == false ? (false) : (true)
  rescue Exception => e
    @success = false
    @errors = { 
                heading: I18n.translate("response.unexpected_failure.heading"),
                message: e.message.underscore,
                details: I18n.translate("response.#{e.message.underscore}.details"),
                stacktrace: (embed_stack_in_json_response? ? e.backtrace : nil)
              }
  end
  @status ||= 200

  response_hash = {success: @success}
  response_hash[:alert] = @alert unless @alert.blank?
  response_hash[:data] = @data unless @data.blank?
  response_hash[:errors] = @errors unless @errors.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?

  render status: @status, json: response_hash
  return
end

#require_admin_auth_tokenObject



33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/usman/api_helper.rb', line 33

def require_admin_auth_token
  current_user
  unless @current_user && @current_user.is_admin?
    proc_code = Proc.new do
      set_notification_messages("authentication.permission_denied", :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  end
end

#require_auth_tokenObject



9
10
11
12
13
14
15
16
17
18
19
# File 'app/helpers/usman/api_helper.rb', line 9

def require_auth_token
  current_user
  unless @current_user
    proc_code = Proc.new do
      set_notification_messages("authentication.permission_denied", :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  end
end

#require_super_admin_auth_tokenObject



21
22
23
24
25
26
27
28
29
30
31
# File 'app/helpers/usman/api_helper.rb', line 21

def require_super_admin_auth_token
  current_user
  unless @current_user && @current_user.is_super_admin?
    proc_code = Proc.new do
      set_notification_messages("authentication.permission_denied", :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  end
end