DeviceDetector是一个用Ruby编写的用来解析各种设备User-Agent信息的库,基于及时更新和庞大的user-agent库。使用DeviceDetector可探测设备浏览器、操作系统、设备类型(桌面、平板、移动、电视、汽车、控制台等)、品牌和型号。
示例代码:
user_agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36'client = DeviceDetector.new(user_agent)client.name # => 'Chrome'client.full_version # => '30.0.1599.69'client.os_name # => 'Windows'client.os_full_version # => '8'# For many devices, you can also query the device name (usually the model name)client.device_name # => 'iPhone 5'# Device types can be one of the following: smartphone, tablet, console, # portable media player, tv, car browser, cameraclient.device_type # => 'smartphone'为了提升性能,DeviceDetector可以把UserAgent信息放到缓存中:
DeviceDetector.configure do |config| config.max_cache_keys = 5_000 # increment this if you have enough RAM, proceed with careend
评论