Class: RailsErrorDashboard::Services::PlatformDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/platform_detector.rb

Overview

Detects the platform (iOS/Android/API) from user agent string

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent) ⇒ PlatformDetector

Returns a new instance of PlatformDetector.



13
14
15
# File 'lib/rails_error_dashboard/services/platform_detector.rb', line 13

def initialize(user_agent)
  @user_agent = user_agent
end

Class Method Details

.detect(user_agent) ⇒ Object



9
10
11
# File 'lib/rails_error_dashboard/services/platform_detector.rb', line 9

def self.detect(user_agent)
  new(user_agent).detect
end

Instance Method Details

#detectObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_error_dashboard/services/platform_detector.rb', line 17

def detect
  return "API" if @user_agent.blank?

  browser = Browser.new(@user_agent)

  if browser.device.iphone? || browser.device.ipad?
    "iOS"
  elsif browser.platform.android?
    "Android"
  elsif @user_agent&.include?("Expo")
    # Expo apps might have specific patterns
    if @user_agent.include?("iOS")
      "iOS"
    elsif @user_agent.include?("Android")
      "Android"
    else
      "Mobile"
    end
  else
    "API"
  end
end