ParseUserAgent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,95 @@
1
+ class ParseUserAgent
2
+
3
+ attr_reader :ostype, :browser, :os_version, :browser_version_major
4
+
5
+ def parse(user_agent)
6
+ if '-' == user_agent
7
+ raise 'Invalid User Agent'
8
+ end
9
+
10
+ @user_agent = user_agent
11
+
12
+ # fix Opera
13
+ #useragent =~ s/Opera (\d)/Opera\/$1/i;
14
+ useragent = @user_agent.gsub(/(Opera [\d])/,'Opera\1')
15
+
16
+ # grab all Agent/version strings as 'agents'
17
+ @agents = Array.new
18
+ @user_agent.split(/\s+/).each {|string|
19
+ if string =~ /\//
20
+ @agents<< string
21
+ end
22
+ }
23
+
24
+ # cycle through the agents to set browser and version (MSIE is set later)
25
+ if @agents && @agents.length > 0
26
+ @agents.each {|agent|
27
+ parts = agent.split('/')
28
+ @browser = parts[0]
29
+ @browser_version = parts[1]
30
+ if @browser == 'Firefox'
31
+ @browser_version_major = parts[1].slice(0,3)
32
+ @browser_version_minor = parts[1].sub(@browser_version_major,'').sub('.','')
33
+ elsif @browser == 'Safari'
34
+ if parts[1].slice(0,3).to_f < 400
35
+ @browser_version_major = '1'
36
+ else
37
+ @browser_version_major = '2'
38
+ end
39
+ else
40
+ @browser_version_major = parts[1].slice(0,1)
41
+ end
42
+ }
43
+ end
44
+
45
+ # grab all of the properties (within parens)
46
+ # should be in relation to the agent if possible
47
+ @detail = @user_agent
48
+ @user_agent.gsub(/\((.*)\)/,'').split(/\s/).each {|part| @detail = @detail.gsub(part,'')}
49
+ @detail = @detail.gsub('(','').gsub(')','').lstrip
50
+ @properties = @detail.split(/;\s+/)
51
+
52
+ # cycle through the properties to set known quantities
53
+ @properties.each {|property|
54
+ if property =~ /^Win/
55
+ @ostype = 'Windows'
56
+ @os = property
57
+ if parts = property.split(/ /,2)
58
+ if parts[1] =~ /^NT/
59
+ @ostype = 'Windows'
60
+ subparts = parts[1].split(/ /,2)
61
+ if subparts[1] == '5'
62
+ @os_version = '2000'
63
+ elsif subparts[1] == '5.1'
64
+ @os_version = 'XP'
65
+ else
66
+ @os_version = subparts[1]
67
+ end
68
+ end
69
+ end
70
+ end
71
+ if property == 'Macintosh'
72
+ @ostype = 'Macintosh'
73
+ @os = property
74
+ end
75
+ if property =~ /OS X/
76
+ @ostype = 'Macintosh'
77
+ @os_version = 'OS X'
78
+ @os = property
79
+ end
80
+ if property =~ /^Linux/
81
+ @ostype = 'Linux'
82
+ @os = property
83
+ end
84
+ if property =~ /^MSIE/
85
+ @browser = 'MSIE'
86
+ @browser_version = property.gsub('MSIE ','').lstrip
87
+ @browser_version_major,@browser_version_minor = @browser_version.split('.')
88
+ end
89
+ }
90
+ self
91
+ end
92
+
93
+
94
+
95
+ end
@@ -0,0 +1,93 @@
1
+ require 'test/unit'
2
+ require 'parse_user_agent'
3
+
4
+ class ParseUserAgentTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ # setup a list of user agent strings to test
8
+ @user_agents = Array.new
9
+ @user_agents[0] = ParseUserAgent.new
10
+ @user_agents[1] = ParseUserAgent.new
11
+ @user_agents[2] = ParseUserAgent.new
12
+ @user_agents[3] = ParseUserAgent.new
13
+ @user_agents[4] = ParseUserAgent.new
14
+ @user_agents[5] = ParseUserAgent.new
15
+ @user_agents[6] = ParseUserAgent.new
16
+ @user_agents[7] = ParseUserAgent.new
17
+
18
+ @user_agent_strings = Array.new
19
+ @user_agent_strings[0] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'
20
+ @user_agent_strings[1] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)'
21
+ @user_agent_strings[2] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
22
+ @user_agent_strings[3] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2 '
23
+ @user_agent_strings[4] = 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'
24
+ @user_agent_strings[5] = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'
25
+ @user_agent_strings[6] = 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.9.2'
26
+ @user_agent_strings[7] = 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) NetNewsWire/2.0.1'
27
+ # need to add others like older MSIE, Opera / Konqueror, and Linux / *BSD options, and other languages (does IE not include language?)
28
+
29
+ @user_agent_strings.each_index { |n| @user_agents[n].parse(@user_agent_strings[n])}
30
+
31
+ end
32
+
33
+ # this should throw a better exception
34
+ def test_invalid
35
+ assert_raise(RuntimeError) { ParseUserAgent.new.parse('-')}
36
+ # need to test blank user agent strings as well
37
+ end
38
+
39
+ # test using new to create
40
+ def test_create
41
+ @user_agents.each { |user_agent| assert_kind_of ParseUserAgent, user_agent}
42
+ end
43
+
44
+ # this test sucks because I had to parse during setup
45
+ def test_parse
46
+ @user_agent_strings.each_index { |n| @user_agents[n].parse(@user_agent_strings[n])}
47
+ @user_agents.each { |user_agent| assert_kind_of ParseUserAgent, user_agent}
48
+ end
49
+
50
+ def test_browser
51
+ assert_equal 'Firefox', @user_agents[0].browser
52
+ assert_equal 'MSIE', @user_agents[1].browser
53
+ assert_equal 'MSIE', @user_agents[2].browser
54
+ assert_equal 'Firefox', @user_agents[3].browser
55
+ assert_equal 'Firefox', @user_agents[4].browser
56
+ assert_equal 'MSIE', @user_agents[5].browser
57
+ assert_equal 'Safari', @user_agents[6].browser
58
+ assert_equal 'NetNewsWire', @user_agents[7].browser
59
+ # add additional tests when more test strings are available
60
+ end
61
+
62
+ def test_browser_version_major
63
+ assert_equal '1.5',@user_agents[0].browser_version_major
64
+ assert_equal '6',@user_agents[1].browser_version_major
65
+ assert_equal '6',@user_agents[2].browser_version_major
66
+ assert_equal '1.5', @user_agents[3].browser_version_major
67
+ assert_equal '1.5', @user_agents[4].browser_version_major
68
+ assert_equal '6', @user_agents[5].browser_version_major
69
+ assert_equal '2', @user_agents[6].browser_version_major
70
+ assert_equal '2', @user_agents[7].browser_version_major
71
+ end
72
+
73
+ def test_ostype
74
+ assert_equal 'Windows', @user_agents[0].ostype
75
+ assert_equal 'Windows', @user_agents[1].ostype
76
+ assert_equal 'Windows', @user_agents[2].ostype
77
+ assert_equal 'Windows', @user_agents[3].ostype
78
+ assert_equal 'Macintosh', @user_agents[4].ostype
79
+ assert_equal 'Windows', @user_agents[5].ostype
80
+ assert_equal 'Macintosh', @user_agents[6].ostype
81
+ end
82
+
83
+ def test_os_version
84
+ assert_equal 'XP', @user_agents[0].os_version
85
+ assert_equal 'XP', @user_agents[1].os_version
86
+ assert_equal 'XP', @user_agents[2].os_version
87
+ assert_equal 'XP', @user_agents[3].os_version
88
+ assert_equal 'OS X', @user_agents[4].os_version
89
+ assert_equal 'XP', @user_agents[5].os_version
90
+ assert_equal 'OS X', @user_agents[6].os_version
91
+ end
92
+
93
+ end
metadata ADDED
@@ -0,0 +1,41 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: ParseUserAgent
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-05-16
8
+ summary: Parse HTTP User Agent strings
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage: http://jaxn.org/blog/tag/parseuseragent
13
+ rubyforge_project: parseuseragent
14
+ description: "ParseUserAgent is a library for parsing HTTP User Agent strings. It provides
15
+ access to the browser name, broswer version (major and minor), operating system
16
+ name, and operating system version (major and minor)."
17
+ autorequire:
18
+ default_executable:
19
+ bindir: bin
20
+ has_rdoc: false
21
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
22
+ requirements:
23
+ -
24
+ - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.0
27
+ version:
28
+ platform: ruby
29
+ authors:
30
+ - Jackson Miller
31
+ files:
32
+ - lib/parse_user_agent.rb
33
+ - test/unit
34
+ - test/unit/parse_user_agent_test.rb
35
+ test_files: []
36
+ rdoc_options: []
37
+ extra_rdoc_files: []
38
+ executables: []
39
+ extensions: []
40
+ requirements: []
41
+ dependencies: []