User-Agent 解析库:DeviceDetector
fn67
10年前
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, camera client.device_type # => 'smartphone'
为了提升性能,DeviceDetector 可以把 UserAgent 信息放到缓存中:
DeviceDetector.configure do |config| config.max_cache_keys = 5_000 # increment this if you have enough RAM, proceed with care end