Class: Guilded::BrowserDetector

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

Overview

The BrowserDetector provides the ability to determine browser information from the user agent string.

Class Method Summary collapse

Class Method Details

.all_browsersObject



115
116
117
# File 'lib/guilded/browser_detector.rb', line 115

def self.all_browsers
  %W( ie7, ie6, opera, firefox, netscape, konqueror, safari )
end

.all_mobile_browsersObject



119
120
121
# File 'lib/guilded/browser_detector.rb', line 119

def self.all_mobile_browsers
  %w( ie4_ce )
end

.browser_full_name(request) ⇒ Object

Returns the browser name concatenated with the browser version. for example, ‘ie7’.

Request

  • request - The request object.



73
74
75
# File 'lib/guilded/browser_detector.rb', line 73

def self.browser_full_name( request )
  browser_name( request ) + browser_version( request )
end

.browser_is?(request, options = {}) ⇒ Boolean

Returns true if the browser matches the options ent in, otherwise returns false.

Request

  • request - The request object.

Options

  • :name - The name of the browser. For example ‘ie’.

  • :version - The version of the browser. For example ‘7’.

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
# File 'lib/guilded/browser_detector.rb', line 17

def self.browser_is?( request, options={} )
  #name = name.to_s.strip
  name = options[:name].to_s.strip
  version = options[:version].to_s.strip

  return true if browser_name( request ) == name
  return true if name == 'mozilla' && browser_name( request ) == 'gecko'
  return true if name == 'ie' && browser_name( request ).index( 'ie' )
  return true if name == 'webkit' && browser_name( request ) == 'safari'
end

.browser_name(request) ⇒ Object

Returns the name of the browser that is making this request. For example ‘ie’.

Request

  • request - The request object.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/guilded/browser_detector.rb', line 33

def self.browser_name( request )
  @browser_name ||= begin
    ua = request.env['HTTP_USER_AGENT']
    if ua.nil?
      'unknown'
    else
      ua = ua.downcase  

      if ua.index( 'msie' ) && !ua.index( 'opera' ) && !ua.index( 'webtv' )
        if ua.index( 'windows ce' )
          'ie' + ua[ua.index( 'msie' ) + 5].chr + '_ce'
        else
          'ie' + ua[ua.index( 'msie' ) + 5].chr
        end
      elsif ua.index( 'netscape' )
        'netscape'
      elsif ua.index( 'gecko/' ) 
        'firefox'
      elsif ua.index( 'opera' )
        'opera'
      elsif ua.index( 'konqueror' ) 
        'konqueror'
      elsif ua.index( 'applewebkit/' )
        'safari'
      elsif ua.index( 'mozilla/' )
        'firefox'
      elsif ua.index( 'firefox' )
        'firefox'
      else
        'unknown'
      end
    end
  end
end

.browser_version(request) ⇒ Object

Returns the version of the browser that is making this request. For example ‘7’.

Request

  • request - The request object.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/guilded/browser_detector.rb', line 82

def self.browser_version( request )
  @browser_version ||= begin
    ua = request.env['HTTP_USER_AGENT'].downcase

    if browser_name( request ) == 'opera'
      ua[ua.index( 'opera' ) + 6].chr
    elsif browser_name( request ) == 'firefox'
      ua[ua.index( 'firefox' ) + 8 ].chr
    elsif browser_name( request ) == 'netscape'
      ua[ua.index( 'netscape' ) + 9].chr
    elsif browser_name( request ).index( 'ie' )
      ua[ua.index( 'msie' ) + 5].chr
    else
      'unknown'
    end

  end
end

.can_use_png?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/guilded/browser_detector.rb', line 101

def self.can_use_png?
   if browser_name.index( 'ie' ) == 0
	  if browser_version.to_i < 7 
			false
		else
			true
		end
	#elsif browser_name == 'firefox'
	  #	false
	else
		true
	end
end