Class: DeviceDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/device_detector.rb,
lib/device_detector/os.rb,
lib/device_detector/bot.rb,
lib/device_detector/client.rb,
lib/device_detector/device.rb,
lib/device_detector/parser.rb,
lib/device_detector/browser.rb,
lib/device_detector/version.rb,
lib/device_detector/client_hint.rb,
lib/device_detector/memory_cache.rb,
lib/device_detector/name_extractor.rb,
lib/device_detector/model_extractor.rb,
lib/device_detector/vendor_fragment.rb,
lib/device_detector/version_extractor.rb,
lib/device_detector/metadata_extractor.rb

Defined Under Namespace

Classes: Bot, Browser, Client, ClientHint, Configuration, Device, MemoryCache, MetadataExtractor, ModelExtractor, NameExtractor, OS, Parser, VendorFragment, VersionExtractor

Constant Summary collapse

VERSION =
'1.1.3'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent, headers = nil) ⇒ DeviceDetector

Returns a new instance of DeviceDetector.



23
24
25
26
27
# File 'lib/device_detector.rb', line 23

def initialize(user_agent, headers = nil)
  @client_hint = ClientHint.new(headers)
  utf8_user_agent = encode_user_agent_if_needed(user_agent)
  @user_agent = build_user_agent(utf8_user_agent)
end

Instance Attribute Details

#client_hintObject (readonly)

Returns the value of attribute client_hint.



21
22
23
# File 'lib/device_detector.rb', line 21

def client_hint
  @client_hint
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



21
22
23
# File 'lib/device_detector.rb', line 21

def user_agent
  @user_agent
end

Class Method Details

.cacheObject



218
219
220
# File 'lib/device_detector.rb', line 218

def cache
  @cache ||= MemoryCache.new(config.to_hash)
end

.configObject



214
215
216
# File 'lib/device_detector.rb', line 214

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



222
223
224
225
# File 'lib/device_detector.rb', line 222

def configure
  @config = Configuration.new
  yield(config)
end

Instance Method Details

#bot?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/device_detector.rb', line 195

def bot?
  bot.bot?
end

#bot_nameObject



199
200
201
# File 'lib/device_detector.rb', line 199

def bot_name
  bot.name
end

#build_user_agent(user_agent) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/device_detector.rb', line 30

def build_user_agent(user_agent)
  return user_agent if client_hint.model.nil?

  regex = build_regex('Android 10[.\d]*; K(?: Build/|[;)])')
  return user_agent unless user_agent =~ regex

  version = client_hint.os_version || '10'

  user_agent.gsub(/(Android 10[.\d]*; K)/, "Android #{version}; #{client_hint.model}")
end

#device_brandObject



84
85
86
87
88
89
90
91
92
# File 'lib/device_detector.rb', line 84

def device_brand
  return if fake_ua?

  # Assume all devices running iOS / Mac OS are from Apple
  brand = device.brand
  brand = 'Apple' if brand.nil? && DeviceDetector::OS::APPLE_OS_NAMES.include?(os_name)

  brand
end

#device_nameObject



78
79
80
81
82
# File 'lib/device_detector.rb', line 78

def device_name
  return if fake_ua?

  device.name || client_hint.model || fix_for_x_music
end

#device_typeObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/device_detector.rb', line 94

