Class: UserAgent

Inherits:
Object show all
Defined in:
lib/sinatra/support/useragenthelpers.rb

Constant Summary collapse

UA_REGEXP =
%r{([^ /]+)/([^ ]+)(?: \(([^)]+)\))?}

Instance Method Summary collapse

Constructor Details

#initialize(ua) ⇒ UserAgent

Returns a new instance of UserAgent.



37
38
39
40
41
42
43
# File 'lib/sinatra/support/useragenthelpers.rb', line 37

def initialize(ua)
  @ua_string = ua
  @ua = ua.scan(UA_REGEXP).map { |r|
    r[2] = r[2].split(';').map { |s| s.strip }  unless r[2].nil?
    { :product => r[0], :version => r[1], :details => r[2] }
  }
end

Instance Method Details

#android1?Boolean

Returns:

  • (Boolean)


59
# File 'lib/sinatra/support/useragenthelpers.rb', line 59

def android1?()  detail?(/^Android 1\./, 'Mozilla'); end

#android2?Boolean

Returns:

  • (Boolean)


60
# File 'lib/sinatra/support/useragenthelpers.rb', line 60

def android2?()  detail?(/^Android 2\./, 'Mozilla'); end

#android?Boolean

Returns:

  • (Boolean)


58
# File 'lib/sinatra/support/useragenthelpers.rb', line 58

def android?()   detail?(/^Android/, 'Mozilla'); end

#blackberry?Boolean

Returns:

  • (Boolean)


66
# File 'lib/sinatra/support/useragenthelpers.rb', line 66

def blackberry?() detail?(/^Blackberry/, 'Mozilla'); end

#body_classObject

Returns the list of applicable browser features.

Examples

<body class="<%= browser.body_class %>">

This can return one of the following:

<body class="ios webkit ipad">
<body class="chrome linux webkit">
<body class="gecko linux">
<body class="windows ie ie6">


102
103
104
105
106
107
108
109
# File 'lib/sinatra/support/useragenthelpers.rb', line 102

def body_class
  (%w(webkit chrome safari ios gecko opera ie linux) +
   %w(blackberry nokia android iphone) +
   %w(gecko36 gecko35 gecko30 gecko20 gecko_old) +
   %w(ipad windows osx mac ie6 ie7 ie8 ie9)).map do |aspect|
    aspect  if self.send :"#{aspect}?"
  end.compact.join(' ')
end

#chrome?Boolean

Returns:

  • (Boolean)


47
# File 'lib/sinatra/support/useragenthelpers.rb', line 47

def chrome?()    product?('Chrome'); end

#gecko20?Boolean

Returns:

  • (Boolean)


79
# File 'lib/sinatra/support/useragenthelpers.rb', line 79

def gecko20?()   gecko_version?(20061010, 20080610); end

#gecko30?Boolean

Returns:

  • (Boolean)


78
# File 'lib/sinatra/support/useragenthelpers.rb', line 78

def gecko30?()   gecko_version?(20080610, 20090612); end

#gecko35?Boolean

Returns:

  • (Boolean)


77
# File 'lib/sinatra/support/useragenthelpers.rb', line 77

def gecko35?()   gecko_version?(20090612, 20091111); end

#gecko36?Boolean

Returns:

  • (Boolean)


76
# File 'lib/sinatra/support/useragenthelpers.rb', line 76

def gecko36?()   gecko_version?(20091111, 20100401); end

#gecko40?Boolean

Different Firefox versions

Returns:

  • (Boolean)


75
# File 'lib/sinatra/support/useragenthelpers.rb', line 75

def gecko40?()   gecko_version?(20100401, 20110101); end

#gecko?Boolean

Returns:

  • (Boolean)


50
# File 'lib/sinatra/support/useragenthelpers.rb', line 50

def gecko?()     product?('Gecko'); end

#gecko_old?Boolean

Returns:

  • (Boolean)


80
# File 'lib/sinatra/support/useragenthelpers.rb', line 80

