agent_orange 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -36,13 +36,11 @@ If you're going to use it with Rails then add the gem to your Gemfile.
36
36
  === Looking at the device
37
37
 
38
38
  >> device = ua.device
39
- => "iPhone 3G"
39
+ => "Mobile"
40
40
  >> device.type
41
41
  => "mobile"
42
- >> device.manufacturer
43
- => "Apple"
44
42
  >> device.name
45
- => "iPhone"
43
+ => "Mobile"
46
44
  >> device.version
47
45
  => nil
48
46
 
@@ -60,6 +58,28 @@ If you're going to use it with Rails then add the gem to your Gemfile.
60
58
  >> ua.is_mobile?
61
59
  => true
62
60
 
61
+ === Looking at the platform
62
+
63
+ >> platform = ua.device.platform
64
+ => "iPhone"
65
+ >> platform.type
66
+ => "iphone"
67
+ >> platform.name
68
+ => "iPhone"
69
+ >> platform.version
70
+ => "3GS"
71
+
72
+ === Looking at the operating system
73
+
74
+ >> os = ua.device.os
75
+ => "iPhone OS 4.3"
76
+ >> os.type
77
+ => "iphone_os"
78
+ >> os.name
79
+ => "iPhone OS"
80
+ >> os.version
81
+ => "4.3"
82
+
63
83
  === Looking at the web engine
64
84
 
65
85
  >> engine = ua.device.engine
@@ -2,6 +2,21 @@
2
2
 
3
3
  require "agent_orange/user_agent"
4
4
 
5
+ user_agent_strings = [
6
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; sv-se) AppleWebKit/525.26.2 (KHTML, like Gecko) Version/3.2 Safari/525.26.12",
7
+ "Mozilla/5.0 (iPhone; U; Linux i686; pt-br) AppleWebKit/532+ (KHTML, like Gecko) Version/3.0 Mobile/1A538b Safari/419.3 Midori/0.2.0",
8
+ "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_1 like Mac OS X; zh-tw) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5",
9
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1",
10
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1",
11
+ "Mozilla/5.0 (Windows; U; Windows NT 6.1; tr-TR) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27",
12
+ "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",
13
+ "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Zune 4.0; InfoPath.3; MS-RTC LM 8; .NET4.0C; .NET4.0E)",
14
+ "Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0",
15
+ "Mozilla/5.0 (X11; U; Linux i586; de; rv:5.0) Gecko/20100101 Firefox/5.0",
16
+ "Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.00",
17
+ "Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11"
18
+ ]
19
+
5
20
  def process_user_agent(ua_str)
6
21
  puts
7
22
  puts '='*120
@@ -12,8 +27,6 @@ def process_user_agent(ua_str)
12
27
  puts '='*120
13
28
  end
14
29
 
15
- ua_str = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; sv-se) AppleWebKit/525.26.2 (KHTML, like Gecko) Version/3.2 Safari/525.26.12"
16
- process_user_agent(ua_str)
17
-
18
- ua_str = "Mozilla/5.0 (iPhone; U; Linux i686; pt-br) AppleWebKit/532+ (KHTML, like Gecko) Version/3.0 Mobile/1A538b Safari/419.3 Midori/0.2.0"
19
- process_user_agent(ua_str)
30
+ user_agent_strings.each do |ua_str|
31
+ process_user_agent(ua_str)
32
+ end
@@ -6,6 +6,7 @@ module AgentOrange
6
6
  BROWSERS = {
7
7
  :ie => 'MSIE|Internet Explorer|IE',
8
8
  :firefox => 'Firefox',
9
+ :opera => 'Opera',
9
10
  :safari => 'Safari'
10
11
  }
11
12
 
@@ -36,6 +37,8 @@ module AgentOrange
36
37
  self.type = "firefox"