def device_type
  t = device.type

  t = nil if fake_ua?

  # Chrome on Android passes the device type based on the keyword 'Mobile'
  # If it is present the device should be a smartphone, otherwise it's a tablet
  # See https://developer.chrome.com/multidevice/user-agent#chrome_for_android_user_agent
  # Note: We do not check for browser (family) here, as there might be mobile apps using Chrome,
  # that won't have a detected browser, but can still be detected. So we check the useragent for
  # Chrome instead.
  if t.nil? && os_family == 'Android' && user_agent =~ build_regex('Chrome\/[\.0-9]*')
    t = user_agent =~ build_regex('(?:Mobile|eliboM)') ? 'smartphone' : 'tablet'
  end

  # Some UA contain the fragment 'Pad/APad', so we assume those devices as tablets
  t = 'tablet' if t == 'smartphone' && user_agent =~ build_regex('Pad\/APad')

  # Some UA contain the fragment 'Android; Tablet;' or 'Opera Tablet', so we assume those devices
  # as tablets
  t = 'tablet' if t.nil? && (android_tablet_fragment? || opera_tablet?)

  # Some user agents simply contain the fragment 'Android; Mobile;', so we assume those devices
  # as smartphones
  t = 'smartphone' if t.nil? && android_mobile_fragment?

  # Some UA contains the 'Android; Mobile VR;' fragment
  t = 'wearable' if t.nil? && android_vr_fragment?

  # Android up to 3.0 was designed for smartphones only. But as 3.0,
  # which was tablet only, was published too late, there were a
  # bunch of tablets running with 2.x With 4.0 the two trees were
  # merged and it is for smartphones and tablets
  #
  # So were are expecting that all devices running Android < 2 are
  # smartphones Devices running Android 3.X are tablets. Device type
  # of Android 2.X and 4.X+ are unknown
  if t.nil? && os_name == 'Android' && os.full_version && !os.full_version.empty?
    full_version = Gem::Version.new(os.full_version)
    if full_version < VersionExtractor::MAJOR_VERSION_2
      t = 'smartphone'
    elsif full_version >= VersionExtractor::MAJOR_VERSION_3 && \
          full_version < VersionExtractor::MAJOR_VERSION_4
      t = 'tablet'
    end
  end

  # All detected feature phones running android are more likely a smartphone
  t = 'smartphone' if t == 'feature phone' && os_family == 'Android'

  # All unknown devices under running Java ME are more likely a features phones
  t = 'feature phone' if t.nil? && os_name == 'Java ME'

  # According to http://msdn.microsoft.com/en-us/library/ie/hh920767(v=vs.85).aspx
  # Internet Explorer 10 introduces the "Touch" UA string token. If this token is present at the
  # end of the UA string, the computer has touch capability, and is running Windows 8 (or later).
  # This UA string will be transmitted on a touch-enabled system running Windows 8 (RT)
  #
  # As most touch enabled devices are tablets and only a smaller part are desktops/notebooks we
  # assume that all Windows 8 touch devices are tablets.
  if t.nil? && touch_enabled? &&
     (os_name == 'Windows RT' ||
      (os_name == 'Windows' && os_full_version &&
       Gem::Version.new(os_full_version) >= VersionExtractor::MAJOR_VERSION_8))
    t = 'tablet'
  end

  # All devices running Opera TV Store are assumed to be a tv
  t = 'tv' if opera_tv_store?

  # All devices that contain Andr0id in string are assumed to be a tv
  if user_agent =~ build_regex('Andr0id|(?:Android(?: UHD)?|Google) TV|\(lite\) TV|BRAVIA')
    t = 'tv'
  end

  # All devices running Tizen TV or SmartTV are assumed to be a tv
  t = 'tv' if t.nil? && tizen_samsung_tv?

  # Devices running those clients are assumed to be a TV
  t = 'tv' if ['Kylo', 'Espial TV Browser', 'LUJO TV Browser', 'LogicUI TV Browser',
               'Open TV Browser', 'Seraphic Sraf', 'Opera Devices', 'Crow Browser',
               'Vewd Browser', 'TiviMate', 'Quick Search TV', 'QJY TV Browser',
               'TV Bro'].include?(name)

  # All devices containing TV fragment are assumed to be a tv
  t = 'tv' if t.nil? && user_agent =~ build_regex('\(TV;')

  has_desktop = t != 'desktop' && desktop_string? && desktop_fragment?
  t = 'desktop' if has_desktop

  # set device type to desktop for all devices running a desktop os that were not detected as
  # another device type
  return t if t || !desktop?

  'desktop'
end

#encode_user_agent_if_needed(user_agent) ⇒ Object



41
42
43
44
45
46
# File 'lib/device_detector.rb', line 41

def encode_user_agent_if_needed(user_agent)
  return if user_agent.nil?
  return user_agent if user_agent.encoding.name == 'UTF-8'

  user_agent.encode('utf-8', 'binary', undef: :replace)
end

#full_versionObject



54
55
56
# File 'lib/device_detector.rb', line 54

def full_version
  client_hint.full_version || client.full_version
end

#known?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/device_detector.rb', line 191

def known?
  client.known?
end

#nameObject



48
49
50
51
52
# File 'lib/device_detector.rb', line 48

def name
  return client.name if mobile_fix?

  client_hint.browser_name || client.name
end

#os_familyObject



58
59
60
61
62
# File 'lib/device_detector.rb', line 58

def os_family
  return 'GNU/Linux' if linux_fix?

  client_hint.os_family || os.family || client_hint.platform
end

#os_full_versionObject



70
71
72
73
74
75
76
# File 'lib/device_detector.rb', line 70

def os_full_version
  return if skip_os_version?
  return os.full_version if pico_os_fix?
  return fire_os_version if fire_os_fix?

  client_hint.os_version || os.full_version
end

#os_nameObject



64
65
66
67
68
# File 'lib/device_detector.rb', line 64

def os_name
  return 'GNU/Linux' if linux_fix?

  client_hint.os_name || os.name || client_hint.platform
end