def gecko_old?() gecko_version?(0, 20061010); end

#gecko_version?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/sinatra/support/useragenthelpers.rb', line 82

def gecko_version?(from, to)
  g = product('Gecko')
  return nil  if g.nil?
  v = g[:version][0...8].to_i
  (v >= from) and (v < to)
end

#ie6?Boolean

Returns:

  • (Boolean)


72
# File 'lib/sinatra/support/useragenthelpers.rb', line 72

def ie6?()       detail?(/^MSIE 6/, 'Mozilla'); end

#ie7?Boolean

Returns:

  • (Boolean)


71
# File 'lib/sinatra/support/useragenthelpers.rb', line 71

def ie7?()       detail?(/^MSIE 7.0/, 'Mozilla'); end

#ie8?Boolean

Returns:

  • (Boolean)


70
# File 'lib/sinatra/support/useragenthelpers.rb', line 70

def ie8?()       detail?('MSIE 8.0', 'Mozilla'); end

#ie9?Boolean

IE

Returns:

  • (Boolean)


69
# File 'lib/sinatra/support/useragenthelpers.rb', line 69

def ie9?()       detail?('MSIE 9.0', 'Mozilla'); end

#ie?Boolean

Returns:

  • (Boolean)


52
# File 'lib/sinatra/support/useragenthelpers.rb', line 52

def ie?()        detail?(/^MSIE/, 'Mozilla'); end

#ios?Boolean

Returns:

  • (Boolean)


49
# File 'lib/sinatra/support/useragenthelpers.rb', line 49

def ios?()       product?('Safari') && product?('Mobile'); end

#ipad?Boolean

Returns:

  • (Boolean)


62
# File 'lib/sinatra/support/useragenthelpers.rb', line 62

def ipad?()      detail?(/^iPad/, 'Mozilla'); end

#iphone?Boolean

Returns:

  • (Boolean)


56
# File 'lib/sinatra/support/useragenthelpers.rb', line 56

def iphone?()    detail?(/^iPhone/, 'Mozilla'); end

#linux?Boolean

OS’s and devices

Returns:

  • (Boolean)


55
# File 'lib/sinatra/support/useragenthelpers.rb', line 55

def linux?()     detail?(/^Linux/, 'Mozilla'); end

#mac?Boolean

Returns:

  • (Boolean)


65
# File 'lib/sinatra/support/useragenthelpers.rb', line 65

def mac?()       detail?(/^Macintosh/, 'Mozilla') || osx?; end

#nokia?Boolean

Returns:

  • (Boolean)


61
# File 'lib/sinatra/support/useragenthelpers.rb', line 61

def nokia?()     detail?(/^Nokia/, 'Mozilla') || detail?(/^Series[38]0/, 'Mozilla'); end

#opera?Boolean

Returns:

  • (Boolean)


51
# File 'lib/sinatra/support/useragenthelpers.rb', line 51

def opera?()     product?('Opera'); end

#osx?Boolean

Returns:

  • (Boolean)


64
# File 'lib/sinatra/support/useragenthelpers.rb', line 64

def osx?()       detail?(/^(Intel )?Mac OS X/, 'Mozilla'); end

#rim?Boolean

Returns:

  • (Boolean)


57
# File 'lib/sinatra/support/useragenthelpers.rb', line 57

def rim?()       detail?(/^Blackberry/, 'Mozilla'); end

#safari?Boolean

Returns:

  • (Boolean)


48
# File 'lib/sinatra/support/useragenthelpers.rb', line 48

def safari?()    product?('Safari') && !chrome?; end

#webkit?Boolean

Browsers

Returns:

  • (Boolean)


46
# File 'lib/sinatra/support/useragenthelpers.rb', line 46

def webkit?()    product?('AppleWebKit'); end

#windows?Boolean

Returns:

  • (Boolean)


63
# File 'lib/sinatra/support/useragenthelpers.rb', line 63

def windows?()   detail?(/^Windows/, 'Mozilla'); end