jaxn-parse_user_agent 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -0
- data/lib/parse_user_agent.rb +95 -0
- data/parse_user_agent.gemspec +17 -0
- data/test/parse_user_agent_test.rb +93 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
A simple user agent parser for Ruby.
|
@@ -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,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "parse_user_agent"
|
3
|
+
s.version = "0.1.1"
|
4
|
+
s.date = "2008-12-20"
|
5
|
+
s.summary = "UserAgent parser for Ruby."
|
6
|
+
s.email = "jackson@tekzen.net"
|
7
|
+
s.homepage = "http://github.com/jaxn/parse_user_agent"
|
8
|
+
s.description = ""
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Jackson Miller"]
|
11
|
+
s.files = ["README",
|
12
|
+
"parse_user_agent.gemspec",
|
13
|
+
"lib/parse_user_agent.rb"]
|
14
|
+
s.test_files = ["test/parse_user_agent_test.rb"]
|
15
|
+
s.rdoc_options = ["--main", "README"]
|
16
|
+
s.extra_rdoc_files = ["README"]
|
17
|
+
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,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jaxn-parse_user_agent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jackson Miller
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-20 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: jackson@tekzen.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- parse_user_agent.gemspec
|
27
|
+
- lib/parse_user_agent.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://github.com/jaxn/parse_user_agent
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options:
|
32
|
+
- --main
|
33
|
+
- README
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.2.0
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: UserAgent parser for Ruby.
|
55
|
+
test_files:
|
56
|
+
- test/parse_user_agent_test.rb
|