Class: AgentOrange::Base

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

Direct Known Subclasses

Browser, Device, Engine, OperatingSystem, Platform

Instance Method Summary collapse

Constructor Details

#initialize(user_agent) ⇒ Base

Returns a new instance of Base.



4
5
6
# File 'lib/agent_orange/base.rb', line 4

def initialize(user_agent)
  parse(user_agent)
end

Instance Method Details

#analysisObject



64
65
66
67
68
# File 'lib/agent_orange/base.rb', line 64

def analysis
  AgentOrange.debug self.class.name.upcase + ' ANALYSIS', 2
  debug_content(:type => type, :name => name, :version => version)
  AgentOrange.debug '', 2
end

#debug_content(content) ⇒ Object



58
59
60
61
62
# File 'lib/agent_orange/base.rb', line 58

def debug_content(content)
  AgentOrange.debug "  Type: #{self.type}", 2
  AgentOrange.debug "  Name: #{self.name}", 2
  AgentOrange.debug "  Version: #{self.version}", 2
end

#debug_raw_content(content) ⇒ Object



52
53
54
55
56
# File 'lib/agent_orange/base.rb', line 52

def debug_raw_content(content)
  AgentOrange.debug "  Raw Name   : #{content[:name]}", 2
  AgentOrange.debug "  Raw Version: #{content[:version]}", 2
  AgentOrange.debug "  Raw Comment: #{content[:comment]}", 2
end

#determine_type(types = {}, content = "") ⇒ String

Returns:

  • (String)


42
43
44
45
46
47
48
49
50
# File 'lib/agent_orange/base.rb', line 42

def determine_type(types={}, content="")
  # Determine type
  type = nil
  types.each do |key, value|
    type = key if content =~ /(#{value})/i
  end
  type = "other" if type.nil?
  type
end

#parse(user_agent) ⇒ Object



8
9
10
# File 'lib/agent_orange/base.rb', line 8

def parse(user_agent)
  raise 'Any class that extends AgentOrange::Base must implement parse(user_agent)'
end

#parse_comment(comment) ⇒ Array

Returns:

  • (Array)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/agent_orange/base.rb', line 25

def parse_comment(comment)
  groups = []
  comment.split('; ').each do |piece|
    content = { :name => nil, :version => nil }
    
    # Remove 'like Mac OS X' or similar since it distracts from real results
    piece = piece.scan(/(.+) like .+$/i)[0][0] if piece =~ /(.+) like (.+)$/i
    
    if piece =~ /(.+)[ \/]([\w.]+)$/i
      chopped = piece.scan(/(.+)[ \/]([\w.]+)$/i)[0]
      groups << { :name => chopped[0], :version => chopped[1] }
    end
  end
  groups
end

#parse_user_agent_string_into_groups(user_agent) ⇒ Array

Returns:

  • (Array)


13
14
15
16
17
18
19
20
21
22
# File 'lib/agent_orange/base.rb', line 13

def parse_user_agent_string_into_groups(user_agent)
  results = user_agent.scan(/([^\/[:space:]]*)(\/([^[:space:]]*))?([[:space:]]*\[[a-zA-Z][a-zA-Z]\])?[[:space:]]*(\((([^()]|(\([^()]*\)))*)\))?[[:space:]]*/i)
  groups = []
  results.each do |result|
    if result[0] != "" # Add the group of content if name isn't blank
      groups << { :name => result[0], :version => result[2], :comment => result[5] }
    end
  end
  groups
end