Class: DeviceWizard::UserAgentDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/device_wizard.rb

Constant Summary collapse

MOBILE =

CONST Keywords

'mobile'
IPAD =
'ipad'
IPHONE =
'iphone'
ANDROID =
'android'
TABLET =
'tablet'
SILK =
'silk'
REGEX_OS =

CONST Regex Patterns

/(windows|linux|os\s+[x9]|solaris|bsd)/
REGEX_MOBILE =
/(iphone|ipod|blackberry|android|palm|windows\s+ce|mobile|symbian|phone)/
REGEX_CRAWLER =
/(spider|crawl|slurp|bot)/

Instance Method Summary collapse

Constructor Details

#initializeUserAgentDetector

Returns a new instance of UserAgentDetector.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/device_wizard.rb', line 35

def initialize
  @browser_resolvers = []
  @browser_resolvers.push(FirefoxResolver.new)
  @browser_resolvers.push(GoogleChromeResolver.new)
  @browser_resolvers.push(InternetExplorerResolver.new)
  @browser_resolvers.push(SafariResolver.new)

  @os_resolvers = []
  @os_resolvers.push(AndroidResolver.new)
  @os_resolvers.push(IOSResolver.new)
  @os_resolvers.push(MacResolver.new)
  @os_resolvers.push(WindowsResolver.new)
end

Instance Method Details

#get_browser(user_agent) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/device_wizard.rb', line 95

def get_browser(user_agent)

  @browser_resolvers.each do |r|

    browser = r.identify(user_agent)

    if browser != nil
      return browser
    end

  end

  return BrowserDetails.new

end

#get_details(user_agent) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/device_wizard.rb', line 127

def get_details(user_agent)
  details = DeviceDetails.new

  details.type = get_device_type(user_agent)
  details.browser = get_browser(user_agent)
  details.os = get_os(user_agent)

  return details
end

#get_device_type(user_agent) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/device_wizard.rb', line 53

def get_device_type(user_agent)

  if user_agent.to_s.strip.length == 0
    return DeviceType::UNKNOWN
  end

  user_agent.downcase!

  if user_agent.include? TABLET
    return DeviceType::TABLET
  end

  if user_agent.include? MOBILE
    if user_agent.include? IPAD
      return DeviceType::TABLET
    else
      return DeviceType::MOBILE
    end
  elsif user_agent.include? ANDROID
    return DeviceType::TABLET
  end

  if REGEX_MOBILE =~ user_agent
    return DeviceType::MOBILE
  end

  if user_agent.include? SILK
    return DeviceType::TABLET
  end

  if REGEX_OS =~ user_agent
    return DeviceType::DESKTOP
  end

  if REGEX_CRAWLER =~ user_agent
    return DeviceType::CRAWLER
  end

  return DeviceType::UNKNOWN

end

#get_os(user_agent) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/device_wizard.rb', line 111

def get_os(user_agent)

  @os_resolvers.each do |r|

    os = r.identify(user_agent)

    if os != nil
      return os
    end

  end

  return OperatingSystemDetails.new

end

#unknownObject



49
50
51
# File 'lib/device_wizard.rb', line 49

def unknown
  UNKNOWN
end