ruby-on-rails - 找到网络应用客户端的操作系统

标签 ruby-on-rails ruby operating-system client-side

我有一个要求,我必须找到我的网络应用程序用户的操作系统详细信息。如果用户在 Windows 上运行,它应该给出“Windows 7 ... Blah blah”如果是 Linux“Ubuntu .. blah..blah OR Fedora .. ..”如果是 Debian“任何一个”Debian 版本。

谁能建议我们该怎么做?我看了看 sys-uname gem 。但它只让我知道服务器端信息。

最佳答案

你必须使用 request.env['HTTP_USER_AGENT']

我找到了示例代码。

  def getBrowser(bt)
    rs=false
    ua=request.env['HTTP_USER_AGENT'].downcase
    isOpera = ua.index('opera') ? true : false
    isSafari = (ua =~ /webkit|khtml/) ? true : false
    isSafari3 = (ua.index('webkit/5')) ? true : false
    isGecko = (!isSafari and ua.index('gecko')) ? true : false
    isGecko3 = (!isSafari and ua.index('rv:1.9')) ? true : false
    isIE = (!isOpera and ua.index('msie')) ? true : false
    isIE7 = (!isOpera and ua.index('msie 7')) ? true : false
    case bt
      when 0  #isKonqueror
        if ua.index('konqueror') then rs=true end
      when 1  #isOpera
        rs=isOpera
      when 2  #isSafari
        rs=isSafari
      when 3  #isSafari2
        rs=isSafari && !isSafari3
      when 4  #isSafari3
        rs=isSafari3
      when 5  #isIE
        rs=isIE
      when 6  #isIE6
        rs=isIE && !isIE7
      when 7  #isIE7
        rs=isIE7
      when 8  #isGecko
        rs=isGecko
      when 9  #isGecko2
        rs=isGecko && !isGecko3
      when 10 #isGecko3
        rs=isGecko3
      when 11 #isWindows
        if ua.index('windows') or ua.index('win32') then rs=true end
      when 12 #isMac
        if ua.index('macintosh') or ua.index('mac os x') then rs=true 
end
      when 13 #isAir
        if ua.index('adobeair') then rs=true end
      when 14 #isLinux
        if ua.index('linux') then rs=true end
      when 15 #isSecure
        s = request.env['SERVER_PROTOCOL'].downcase
        if s.index('https') then rs=true end
    end
    rs
  end

编辑:创建另一个操作以确定操作系统

def get_operating_system
  if request.env['HTTP_USER_AGENT'].downcase.match(/mac/i)
    "Mac"
  elsif request.env['HTTP_USER_AGENT'].downcase.match(/windows/i)
    "Windows"
  elsif request.env['HTTP_USER_AGENT'].downcase.match(/linux/i)
    "Linux"
  elsif request.env['HTTP_USER_AGENT'].downcase.match(/unix/i)
    "Unix"
  else
    "Unknown"
  end
end

希望对您有所帮助!

关于ruby-on-rails - 找到网络应用客户端的操作系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10990224/

相关文章:

ruby-on-rails - 将 session 从事件记录存储迁移到 cookie 存储

ruby - 如何在 Ruby Shoes 中显示数组的输出?

linux - postgresql:读取器进程消耗高 cpu

ruby-on-rails - Rails STI 和 has_many 通过关联不起作用,SQLException : no such table

ruby - 在 Ruby 中生成 JWT

python - 通过迭代指定的数据帧值来创建新目录 Python Pandas Numpy

c - *尼克斯读()/写(): Are they re-entrant?

ruby-on-rails - Rails 如何验证可选表单字段?

ruby-on-rails - Rails 4:嵌套属性和 PG::NotNullViolation 错误

mysql - 连接到其他域上的 MySQL 数据库