1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 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 190 191 192 193 194
|
require 'yaml'
require 'device_detector/version'
require 'device_detector/metadata_extractor'
require 'device_detector/version_extractor'
require 'device_detector/model_extractor'
require 'device_detector/name_extractor'
require 'device_detector/memory_cache'
require 'device_detector/parser'
require 'device_detector/bot'
require 'device_detector/client'
require 'device_detector/device'
require 'device_detector/os'
class DeviceDetector
attr_reader :user_agent
def initialize(user_agent)
@user_agent = user_agent
end
def name
client.name
end
def full_version
client.full_version
end
def os_name
os.name
end
def os_full_version
os.full_version
end
def device_name
device.name
end
def device_brand
device.brand
end
def device_type
t = device.type
if t.nil? && android_tablet_fragment? || opera_tablet?
t = 'tablet'
end
if t.nil? && android_mobile_fragment?
t = 'smartphone'
end
# 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.short_name == 'AND' && os.full_version && !os.full_version.empty?
if os.full_version < '2'
t = 'smartphone'
elsif os.full_version >= '3' && os.full_version < '4'
t = 'tablet'
end
end
# All detected feature phones running android are more likely a smartphone
if t == 'feature phone' && os.family == 'Android'
t = 'smartphone'
end
# 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.short_name == 'WRT' || (os.short_name == 'WIN' && os.full_version && os.full_version >= '8'))
t = 'tablet'
end
if opera_tv_store?
t = 'tv'
end
if t.nil? && ['Kylo', 'Espial TV Browser'].include?(client.name)
t = 'tv'
end
# set device type to desktop for all devices running a desktop os that were
# not detected as an other device type
if t.nil? && os.desktop? && !puffin_browser?
t = 'desktop'
end
t
end
def known?
client.known?
end
def bot?
bot.bot?
end
def bot_name
bot.name
end
class << self
class Configuration
attr_accessor :max_cache_keys
def to_hash
{
max_cache_keys: max_cache_keys
}
end
end
def config
@config ||= Configuration.new
end
def cache
@cache ||= MemoryCache.new(config.to_hash)
end
def configure(&block)
@config = Configuration.new
yield(config)
end
end
private
def bot
@bot ||= Bot.new(user_agent)
end
def client
@client ||= Client.new(user_agent)
end
def device
@device ||= Device.new(user_agent)
end
def os
@os ||= OS.new(user_agent)
end
def android_tablet_fragment?
user_agent =~ build_regex('Android(?: \d.\d(?:.\d)?)?; Tablet;')
end
def android_mobile_fragment?
user_agent =~ build_regex('Android(?: \d.\d(?:.\d)?)?; Mobile;')
end
def touch_enabled?
user_agent =~ build_regex('Touch')
end
def opera_tv_store?
user_agent =~ build_regex('Opera TV Store')
end
def opera_tablet?
user_agent =~ build_regex('Opera Tablet')
end
# This is a workaround until we support detecting mobile only browsers
def puffin_browser?
client.name == 'Puffin'
end
def build_regex(src)
Regexp.new('(?:^|[^A-Z0-9\_\-])(?:' + src + ')', Regexp::IGNORECASE)
end
end
|