Class: Kanaui::EngineController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/kanaui/engine_controller.rb

Instance Method Summary collapse

Instance Method Details

#as_string(e) ⇒ Object



53
54
55
56
57
58
59
60
# File 'app/controllers/kanaui/engine_controller.rb', line 53

def as_string(e)
  if e.is_a?(KillBillClient::API::ResponseError)
    "Error #{e.response.code}: #{as_string_from_response(e.response.body)}"
  else
    log_rescue_error(e)
    e.message
  end
end

#as_string_from_response(response) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/kanaui/engine_controller.rb', line 66

def as_string_from_response(response)
  error_message = response
  begin
    # BillingExceptionJson?
    error_message = JSON.parse response
  rescue StandardError => _e
  end

  if error_message.respond_to?(:[]) && error_message['message'].present?
    # Likely BillingExceptionJson
    error_message = error_message['message']
  end
  # Limit the error size to avoid ActionDispatch::Cookies::CookieOverflow
  error_message[0..1000]
end

#current_tenant_userObject



11
12
13
14
15
16
# File 'app/controllers/kanaui/engine_controller.rb', line 11

def current_tenant_user
  # If the rails application on which that engine is mounted defines such method (Devise), we extract the current user,
  # if not we default to nil, and serve our static mock configuration
  user = current_user if respond_to?(:current_user)
  Kanaui.current_tenant_user.call(session, user)
end

#dashboard_reports_json_request?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/controllers/kanaui/engine_controller.rb', line 49

def dashboard_reports_json_request?
  controller_name == 'dashboard' && action_name == 'reports' && params[:format].blank?
end

#get_layoutObject



7
8
9
# File 'app/controllers/kanaui/engine_controller.rb', line 7

def get_layout
  layout ||= Kanaui.config[:layout]
end

#json_request?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/controllers/kanaui/engine_controller.rb', line 45

def json_request?
  request.format.json? || params[:format] == 'json' || dashboard_reports_json_request?
end

#log_rescue_error(error) ⇒ Object



62
63
64
# File 'app/controllers/kanaui/engine_controller.rb', line 62

def log_rescue_error(error)
  Rails.logger.warn "#{error.class} #{error}. #{error.backtrace.join("\n")}"
end

#options_for_klientObject



18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/kanaui/engine_controller.rb', line 18

def options_for_klient
  user = current_tenant_user
  {
    username: user[:username],
    password: user[:password],
    session_id: user[:session_id],
    api_key: user[:api_key],
    api_secret: user[:api_secret]
  }
end