agent_orange 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,10 +20,12 @@ user_agent_strings = [
20
20
  def process_user_agent(ua_str)
21
21
  puts
22
22
  puts '='*120
23
- puts "User-Agent:"
23
+ puts "Original User Agent:"
24
24
  puts " #{ua_str}"
25
25
  puts
26
26
  ua = AgentOrange::UserAgent.new(:user_agent => ua_str)
27
+ puts "Parsed Object String:"
28
+ puts " #{ua}"
27
29
  puts '='*120
28
30
  end
29
31
 
@@ -1,5 +1,6 @@
1
1
  require 'agent_orange/browser'
2
2
  require 'agent_orange/version'
3
+ require 'pp'
3
4
 
4
5
  module AgentOrange
5
6
  class Engine
@@ -9,7 +10,9 @@ module AgentOrange
9
10
  ENGINES = {
10
11
  :gecko => 'Gecko',
11
12
  :presto => 'Presto',
12
- :webkit => 'AppleWebKit'
13
+ :trident => 'Trident',
14
+ :webkit => 'AppleWebKit',
15
+ :other => 'Other'
13
16
  }
14
17
 
15
18
  def initialize(user_agent)
@@ -18,52 +21,84 @@ module AgentOrange
18
21
 
19
22
  def parse(user_agent)
20
23
  AgentOrange.debug "ENGINE PARSING", 2
21
- groups = user_agent.scan(/([^\/[:space:]]*)(\/([^[:space:]]*))?([[:space:]]*\[[a-zA-Z][a-zA-Z]\])?[[:space:]]*(\((([^()]|(\([^()]*\)))*)\))?[[:space:]]*/i)
22
- groups.each_with_index do |pieces,i|
23
- name = pieces[0]
24
- version = pieces[2]
25
- comment = pieces[5]
26
-
27
- if name =~ /(#{ENGINES.collect{|cat,regex| regex}.join(')|(')})/i
28
- # Found the engine
29
- AgentOrange.debug " Got an engine in group #{i+1}!", 2
30
- AgentOrange.debug " Raw Name : #{name}", 2
31
- AgentOrange.debug " Raw Version: #{version}", 2
32
- AgentOrange.debug " Raw Comment: #{comment}", 2
33
- AgentOrange.debug "", 2
34
-
35
- # Determine engine type
36
- if name =~ /(#{ENGINES[:gecko]})/i
37
- self.type = "gecko"
24
+ results = user_agent.scan(/([^\/[:space:]]*)(\/([^[:space:]]*))?([[:space:]]*\[[a-zA-Z][a-zA-Z]\])?[[:space:]]*(\((([^()]|(\([^()]*\)))*)\))?[[:space:]]*/i)
25
+ groups = scan_results_into_groups(results)
26
+ groups.each_with_index do |content,i|
27
+ if content[:name] =~ /(#{ENGINES.collect{|cat,regex| regex}.join(')|(')})/i
28
+ # Matched group against name
29
+ self.populate(content)
30
+ elsif content[:comment] =~ /(#{ENGINES.collect{|cat,regex| regex}.join(')|(')})/i
31
+ # Matched group against comment
32
+ chosen_content = { :name => nil, :version => nil }
33
+ additional_groups = parse_comment(content[:comment])
34
+ additional_groups.each do |additional_content|
35
+ if additional_content[:name] =~ /(#{ENGINES.collect{|cat,regex| regex}.join(')|(')})/i
36
+ chosen_content = additional_content
37
+ end
38
38
  end
39
- if name =~ /(#{ENGINES[:presto]})/i
40
- self.type = "presto"
41
- end
42
- if name =~ /(#{ENGINES[:webkit]})/i
43
- self.type = "webkit"
44
- end
45
-
46
- # Determine engine name
47
- self.name = name
48
-
49
- # Determine device version
50
- self.version = AgentOrange::Version.new(version)
51
-
39
+
40
+ self.populate(chosen_content)
52
41
  end
53
-
54
42
  end
55
43
 
44
+ self.analysis
45
+ self.browser = AgentOrange::Browser.new(user_agent)
46
+ end
47
+
48
+ def scan_results_into_groups(results)
49
+ groups = []
50
+ results.each do |result|
51
+ if result[0] != "" # Add the group of content if name isn't blank
52
+ groups << { :name => result[0], :version => result[2], :comment => result[5] }
53
+ end
54
+ end
55
+ groups
56
+ end
57
+
58
+ def parse_comment(comment)
59
+ groups = []
60
+ comment.split('; ').each do |piece|
61
+ content = { :name => nil, :version => nil }
62
+ if piece =~ /(.+)[ \/]([\w.]+)$/i
63
+ chopped = piece.scan(/(.+)[ \/]([\w.]+)$/i)[0]
64
+ groups << { :name => chopped[0], :version => chopped[1] }
65
+ end
66
+ end
67
+ groups
68
+ end
69
+
70
+ def determine_type(content="")
71
+ # Determine type
72
+ type = nil
73
+ ENGINES.each do |key, value|
74
+ type = key if content =~ /(#{value})/i
75
+ end
76
+ type = "other" if type.nil?
77
+ type
78
+ end
79
+
80
+ def populate(content={})
81
+ AgentOrange.debug " Raw Name : #{content[:name]}", 2
82
+ AgentOrange.debug " Raw Version: #{content[:version]}", 2
83
+ AgentOrange.debug " Raw Comment: #{content[:comment]}", 2
84
+ AgentOrange.debug "", 2
85
+
86
+ self.type = self.determine_type(content[:name])
87
+ self.name = ENGINES[self.type.to_sym]
88
+ self.version = AgentOrange::Version.new(content[:version])
89
+ self
90
+ end
91
+
92
+ def analysis
56
93
  AgentOrange.debug "ENGINE ANALYSIS", 2
57
94
  AgentOrange.debug " Type: #{self.type}", 2
58
95
  AgentOrange.debug " Name: #{self.name}", 2
59
96
  AgentOrange.debug " Version: #{self.version}", 2
60
97
  AgentOrange.debug "", 2
61
-
62
- self.browser = AgentOrange::Browser.new(user_agent)
63
98
  end
64
99
 
65
100
  def to_s
66
- [self.name, self.version].compact.join(' ')
101
+ [self.name, self.version].compact.join(' ') || "Unknown"
67
102
  end
68
103
  end
69
104
  end
@@ -1,8 +1,8 @@
1
1
  require 'agent_orange/device'
2
2
 
3
3
  module AgentOrange
4
- DEBUG = true
5
- DEBUG_LEVEL = 2
4
+ DEBUG = false
5
+ DEBUG_LEVEL = 1
6
6
 
7
7
  class UserAgent
8
8
  attr_accessor :user_agent_string
@@ -26,9 +26,6 @@ module AgentOrange
26
26
  AgentOrange.debug "Browser = #{self.device.engine.browser}"
27
27
 
28
28
  self.summary
29
-
30
- AgentOrange.debug "", 2
31
- AgentOrange.debug "user_agent.to_s = #{self}", 2
32
29
  end
33
30
 
34
31
  def is_computer?(type=nil)
@@ -44,7 +41,12 @@ module AgentOrange
44
41
  end
45
42
 
46
43
  def to_s
47
- [self.device, self.device.platform, self.device.operating_system, self.device.engine, self.device.engine.browser].compact.join(", ")
44
+ [self.device,
45
+ self.device.platform,
46
+ self.device.operating_system,
47
+ self.device.engine,
48
+ self.device.engine.browser
49
+ ].compact.join(", ")
48
50
  end
49
51
 
50
52
  def to_human_string
@@ -1,5 +1,5 @@
1
1
  module AgentOrange
2
- VERSION = "0.0.5" # This is for the gem and does not conflict with the rest of the functionality
2
+ VERSION = "0.0.6" # This is for the gem and does not conflict with the rest of the functionality
3
3
 
4
4
  class Version
5
5
  attr_accessor :major, :minor, :patch_level, :build_number
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agent_orange
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kevin Elliott
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-11 00:00:00 Z
18
+ date: 2011-09-12 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Parse and process User Agents like a secret one