User-Agent 解析庫:DeviceDetector
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
本文由用戶 fn67 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!