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.1'

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 = set_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



207
208
209
# File 'lib/device_detector.rb', line 207

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

.configObject



203
204
205
# File 'lib/device_detector.rb', line 203

def config
  @config ||= Configuration.new
end

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

Yields:



211
212
213
214
# File 'lib/device_detector.rb', line 211

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

Instance Method Details

#bot?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/device_detector.rb', line 184

def bot?
  bot.bot?
end

#bot_nameObject



188
189
190
# File 'lib/device_detector.rb', line 188

def bot_name
  bot.name
end

#device_brandObject



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

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? && ['Apple TV', 'iOS', 'Mac'].include?(os_name)

  brand
end

#device_nameObject



76
77
78
79
80
# File 'lib/device_detector.rb', line 76

def device_name
  return if fake_ua?

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

#device_typeObject



92
93
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
# File 'lib/device_detector.rb', line 92

def device_type
  t = device.type

  # 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]*')
    if user_agent =~ build_regex('(?:Mobile|eliboM) Safari\/')
      t = 'smartphone'
    elsif user_agent =~ build_regex('(?!Mobile )Safari\/')
      t = 'tablet'
    end
  end

  # 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?

  # 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
  t = 'tv' if user_agent =~ build_regex('Andr0id|Android TV')

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

  # Devices running Kylo or Espital TV Browsers are assumed to be a TV
  t = 'tv' if t.nil? && ['Kylo', 'Espial TV Browser'].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.platform_version || client.full_version
end

#known?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/device_detector.rb', line 180

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
# File 'lib/device_detector.rb', line 70

def os_full_version
  return if skip_os_version?

  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

#set_user_agent(user_agent) ⇒ Object



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

def set_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(regex, "Android #{version}, #{client_hint.model}")
end