Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过用户代理(rails)检查设备类型_Ruby On Rails_User Agent - Fatal编程技术网

Ruby on rails 通过用户代理(rails)检查设备类型

Ruby on rails 通过用户代理(rails)检查设备类型,ruby-on-rails,user-agent,Ruby On Rails,User Agent,我想知道我的用户是否正在使用我的rails应用程序浏览页面 药片 移动设备或 台式计算机 我钻研了许多不同的解决方案。以下是我的最爱: ua parser gem:看起来非常干净,但不幸的是,当我使用parsed_string.device时,它总是绘制Other——我可以很好地检测操作系统和浏览器 从头开始写 白手起家的写作结果是这样的: if request.user_agent.downcase.match(/mobile|android|iphone|blackberry|iemob

我想知道我的用户是否正在使用我的rails应用程序浏览页面

  • 药片
  • 移动设备或
  • 台式计算机
我钻研了许多不同的解决方案。以下是我的最爱:

  • ua parser gem:看起来非常干净,但不幸的是,当我使用
    parsed_string.device时,它总是绘制
    Other
    ——我可以很好地检测操作系统和浏览器
  • 从头开始写
  • 白手起家的写作结果是这样的:

    if request.user_agent.downcase.match(/mobile|android|iphone|blackberry|iemobile|kindle/)
      @os = "mobile"
    elsif request.user_agent.downcase.match(/ipad/)
      @os = "tablet"
    elsif request.user_agent.downcase.match(/mac OS|windows/)
      @os = "desktop"
    end
    
    但是,我缺少的是用户代理“设备”定义的完整文档

    例如: 如果我的用户在平板电脑/移动设备或桌面上浏览,我需要查看哪些模式?我不能只是猜测和检查,例如ua解析器正则表达式也帮不了我(非常复杂):

    有什么简单的方法可以解决我的问题吗?
    谷歌分析是如何做到的?我试着研究,但找不到。它们还显示设备(台式机/平板电脑/移动设备)。

    必须这样做,但在添加之前,您仍然可以使用gem通过使用
    browser.device?

    我正在寻找第二个选项,因为我需要它尽可能精简。用户代理字符串中有很多我不需要的信息。我不想要一个函数试图解析它。简单地说就是:机器人、台式机、平板电脑、手机和其他

    这是一个很大的阅读,但我正在寻找关键字使用

    到目前为止,以下关键词似乎对我有效。这是php中的正则表达式,但你会明白的

    //try to find crawlers
    //  https://developers.whatismybrowser.com/useragents/explore/software_type_specific/crawler/
    if (preg_match('/(bot\/|spider|crawler|slurp|pinterest|favicon)/i', $userAgent) === 1)
      return ['type' => 'crawler'];
    
    //try to find tablets
    //  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/tablet/
    //  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/ebook-reader/
    if (preg_match('/(ipad| sm-t| gt-p| gt-n|wt19m-fi|nexus 7| silk\/|kindle| nook )/i', $userAgent) === 1)
      return ['type' => 'tablet'];
    
    //try to find mobiles
    //  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/mobile/
    //  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/phone/
    if (preg_match('/(android|iphone|mobile|opera mini|windows phone|blackberry|netfront)/i', $userAgent) === 1)
      return ['type' => 'mobile'];
    
    //try to find desktops
    //  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/computer/
    if (preg_match('/(windows nt|macintosh|x11; linux|linux x86)/i', $userAgent) === 1)
      return ['type' => 'desktop'];
    
    return ['type' => 'other'];
    

    试试看。它在很多方面都比ua解析器好,看起来很有希望,谢谢