浏览 3623 次
锁定老帖子 主题:ruby中获取浏览器以及系统平台信息
精华帖 (0) :: 良好帖 (2) :: 新手帖 (19) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-04
首先写一个类,用来封装获取系统平台以及浏览器信息的方法
class Info attr_accessor :env, :browser, :platform def initialize(request) self.env = request get_platform get_browser end protected #获取系统平台 def get_platform begin if self.env['HTTP_USER_AGENT'] =~ /Win/i self.platform = "Windows" elsif self.env['HTTP_USER_AGENT'] =~ /Mac/i self.platform = "Mac" elsif self.env['HTTP_USER_AGENT'] =~ /BSD/i self.platform = "FreeBSD"; elsif self.env['HTTP_USER_AGENT'] =~ /Linux/i self.platform = "Linux"; elsif self.env['HTTP_USER_AGENT'] =~ /SunOS/i self.platform = "Sun Solaris"; elsif self.env["HTTP_USER_AGENT"] =~ /iPhone/i self.platform = "iPhone"; else self.platform = nil end rescue self.platform = nil end end #获取浏览器信息 def get_browser self.browser = { :type => nil, :version => nil } begin #Internet Exlorer if self.env['HTTP_USER_AGENT'] =~ /MSIE/i && self.env['HTTP_USER_AGENT'].scan(/AOL|America Online Browser/i).empty? browser[:type] = "MSIE"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/MSIE ([\d\.]+)/i).to_s #Firefox/Firebird/Phoenix elsif self.env['HTTP_USER_AGENT'] =~ /Firefox|Firebird|Phoenix/i browser[:type] = "Firefox"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/[Firefox|Firebird|Phoenix].\/(\d.+)/i).to_s #Galeon elsif self.env['HTTP_USER_AGENT'] =~ /Galeon/i browser[:type] = "Galeon"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Galeon\/([\d\.]+)/i).to_s #Safari elsif self.env['HTTP_USER_AGENT'] =~ /Safari/i browser[:type] = "Safari"; browser[:version] = '0' #Opera elsif self.env['HTTP_USER_AGENT'] =~ /Opera/i browser[:type] = "Opera"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Opera[ |\/]([\d\.]+)/i).to_s #AOL/America Online Browser elsif self.env['HTTP_USER_AGENT'] =~ /AOL|America Online Browser/i browser[:type] = "AOL" browser[:version] = if self.env['HTTP_USER_AGENT'] =~ /AOL/i self.env['HTTP_USER_AGENT'].scan(/AOL[ |\/]([\d.]+)/i).uniq.to_s else self.env['HTTP_USER_AGENT'].scan(/America Online Browser ([\d\.]+)/i).to_s end #Camino elsif self.env['HTTP_USER_AGENT'] =~ /Camino/i browser[:type] = "Camino"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Camino\/([\d\.]+)/i).to_s #Konqueror elsif self.env['HTTP_USER_AGENT'] =~ /Konqueror/i browser[:type] = "Konqueror"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Konqueror\/([\d.]+)/i).to_s #K-Meleon elsif self.env['HTTP_USER_AGENT'] =~ /K-Meleon/i browser[:type] = "K-Meleon"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/K-Meleon\/([\d.]+)/i).to_s #Firefox BonEcho elsif self.env['HTTP_USER_AGENT'] =~ /BonEcho/i browser[:type] = "Firefox BonEcho"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/BonEcho\/([\d.]+)/i).to_s #Netscape elsif self.env['HTTP_USER_AGENT'] =~ /Netscape/i browser[:type] = "Netscape"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Netscape\/([\d.]+)/i).to_s #PSP elsif self.env['HTTP_USER_AGENT'] =~ /PlayStation Portable/i browser[:type] = "PlayStation Portable (PSP)"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/PlayStation Portable\); ([\d\.]+)/i).to_s #PlayStation 3 elsif self.env['HTTP_USER_AGENT'] =~ /PlayStation 3/i browser[:type] = "PlayStation 3"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/PlayStation 3; ([\d\.]+)/i).to_s #Lynx elsif self.env['HTTP_USER_AGENT'] =~ /Lynx/i browser[:type] = "Lynx"; browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Lynx\/([\d\.]+)/i).to_s else browser[:type] = nil browser[:version] = nil end rescue browser[:type] = nil browser[:version] = nil end end end
然后在application.rb中定义三个方法,分别用来获取浏览器的类型、版本以及系统平台
def which_browser get_data return session[:info][:browser_type] end def which_browser_version get_data return session[:info][:browser_version] end def which_platform get_data return session[:info][:platform] end protected def get_data if session[:info].blank? info = Info.new(request.env) session[:info] = {} session[:info][:browser_type] = info.browser[:type] session[:info][:browser_version] = info.browser[:version] session[:info][:platform] = info.platform end end
以下是具体调用方法:
def info which_browser #=> MSIE which_browser_version #=> 7.0 which_platform #=> Windows end
可以很容易的扩展Info类,从而增加新的功能。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-12-05
最后修改:2009-12-05
试试看, 正需要
|
|
返回顶楼 | |
发表时间:2009-12-07
PHP下用 browscap.ini, ruby下有没有对应类库阿?
|
|
返回顶楼 | |
发表时间:2009-12-15
一堆的elsif,还有不厌其烦地重复self.env['HTTP_USER_AGENT']...您这是在虐待键盘吧?
|
|
返回顶楼 | |