37
38
  elsif name =~ /(#{BROWSERS[:safari]})/i
38
39
  self.type = "safari"
40
+ elsif name =~ /(#{BROWSERS[:opera]})/i
41
+ self.type = "opera"
39
42
  else
40
43
  self.type = "other"
41
44
  end
@@ -1,3 +1,5 @@
1
+ require 'agent_orange/platform'
2
+ require 'agent_orange/operating_system'
1
3
  require 'agent_orange/engine'
2
4
 
3
5
  module AgentOrange
@@ -28,9 +30,9 @@ module AgentOrange
28
30
  if comment =~ /(#{DEVICES.collect{|cat,regex| regex}.join(')|(')})/i
29
31
  # Found the device
30
32
  AgentOrange.debug " Got a device in group #{i+1}!", 2
31
- AgentOrange.debug " Raw Name : #{name}", 2
32
- AgentOrange.debug " Raw Version: #{version}", 2
33
- AgentOrange.debug " Raw Comment: #{comment}", 2
33
+ AgentOrange.debug " Group Raw Name : #{name}", 2
34
+ AgentOrange.debug " Group Raw Version: #{version}", 2
35
+ AgentOrange.debug " Group Raw Comment: #{comment}", 2
34
36
  AgentOrange.debug "", 2
35
37
 
36
38
  # Determine device type
@@ -45,7 +47,7 @@ module AgentOrange
45
47
  end
46
48
 
47
49
  # Determine device name
48
- self.name = comment.split(';')[0]
50
+ self.name = self.type.capitalize
49
51
 
50
52
  # Determine device version
51
53
  self.version = nil
@@ -60,6 +62,8 @@ module AgentOrange
60
62
  AgentOrange.debug " Version: #{self.version}", 2
61
63
  AgentOrange.debug "", 2
62
64
 
65
+ self.platform = AgentOrange::Platform.new(user_agent)
66
+ self.operating_system = AgentOrange::OperatingSystem.new(user_agent)
63
67
  self.engine = AgentOrange::Engine.new(user_agent)
64
68
  end
65
69
 
@@ -1,9 +1,88 @@
1
1
  module AgentOrange
2
2
  class OperatingSystem
3
- attr_accessor :name, :version
3
+ attr_accessor :type, :name, :version
4
4
 
5
+ OPERATING_SYSTEMS = {
6
+ :android => 'android',
7
+ :freebsd => 'freebsd',
8
+ :ios => 'iphone|ipad|ipod',
9
+ :linux => 'linux',
10
+ :netbsd => 'netbsd',
11
+ :osx => 'osx',
12
+ :windows => 'windows'
13
+ }
14
+
15
+ OPERATING_SYSTEM_NAMES = {
16
+ :android => 'Android',
17
+ :freebsd => 'FreeBSD',
18
+ :ios => 'Apple iOS',
19
+ :linux => 'Linux',
20
+ :netbsd => 'NetBSD',
21
+ :osx => 'Mac OS X',
22
+ :windows => 'Windows'
23
+ }
24
+
25
+ def initialize(user_agent)
26
+ self.parse(user_agent)
27
+ end
28
+
29
+ def parse(user_agent)
30
+ AgentOrange.debug "OPERATING SYSTEM PARSING", 2
31
+ groups = user_agent.scan(/([^\/[:space:]]*)(\/([^[:space:]]*))?([[:space:]]*\[[a-zA-Z][a-zA-Z]\])?[[:space:]]*(\((([^()]|(\([^()]*\)))*)\))?[[:space:]]*/i)
32
+ groups.each_with_index do |pieces,i|
33
+ name = pieces[0]
34
+ version = pieces[2]
35
+ comment = pieces[5]
36
+
37
+ if comment =~ /(#{OPERATING_SYSTEMS.collect{|cat,regex| regex}.join(')|(')})/i
38
+ # Found the operating system
39
+ AgentOrange.debug " Got an operating system in group #{i+1}!", 2
40
+ AgentOrange.debug " Raw Name : #{name}", 2
41
+ AgentOrange.debug " Raw Version: #{version}", 2
42
+ AgentOrange.debug " Raw Comment: #{comment}", 2
43
+ AgentOrange.debug "", 2
44
+
45
+ # Determine operating system type
46
+ if comment =~ /(#{OPERATING_SYSTEMS[:android]})/i
47
+ self.type = "android"
48
+ end
49
+ if comment =~ /(#{OPERATING_SYSTEMS[:freebsd]})/i
50
+ self.type = "freebsd"
51
+ end
52
+ if comment =~ /(#{OPERATING_SYSTEMS[:ios]})/i
53
+ self.type = "ios"
54
+ end
55
+ if comment =~ /(#{OPERATING_SYSTEMS[:linux]})/i
56
+ self.type = "linux"
57
+ end
58
+ if comment =~ /(#{OPERATING_SYSTEMS[:netbsd]})/i
59
+ self.type = "netbsd"
60
+ end
61
+ if comment =~ /(#{OPERATING_SYSTEMS[:osx]})/i
62
+ self.type = "osx"
63
+ end
64
+ if comment =~ /(#{OPERATING_SYSTEMS[:windows]})/i
65
+ self.type = "windows"
66
+ end
67
+
68
+ # Determine operating system name
69
+ self.name = OPERATING_SYSTEM_NAMES[self.type.to_sym]
70
+
71
+ # Determine operating system version
72
+ self.version = nil
73
+ end
74
+
75
+ end
76
+
77
+ AgentOrange.debug "OPERATING SYSTEM ANALYSIS", 2
78
+ AgentOrange.debug " Type: #{self.type}", 2
79
+ AgentOrange.debug " Name: #{self.name}", 2
80
+ AgentOrange.debug " Version: #{self.version}", 2
81
+ AgentOrange.debug "", 2
82
+ end
83
+
5
84
  def to_s
6
- "OS X 10.6"
85
+ [self.name, self.version].compact.join(' ')
7
86
  end
8
87
  end
9
88
  end
@@ -1,9 +1,73 @@
1
1
  module AgentOrange
2
2
  class Platform
3
- attr_accessor :name, :version
3
+ attr_accessor :type, :name, :version
4
+
5
+ PLATFORMS = {
6
+ :android => 'android',
7
+ :ios => 'iphone|ipad|ipod',
8
+ :mac => 'macintosh',
9
+ :pc => 'freebsd|linux|netbsd|windows|x11'
10
+ }
11
+
12
+ PLATFORM_NAMES = {
13
+ :android => 'Android',
14
+ :ios => 'iOS',
15
+ :mac => 'Macintosh',
16
+ :pc => 'PC'
17
+ }
18
+
19
+ def initialize(user_agent)
20
+ self.parse(user_agent)
21
+ end
22
+
23
+ def parse(user_agent)
24
+ AgentOrange.debug "PLATFORM PARSING", 2
25
+ groups = user_agent.scan(/([^\/[:space:]]*)(\/([^[:space:]]*))?([[:space:]]*\[[a-zA-Z][a-zA-Z]\])?[[:space:]]*(\((([^()]|(\([^()]*\)))*)\))?[[:space:]]*/i)
26
+ groups.each_with_index do |pieces,i|
27
+ name = pieces[0]
28
+ version = pieces[2]
29
+ comment = pieces[5]
30
+
31
+ if comment =~ /(#{PLATFORMS.collect{|cat,regex| regex}.join(')|(')})/i
32
+ # Found the device
33
+ AgentOrange.debug " Got a platform in group #{i+1}!", 2
34
+ AgentOrange.debug " Raw Name : #{name}", 2
35
+ AgentOrange.debug " Raw Version: #{version}", 2
36
+ AgentOrange.debug " Raw Comment: #{comment}", 2
37
+ AgentOrange.debug "", 2
38
+
39
+ # Determine platform type
40
+ if comment =~ /(#{PLATFORMS[:android]})/i
41
+ self.type = "android"
42
+ end
43
+ if comment =~ /(#{PLATFORMS[:ios]})/i
44
+ self.type = "ios"
45
+ end
46
+ if comment =~ /(#{PLATFORMS[:mac]})/i
47
+ self.type = "mac"
48
+ end
49
+ if comment =~ /(#{PLATFORMS[:pc]})/i
50
+ self.type = "pc"
51
+ end
52
+
53
+ # Determine platform name
54
+ self.name = PLATFORM_NAMES[self.type.to_sym]
55
+
56
+ # Determine platform version
57
+ self.version = nil
58
+ end
59
+
60
+ end
61
+
62
+ AgentOrange.debug "PLATFORM ANALYSIS", 2
63
+ AgentOrange.debug " Type: #{self.type}", 2
64
+ AgentOrange.debug " Name: #{self.name}", 2
65
+ AgentOrange.debug " Version: #{self.version}", 2
66
+ AgentOrange.debug "", 2
67
+ end
4
68
 
5
69
  def to_s
6
- "#{name} #{version}"
70
+ [self.name, self.version].compact.join(' ')
7
71
  end
8
72
  end
9
73
  end
@@ -2,7 +2,7 @@ require 'agent_orange/device'
2
2
 
3
3
  module AgentOrange
4
4
  DEBUG = true
5
- DEBUG_LEVEL = 1
5
+ DEBUG_LEVEL = 2
6
6
 
7
7
  class UserAgent
8
8
  attr_accessor :user_agent_string
@@ -10,30 +10,23 @@ module AgentOrange
10
10
  attr_accessor :device
11
11
 
12
12
  def initialize(options = {}, &block)
13
- @user_agent_string = options[:user_agent].to_s
14
- self.parse(@user_agent_string)
13
+ self.parse(options[:user_agent].to_s)
15
14
 
16
15
  yield self if block_given?
17
16
  end
18
17
 
19
18
  def parse(user_agent)
20
- self.device = AgentOrange::Device.new(user_agent)
19
+ self.user_agent_string = user_agent
20
+ self.device = AgentOrange::Device.new(self.user_agent_string)
21
21
 
22
- AgentOrange.debug "Device = #{self.device}"
23
- AgentOrange.debug " Is computer? #{self.is_computer?}"
24
- AgentOrange.debug " Is mobile? #{self.is_mobile?}"
25
- if self.is_mobile?
26
- AgentOrange.debug " Is an iPhone? #{self.is_mobile? :iphone}"
27
- AgentOrange.debug " Is an iPad? #{self.is_mobile? :ipad}"
28
- AgentOrange.debug " Is an iPod? #{self.is_mobile? :ipod}"
29
- AgentOrange.debug " Is an Android? #{self.is_mobile? :android}"
30
- end
31
- AgentOrange.debug " Is bot? #{self.is_bot?}"
32
- AgentOrange.debug
33
-
34
- AgentOrange.debug "Engine = #{self.device.engine}"
35
- AgentOrange.debug "Browser = #{self.device.engine.browser}"
22
+ AgentOrange.debug "Device = #{self.device}"
23
+ AgentOrange.debug "Platform = #{self.device.platform}"
24
+ AgentOrange.debug "OS = #{self.device.operating_system}"
25
+ AgentOrange.debug "Engine = #{self.device.engine}"
26
+ AgentOrange.debug "Browser = #{self.device.engine.browser}"
36
27
 
28
+ self.summary
29
+
37
30
  AgentOrange.debug "", 2
38
31
  AgentOrange.debug "user_agent.to_s = #{self}", 2
39
32
  end
@@ -51,7 +44,7 @@ module AgentOrange
51
44
  end
52
45
 
53
46
  def to_s
54
- [self.device, self.device.engine, self.device.engine.browser].compact.join(", ")
47
+ [self.device, self.device.platform, self.device.operating_system, self.device.engine, self.device.engine.browser].compact.join(", ")
55
48
  end
56
49
 
57
50
  def to_human_string
@@ -62,6 +55,20 @@ module AgentOrange
62
55
  end
63
56
  end
64
57
 
58
+ def summary
59
+ AgentOrange.debug
60
+ AgentOrange.debug "SUMMARY"
61
+ AgentOrange.debug " Is computer? #{self.is_computer?}"
62
+ AgentOrange.debug " Is mobile? #{self.is_mobile?}"
63
+ if self.is_mobile?
64
+ AgentOrange.debug " Is an iPhone? #{self.is_mobile? :iphone}"
65
+ AgentOrange.debug " Is an iPad? #{self.is_mobile? :ipad}"
66
+ AgentOrange.debug " Is an iPod? #{self.is_mobile? :ipod}"
67
+ AgentOrange.debug " Is an Android? #{self.is_mobile? :android}"
68
+ end
69
+ AgentOrange.debug " Is bot? #{self.is_bot?}"
70
+ AgentOrange.debug
71
+ end
65
72
  end
66
73
 
67
74
  def self.debug(str="", level=1)
@@ -1,5 +1,5 @@
1
1
  module AgentOrange
2
- VERSION = "0.0.2" # This is for the gem and does not conflict with the rest of the functionality
2
+ VERSION = "0.0.3" # 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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kevin Elliott