Class: UserAgentInfo::UserAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/user_agent_info.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r) ⇒ UserAgent

:nodoc:



27
28
29
# File 'lib/user_agent_info.rb', line 27

def initialize(r) #:nodoc:
  parse_browser_info(r)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments, &block) ⇒ Object

:nodoc:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/user_agent_info.rb', line 68

def method_missing(method_id, *arguments, &block) #:nodoc:
  actions = ['better', 'worse']
  operators = ['or', 'and']
  
  method = method_id.to_s
  method_name = "#{method.split('_')[0]}?"
  
  operator = method.split('_')[1]      
  action   = method.split('_')[2].chop
 
  return false if !operators.include?(operator)
  return false if !action.include?(action)      
 
  if self.send method_name.to_sym
    if arguments.size != 1
      raise RuntimeError, "Missing 'version' argument."
    end
            
    version = arguments[0].to_f
    case 
    when action.eql?("better") && operator.eql?('or')
      return @browser_info['version'] >= version
    when action.eql?("better") && operator.eql?('and')          
      return @browser_info['version'] > version        
    when action.eql?("worse") && operator.eql?('or')
      return @browser_info['version'] <= version
    when action.eql?("worse") && operator.eql?('and')          
      return @browser_info['version'] < version        
    end                  
  end
  
  return false
end

Instance Attribute Details

#browser_infoObject (readonly)

Returns the value of attribute browser_info.



25
26
27
# File 'lib/user_agent_info.rb', line 25

def browser_info
  @browser_info
end

Instance Method Details

#[](lookup) ⇒ Object

query user_agent_info

request.user_agent_info['header']         ==> Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 
request.user_agent_info['platform']       ==> Macintosh 
request.user_agent_info['name']           ==> Firefox 
request.user_agent_info['name_display']   ==> Mozilla Firefox 
request.user_agent_info['cpuos']          ==> Intel Mac OS X 10.5 
request.user_agent_info['language']       ==> en-US 
request.user_agent_info['version']        ==> 3.5 
request.user_agent_info['engine']         ==> Gecko/20091016


41
42
43
# File 'lib/user_agent_info.rb', line 41

def [](lookup)
  @browser_info[lookup]
end

#isChrome?(version = nil) ⇒ Boolean

Returns:



60
61
62
# File 'lib/user_agent_info.rb', line 60

def isChrome?(version = nil)
  browser_query('Chrome', version)
end

#isFirefox?(version = nil) ⇒ Boolean

Returns:



56
57
58
# File 'lib/user_agent_info.rb', line 56

def isFirefox?(version = nil)
  browser_query('Firefox', version)
end

#isIE?(version = nil) ⇒ Boolean

check if browser is Internet Explorer

request.user_agent_info.isIE?                     ==> true if browser is Internet Explorer <br />
request.user_agent_info.isIE?('6.0')              ==> true if browser is Internet Explorer version 6.0                    '==' 
request.user_agent_info.isIE_or_worse?('6.0')     ==> true if browser is Internet Explorer version worse or equal to 6.0  '<=' 
request.user_agent_info.isIE_and_worse?('6.0')    ==> true if browser is Internet Explorer version worse then 6.0         '<' 
request.user_agent_info.isIE_or_better?('6.0')    ==> true if browser is Internet Explorer version better or equal to 6.0 '>=' 
request.user_agent_info.isIE_and_better?('6.0')   ==> true if browser is Internet Explorer version better then 6.0        '>'

Returns:



52
53
54
# File 'lib/user_agent_info.rb', line 52

def isIE?(version = nil)
  browser_query('MSIE', version)
end

#isOpera?(version = nil) ⇒ Boolean

Returns:



64
65
66
# File 'lib/user_agent_info.rb', line 64

def isOpera?(version = nil)
  browser_query('Opera', version)
end