Module: RailsErrorDashboard::UserAgentHelper

Includes:
I18nHelper
Defined in:
app/helpers/rails_error_dashboard/user_agent_helper.rb

Instance Method Summary collapse

Methods included from I18nHelper

#red_chart_date, #red_js_t, #red_js_tp, #red_js_translations, #red_locale, #red_t, #red_time_format, #red_tp

Instance Method Details

#browser_icon(browser_info) ⇒ Object

Get browser icon based on browser name



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/rails_error_dashboard/user_agent_helper.rb', line 36

def browser_icon(browser_info)
  return '<i class="bi bi-question-circle text-muted"></i>'.html_safe if browser_info.blank?

  case browser_info[:browser_name]&.downcase
  when "chrome"
    '<i class="bi bi-browser-chrome text-warning"></i>'.html_safe
  when "firefox"
    '<i class="bi bi-browser-firefox text-danger"></i>'.html_safe
  when "safari"
    '<i class="bi bi-browser-safari text-primary"></i>'.html_safe
  when "edge"
    '<i class="bi bi-browser-edge text-info"></i>'.html_safe
  else
    '<i class="bi bi-globe text-secondary"></i>'.html_safe
  end
end

#device_icon(browser_info) ⇒ Object

Get device icon based on device type



74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/rails_error_dashboard/user_agent_helper.rb', line 74

def device_icon(browser_info)
  return '<i class="bi bi-question-circle text-muted"></i>'.html_safe if browser_info.blank?

  if browser_info[:is_mobile]
    '<i class="bi bi-phone text-success"></i>'.html_safe
  elsif browser_info[:is_tablet]
    '<i class="bi bi-tablet text-info"></i>'.html_safe
  else
    '<i class="bi bi-laptop text-secondary"></i>'.html_safe
  end
end

#os_icon(browser_info) ⇒ Object

Get OS icon based on OS name



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/rails_error_dashboard/user_agent_helper.rb', line 54

def os_icon(browser_info)
  return '<i class="bi bi-question-circle text-muted"></i>'.html_safe if browser_info.blank?

  case browser_info[:os_name]&.downcase
  when /windows/
    '<i class="bi bi-windows text-primary"></i>'.html_safe
  when /mac|darwin/
    '<i class="bi bi-apple text-secondary"></i>'.html_safe
  when /linux/
    '<i class="bi bi-ubuntu text-danger"></i>'.html_safe
  when /android/
    '<i class="bi bi-android2 text-success"></i>'.html_safe
  when /ios|iphone|ipad/
    '<i class="bi bi-apple text-secondary"></i>'.html_safe
  else
    '<i class="bi bi-cpu text-muted"></i>'.html_safe
  end
end

#parse_user_agent(user_agent_string) ⇒ Object

Parse user agent string into browser, OS, and device info



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/rails_error_dashboard/user_agent_helper.rb', line 11

def parse_user_agent(user_agent_string)
  return default_user_agent_info if user_agent_string.blank?

  browser = Browser.new(user_agent_string)

  {
    browser_name: browser_name(browser),
    browser_version: browser.version,
    os_name: os_name(browser),
    device_type: device_type(browser),
    # No titleize: the platform is a machine identifier from the browser
    # gem, and titleize applies English morphology to it. Nothing renders
    # this value today; it stays raw so a future reader gets the identifier
    # rather than a mangled version of one.
    platform: browser.platform.to_s,
    is_mobile: browser.device.mobile?,
    is_tablet: browser.device.tablet?,
    is_bot: browser.bot?
  }
rescue StandardError => e
  Rails.logger.warn "[RailsErrorDashboard] Failed to parse user agent: #{e.message}"
  default_user_agent_info
end