Class: BrowserDetector

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

Overview

Lib used to determine browser version in Rails app

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ BrowserDetector

Returns a new instance of BrowserDetector.



4
5
6
7
8
# File 'lib/browser_detector.rb', line 4

def initialize(request)
  @browser_data = request
  @user_agent = @browser_data.env['HTTP_USER_AGENT'] ?
    @browser_data.env['HTTP_USER_AGENT'].downcase : ''
end

Instance Method Details

#is_ie6Object



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

def is_ie6
  return true if self.name == :ie && self.main_version == 6
  return false
end

#is_ie7Object



75
76
77
78
# File 'lib/browser_detector.rb', line 75

def is_ie7
  return true if self.name == :ie && self.main_version == 7
  return false
end

#is_ie8Object



80
81
82
83
# File 'lib/browser_detector.rb', line 80

def is_ie8
    return true if self.name == :ie && self.main_version >= 8
    return false
end

#is_old_ieObject



85
86
87
# File 'lib/browser_detector.rb', line 85

def is_old_ie
  true if is_ie6 || is_ie7
end

#main_versionObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/browser_detector.rb', line 46

def main_version
  agent = @user_agent

  begin
    if agent =~ /msie/i
      agent.split(';').second.split(' ').second.split('.').first.to_i
    elsif agent =~ /konqueror/i
      agent.split('konqueror/').second.split(' ').first.split('.').first.to_i
    elsif agent =~ /chrome/i
      agent.split('chrome/').second.split(' ').first.split('.').first.to_i
    elsif agent =~ /gecko/i
      agent.split('firefox/').second.split(' ').first.split('.').first.to_i
    elsif agent =~ /opera/i
      agent.split('opera/').second.split(' ').first.split('.').first.to_i
    elsif agent =~ /applewebkit/i
      agent.split('safari/').second.split(' ').first.split('.').first.to_i
    else
      "Unknown"
    end
  rescue
    return "Unknown"
  end
end

#nameObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/browser_detector.rb', line 10

def name
  agent = @user_agent
  if agent =~ /msie/i; return :ie
  elsif agent =~ /konqueror/i; return :konqueror
  elsif agent =~ /chrome/i; return :chrome
  elsif agent =~ /gecko/i; return :mozilla
  elsif agent =~ /opera/i; return :opera
  elsif agent =~ /applewebkit/i; return :safari
  else; return :else
  end
end

#versionObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/browser_detector.rb', line 22

def version
  agent = @user_agent
  begin

    if agent =~ /msie/i
      agent.split(';').second.split(' ').second
    elsif agent =~ /konqueror/i
      agent.split('konqueror/').second.split(' ').first
    elsif agent =~ /chrome/i
      agent.split('chrome/').second.split(' ').first
    elsif agent =~ /gecko/i
      agent.split('firefox/').second.split(' ').first
    elsif agent =~ /opera/i
      agent.split('opera/').second.split(' ').first
    elsif agent =~ /applewebkit/i
      agent.split('safari/').second.split(' ').first
    else
      "Unknown"
    end
  rescue
    return "Unknown"
  end
end