Module: WhichBrowser

Defined in:
lib/which_browser.rb,
lib/which_browser/version.rb

Constant Summary collapse

VERSION =
"0.2.3"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/which_browser.rb', line 96

def method_missing(method, *args, &block)
  if matches = method.to_s.match(/^(ie|safari|ios|ff|firefox|chrome|opera)\_version$/)

    get_browser_version(matches[1])

  elsif matches = method.to_s.match(/^(ie|safari|ff|firefox|chrome|opera)(_[l|g]te?)?([0-9]+)\?$/)

    browser_v = self.send(matches[1] + "_version")
    
    # method really is missing
    super if browser_v == nil
    
    # browser version not found
    return false unless browser_v        
    
    operator = matches[2].to_s.sub('_', '')
    version  = matches[3].to_f
    
    case operator
      when 'lt'
        browser_v < version
      when 'gt'
        browser_v > version            
      when 'lte'
        browser_v <= version
      when 'gte'
        browser_v >= version
      else
        test_user_agent Regexp.new("MSIE\s#{version.to_i}")
    end
  
  else
    super
  end 
end

Instance Method Details

#blackberry?Boolean

Blackberry

Returns:

  • (Boolean)


45
46
47
# File 'lib/which_browser.rb', line 45

def blackberry?
  test_user_agent(/BlackBerry/)
end

#chrome?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/which_browser.rb', line 66

def chrome?
  test_user_agent(/Chrome/)
end

#firefox?Boolean Also known as: ff?

Returns:

  • (Boolean)


61
62
63
# File 'lib/which_browser.rb', line 61

def firefox?
  test_user_agent(/Firefox/)
end

#ie?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/which_browser.rb', line 74

def ie?
  test_user_agent(/MSIE/)
end

#ipad?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/which_browser.rb', line 38

def ipad?
  mobile_safari? && test_user_agent(/iPad/)
end

#iphone?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/which_browser.rb', line 30

def iphone?
  mobile_safari? && test_user_agent(/iPhone/)
end

#ipod?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/which_browser.rb', line 34

def ipod?
  mobile_safari? && test_user_agent(/iPod/)
end

#linux?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/which_browser.rb', line 14

def linux?
   test_user_agent(/Linux/)
end

#mac?Boolean

OS Methods

Returns:

  • (Boolean)


6
7
8
# File 'lib/which_browser.rb', line 6

def mac?
   test_user_agent(/Macintosh/)
end

#mobile?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/which_browser.rb', line 18

def mobile?
   test_user_agent(/Mobile|webOS|BlackBerry|Nokia|Symbian/)
end

#mobile_safari?Boolean

iOS Methods

Returns:

  • (Boolean)


26
27
28
# File 'lib/which_browser.rb', line 26

def mobile_safari?
  test_user_agent(/Mobile\/.+Safari/)
end

#old_ie?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/which_browser.rb', line 78

def old_ie?
  test_user_agent(/MSIE\s[4567]/)
end

#opera?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/which_browser.rb', line 70

def opera?
  test_user_agent(/Opera/)
end

#pc?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/which_browser.rb', line 10

def pc?
   test_user_agent(/Windows/)
end

#safari?Boolean

Browsers

Returns:

  • (Boolean)


57
58
59
# File 'lib/which_browser.rb', line 57

def safari?
  test_user_agent(/Safari/) && !mobile_safari?
end

#symbian?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/which_browser.rb', line 49

def symbian?
  test_user_agent(/Nokia|Symbian/)
end

#test_user_agent(regex) ⇒ Object



87
88
89
# File 'lib/which_browser.rb', line 87

def test_user_agent(regex)
  self.user_agent && self.user_agent.match(regex) != nil
end