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

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ BrowserDetector

Returns a new instance of BrowserDetector.



8
9
10
# File 'lib/guilded/browser_detector.rb', line 8

def initialize( request )
  @request = request
end

Class Method Details

.all_browsersObject



131
132
133
# File 'lib/guilded/browser_detector.rb', line 131

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

.all_mobile_browsersObject



135
136
137
# File 'lib/guilded/browser_detector.rb', line 135

def self.all_mobile_browsers
  %w( ie_ce4 )
end

Instance Method Details

#browser_full_nameObject

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

Request

  • request - The request object.



82
83
84
# File 'lib/guilded/browser_detector.rb', line 82

def browser_full_name
  browser_name + browser_version
end

#browser_is?(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)


21
22
23
24
25
26
27
28
29
# File 'lib/guilded/browser_detector.rb', line 21

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

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

#browser_nameObject

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

Request

  • request - The request object.



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
67
68
69
70
71
72
73
74
75
# File 'lib/guilded/browser_detector.rb', line 36

def browser_name
  begin
    @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' + '_ce' #+ ua[ua.index( 'msie' ) + 5].chr 
          else
            'ie' # + ua[ua.index( 'msie' ) + 5].chr
          end
        elsif ua.include?( 'chrome' )
          'chrome'
        elsif ua.include?( 'netscape' )
          'netscape'
        elsif ua.include?( 'gecko/' ) 
          'firefox'
        elsif ua.include?( 'opera' )
          'opera'
        elsif ua.include?( 'konqueror' ) 
          'konqueror'
        elsif ua.include?( 'applewebkit/' )
          'safari'
        elsif ua.include?( 'mozilla/' )
          'firefox'
        elsif ua.include?( 'firefox' )
          'firefox'
        else
          'unknown'
        end
      end
    end
  rescue
    'unknown'
  end
end

#browser_versionObject

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

Request

  • request - The request object.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/guilded/browser_detector.rb', line 91

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

      if browser_name == 'opera'
        ua[ua.index( 'opera' ) + 6].chr
      elsif browser_name == 'chrome'
        ua[ua.index( 'chrome' ) + 7].chr
      elsif browser_name == 'firefox'
        ua[ua.index( 'firefox' ) + 8].chr
      elsif browser_name == 'netscape'
        ua[ua.index( 'netscape' ) + 9].chr
      elsif browser_name.index( 'ie' )
        ua[ua.index( 'msie' ) + 5].chr
      elsif browser_name.index( 'safari' )
        ua[ua.index( 'version' ) + 8].chr
      else
        '0'
      end
    end
  rescue
    '0'
  end
end

#can_use_png?Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/guilded/browser_detector.rb', line 117

def 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