Class: AgentOrange::Browser

Inherits:
Base
  • Object
show all
Defined in:
lib/agent_orange/browser.rb

Constant Summary collapse

BROWSERS =
{
  :ie       => 'MSIE|Internet Explorer|IE',
  :firefox  => 'Firefox',
  :opera    => 'Opera',
  :chrome   => 'Chrome',
  :safari   => 'Safari'
}
BROWSER_TITLES =
BROWSERS.merge :ie => 'MSIE'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#analysis, #debug_content, #debug_raw_content, #determine_type, #initialize, #parse_comment, #parse_user_agent_string_into_groups

Constructor Details

This class inherits a constructor from AgentOrange::Base

Instance Attribute Details

#nameString

Returns one of the values from BROWSERS.

Returns:

  • (String)

    one of the values from BROWSERS



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

def name
  @name
end

#securityObject

Returns the value of attribute security.



15
16
17
# File 'lib/agent_orange/browser.rb', line 15

def security
  @security
end

#typeSymbol

Returns one of the keys from BROWSERS.

Returns:

  • (Symbol)

    one of the keys from BROWSERS



7
8
9
# File 'lib/agent_orange/browser.rb', line 7

def type
  @type
end

#versionAgentOrange::Version



13
14
15
# File 'lib/agent_orange/browser.rb', line 13

def version
  @version
end

Instance Method Details

#parse(user_agent) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/agent_orange/browser.rb', line 27

def parse(user_agent)
  AgentOrange.debug "BROWSER PARSING", 2

  groups = parse_user_agent_string_into_groups(user_agent)
  groups.each_with_index do |content,i|
    if content[:name] =~ /(#{BROWSERS.collect{|cat,regex| regex}.join(')|(')})/i
      # Matched group against name
      self.populate(content)
      break
    elsif content[:comment] =~ /(#{BROWSERS.collect{|cat,regex| regex}.join(')|(')})/i
      # Matched group against comment
      chosen_content = { :name => nil, :version => nil }
      additional_groups = parse_comment(content[:comment])
      additional_groups.each do |additional_content|
        if additional_content[:name] =~ /(#{BROWSERS.collect{|cat,regex| regex}.join(')|(')})/i
          chosen_content = additional_content

          #Additional checking for chromeframe, iemobile, etc.
          BROWSERS.each do |cat, regex|
            chosen_content[:name] = BROWSER_TITLES[cat] if additional_content[:name] =~ /(#{regex})/i
          end
        end
      end

      populate(chosen_content)
    end
  end

  analysis
end

#populate(content = {}) ⇒ Browser

Returns:



59
60
61
62
63
64
65
66
67
# File 'lib/agent_orange/browser.rb', line 59

def populate(content={})
  debug_raw_content(content)
  AgentOrange.debug "", 2

  self.type = determine_type(BROWSERS, content[:name])
  self.name = content[:name]
  self.version = AgentOrange::Version.new(content[:version])
  self
end

#to_sString

Returns:

  • (String)


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

def to_s
  [name, version].compact.join(' ')
end