Class: DeviceDetector
- Inherits:
-
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, = nil)
@client_hint = ClientHint.new()
utf8_user_agent = encode_user_agent_if_needed(user_agent)
@user_agent = build_user_agent(utf8_user_agent)
end
|
Instance Attribute Details
#client_hint ⇒ Object
Returns the value of attribute client_hint.
21
22
23
|
# File 'lib/device_detector.rb', line 21
def client_hint
@client_hint
end
|
#user_agent ⇒ Object
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
.cache ⇒ Object
218
219
220
|
# File 'lib/device_detector.rb', line 218
def cache
@cache ||= MemoryCache.new(config.to_hash)
end
|
.config ⇒ Object
214
215
216
|
# File 'lib/device_detector.rb', line 214
def config
@config ||= Configuration.new
end
|
222
223
224
225
|
# File 'lib/device_detector.rb', line 222
def configure
@config = Configuration.new
yield(config)
end
|
Instance Method Details
#bot? ⇒ Boolean
195
196
197
|
# File 'lib/device_detector.rb', line 195
def bot?
bot.bot?
end
|
#bot_name ⇒ Object
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_brand ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/device_detector.rb', line 84
def device_brand
return if fake_ua?
brand = device.brand
brand = 'Apple' if brand.nil? && DeviceDetector::OS::APPLE_OS_NAMES.include?(os_name)
brand
end
|
#device_name ⇒ Object
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_type ⇒ Object
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?
if t.nil? && os_family == 'Android' && user_agent =~ build_regex('Chrome\/[\.0-9]*')
t = user_agent =~ build_regex('(?:Mobile|eliboM)') ? 'smartphone' : 'tablet'
end
t = 'tablet' if t == 'smartphone' && user_agent =~ build_regex('Pad\/APad')
t = 'tablet' if t.nil? && (android_tablet_fragment? || opera_tablet?)
t = 'smartphone' if t.nil? && android_mobile_fragment?
t = 'wearable' if t.nil? && android_vr_fragment?
if t.nil? && os_name == 'Android' && os.full_version && !os.full_version.empty?
full_version = Gem::Version.new(os.full_version)
if full_version < ::MAJOR_VERSION_2
t = 'smartphone'
elsif full_version >= ::MAJOR_VERSION_3 && \
full_version < ::MAJOR_VERSION_4
t = 'tablet'
end
end
t = 'smartphone' if t == 'feature phone' && os_family == 'Android'
t = 'feature phone' if t.nil? && os_name == 'Java ME'
if t.nil? && touch_enabled? &&
(os_name == 'Windows RT' ||
(os_name == 'Windows' && os_full_version &&
Gem::Version.new(os_full_version) >= ::MAJOR_VERSION_8))
t = 'tablet'
end
t = 'tv' if opera_tv_store?
if user_agent =~ build_regex('Andr0id|(?:Android(?: UHD)?|Google) TV|\(lite\) TV|BRAVIA')
t = 'tv'
end
t = 'tv' if t.nil? && tizen_samsung_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)
t = 'tv' if t.nil? && user_agent =~ build_regex('\(TV;')
has_desktop = t != 'desktop' && desktop_string? && desktop_fragment?
t = 'desktop' if has_desktop
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_version ⇒ Object
54
55
56
|
# File 'lib/device_detector.rb', line 54
def full_version
client_hint.full_version || client.full_version
end
|
#known? ⇒ Boolean
191
192
193
|
# File 'lib/device_detector.rb', line 191
def known?
client.known?
end
|
#name ⇒ Object
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_family ⇒ Object
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_version ⇒ Object
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_name ⇒ Object
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
|