user_agent_sanitizer 1.1.0 → 2.0.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.
- data/README.md +13 -3
- data/lib/user_agent_sanitizer.rb +6 -7
- data/spec/user_agent_sanitizer_spec.rb +16 -7
- data/user_agent_sanitizer.gemspec +3 -3
- metadata +2 -2
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
User agent sanitizer
|
2
2
|
====================
|
3
3
|
|
4
|
-
Parse a HTTP user agent and attempt to return something that means something to a human.
|
4
|
+
Parse a HTTP user agent and attempt to return something that means something to a human. I started this because I needed to sanitize usage statistics by mobile phones. Most user agent parsers focus on the browser, because that is what matters on the desktop. On mobile I care more about the phone than the browser, so those are the details I focus on.
|
5
5
|
|
6
6
|
Example:
|
7
7
|
|
@@ -12,7 +12,7 @@ Example:
|
|
12
12
|
|
13
13
|
UserAgentSanitizer.sanitize_user_agent('Mozilla/5.0 (Linux; U; Android 2.3.3; nl-nl; SAMSUNG GT-I9100/I9100BUKE5 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1')
|
14
14
|
# => 'Samsung GT-I9100'
|
15
|
-
|
15
|
+
|
16
16
|
Since version 1.1.0, UserAgentSanitizer.user_agent is available:
|
17
17
|
|
18
18
|
ua = UserAgentSanitizer.user_agent('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3')
|
@@ -23,6 +23,11 @@ Since version 1.1.0, UserAgentSanitizer.user_agent is available:
|
|
23
23
|
ua.to_s
|
24
24
|
# => "Apple iPhone"
|
25
25
|
|
26
|
+
Changes
|
27
|
+
-------
|
28
|
+
|
29
|
+
* Version 2.0: I care more about devices on mobile. On the desktop, I'll prioritize OS above browser. For this reason I broke backwards compatibility and now detect Microsoft Internet Explorer as the operating system instead of as a browser in its own right.
|
30
|
+
|
26
31
|
Installation
|
27
32
|
------------
|
28
33
|
|
@@ -49,7 +54,12 @@ Contributing
|
|
49
54
|
|
50
55
|
Create a Github issue and/or send a pull request.
|
51
56
|
|
57
|
+
Plans
|
58
|
+
-----
|
59
|
+
|
60
|
+
* Parse browser from user agent. For mobile this should expose devices using Opera Mini. On the desktop, it should reveal the browser instead of the OS.
|
61
|
+
|
52
62
|
Contributors
|
53
63
|
------------
|
54
64
|
|
55
|
-
* [Wes 'Narnach' Oldenbeuving](http://narnach.com/)
|
65
|
+
* [Wes 'Narnach' Oldenbeuving](http://narnach.com/)
|
data/lib/user_agent_sanitizer.rb
CHANGED
@@ -76,16 +76,12 @@ module UserAgentSanitizer
|
|
76
76
|
def basic_user_agent_recognizer
|
77
77
|
string = @user_agent.dup
|
78
78
|
case string
|
79
|
-
when /(Mac OS X) (\d+_\d+
|
79
|
+
when /(Mac OS X) (\d+([_.]\d+)+)/
|
80
80
|
@brand = 'Apple'
|
81
81
|
@model = "#{$1} #{$2.gsub("_", ".")}"
|
82
82
|
return nil
|
83
83
|
when /(LG|sagem)[\-\/](\w+)/i
|
84
84
|
return "#{$1} #{$2}"
|
85
|
-
when /(MSIE) (\d+(\.\d+)*)/
|
86
|
-
@brand = 'Microsoft'
|
87
|
-
@model = "Internet Explorer #{$2}"
|
88
|
-
return nil
|
89
85
|
when /(Blackberry) ?(\d+)/i
|
90
86
|
return "#{$1} #{$2}"
|
91
87
|
when /(HTC)[_\/]([a-z0-9]+)?/i
|
@@ -94,10 +90,13 @@ module UserAgentSanitizer
|
|
94
90
|
return ['Samsung', $2, $4].compact.join(" ")
|
95
91
|
when /\((Linux; U; Android.*)\)/
|
96
92
|
return $1.split(";").last.split("/").first.gsub(/build/i, "").strip
|
97
|
-
when /(iPod)/
|
93
|
+
when /(iPod|iPad)/
|
98
94
|
@brand = 'Apple'
|
99
|
-
@model =
|
95
|
+
@model = $1
|
100
96
|
return nil
|
97
|
+
when /(Windows NT \w+(\.\w+)+)/
|
98
|
+
@brand = "Microsoft"
|
99
|
+
@model = $1
|
101
100
|
when /((#{BRANDS.join("|")}).*?)\//i
|
102
101
|
result=$1
|
103
102
|
|
@@ -4,21 +4,21 @@ require 'user_agent_sanitizer'
|
|
4
4
|
|
5
5
|
describe UserAgentSanitizer do
|
6
6
|
DEVICES = {
|
7
|
+
# Mobile
|
7
8
|
'BlackBerry8520/4.6.1.296 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/150' => ['BlackBerry', '8520'],
|
8
9
|
'BlackBerry8900/4.6.1.199 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/302' => ['BlackBerry', '8900'],
|
10
|
+
'HTC_Touch2_T3333 Opera/9.50 (Windows NT 5.1; U; nl)' => ['HTC','Touch2'],
|
9
11
|
'LG-GD510/V100 Teleca/WAP2.0 Profile/MIDP-2.1 Configuration/CLDC-1.1' => ['LG', 'GD510'],
|
10
12
|
'LG/KU990/v10a Browser/Obigo-Q05A/3.6 MMS/LG-MMS-V1.0/1.2 Java/ASVM/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1' => ['LG','KU990'],
|
11
|
-
'Mozilla/5.0 (Linux; U; Android 2.1-update1; nl-nl; Desire_A8181 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17' => ['HTC','Desire'],
|
12
|
-
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB0.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)' => ['Microsoft','Internet Explorer 7.0'],
|
13
13
|
'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; nl) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.246 Mobile Safari/534.1+' => ['BlackBerry','9800'],
|
14
14
|
'Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3' => ['Apple','iPhone'],
|
15
15
|
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5' => ['Apple','iPhone'],
|
16
16
|
'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; nl-nl) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3' => ['Apple','iPhone'],
|
17
17
|
'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5' => ['Apple','iPod'],
|
18
|
-
'Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8500/S8500XXJEC; U; Bada/1.0; nl-nl) AppleWebKit/533.1 (KHTML, like Gecko) Dolfin/2.0 Mobile WVGA SMM-MMS/1.2.0 OPN-B' => ['Samsung','GT-S8500'],
|
19
|
-
'HTC_Touch2_T3333 Opera/9.50 (Windows NT 5.1; U; nl)' => ['HTC','Touch2'],
|
20
18
|
'Mozilla/5.0 (Linux; U; Android 1.5; en-gb; HTC Magic Build/CRB17) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1' => ['HTC','Magic'],
|
21
19
|
'Mozilla/5.0 (Linux; U; Android 1.5; nl-nl; GT-I5700 Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1' => ['Samsung','GT-I5700'],
|
20
|
+
'Mozilla/5.0 (Linux; U; Android 1.6; nl-nl; SonyEricssonX10i Build/R1FA016) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1' => ['SonyEricsson','X10i'],
|
21
|
+
'Mozilla/5.0 (Linux; U; Android 2.1-update1; nl-nl; Desire_A8181 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17' => ['HTC','Desire'],
|
22
22
|
'Mozilla/5.0 (Linux; U; Android 2.1; nl-nl; HTC Legend Build/ERD79) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17' => ['HTC','Legend'],
|
23
23
|
'Mozilla/5.0 (Linux; U; Android 2.2.1; es-es; Vodafone 858 Build/Vodafone858C02B617) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => ['Vodafone','858'],
|
24
24
|
'Mozilla/5.0 (Linux; U; Android 2.2; es-es; Light Pro Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => ['Light','Pro'],
|
@@ -28,8 +28,7 @@ describe UserAgentSanitizer do
|
|
28
28
|
'Mozilla/5.0 (Linux; U; Android 2.3.3; nl-nl; SAMSUNG GT-I9100/I9100BUKE5 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => ['Samsung','GT-I9100'],
|
29
29
|
'Mozilla/5.0 (Linux; U; Android 2.3.5; es-es; ZTE Skate Build/V1.0.0B03) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => ['ZTE','Skate'],
|
30
30
|
'Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus One Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' => ['Nexus','One'],
|
31
|
-
'Mozilla/5.0 (
|
32
|
-
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10' => ['Apple','Mac OS X 10.6.2'],
|
31
|
+
'Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8500/S8500XXJEC; U; Bada/1.0; nl-nl) AppleWebKit/533.1 (KHTML, like Gecko) Dolfin/2.0 Mobile WVGA SMM-MMS/1.2.0 OPN-B' => ['Samsung','GT-S8500'],
|
33
32
|
'Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaE90-1/07.24.0.3; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413' => ['Nokia','E90'],
|
34
33
|
'Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 NokiaE75-1/110.48.125 Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413' => ['Nokia','E75'],
|
35
34
|
'Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Samsung/I8910; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/525 (KHTML, like Gecko) Version/3.0 Safari/525' => ['Samsung','I8910'],
|
@@ -39,7 +38,17 @@ describe UserAgentSanitizer do
|
|
39
38
|
'SAMSUNG-SGH-L600/L600ASGI3 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0' => ['Samsung','SGH L600'],
|
40
39
|
'SEC-SGHM620/1.0 Openwave/6.2.3 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2.3.3.c.1.101 (GUI) MMP/2.0' => ['SEC','SGH M620'],
|
41
40
|
'SonyEricssonW995/R1FA Browser/NetFront/3.4 Profile/MIDP-2.1 Configuration/CLDC-1.1 JavaPlatform/JP-8.4.3' => ['SonyEricsson','W995'],
|
42
|
-
|
41
|
+
|
42
|
+
# Tables
|
43
|
+
'(iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3' => ['Apple', 'iPad'],
|
44
|
+
|
45
|
+
# Desktop
|
46
|
+
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB0.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)' => ['Microsoft','Windows NT 6.0'],
|
47
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0' => ['Apple','Mac OS X 10.7'],
|
48
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5' => ['Apple','Mac OS X 10.7.4'],
|
49
|
+
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; nl-nl) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1' => ['Apple','Mac OS X 10.5.5'],
|
50
|
+
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10' => ['Apple','Mac OS X 10.6.2'],
|
51
|
+
'(Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10' => ['Microsoft','Windows NT 6.1'],
|
43
52
|
}
|
44
53
|
describe "sanitize_user_agent" do
|
45
54
|
DEVICES.each do |user_agent, brand_model|
|
@@ -3,13 +3,13 @@ Gem::Specification.new do |s|
|
|
3
3
|
s.name = 'user_agent_sanitizer'
|
4
4
|
s.summary = "Browser user agent sanitizer"
|
5
5
|
s.description = "Browser user agent sanitizer, with a focus on sanitizing mobile phone user agents to brand + model number"
|
6
|
-
s.version = '
|
7
|
-
s.date = '
|
6
|
+
s.version = '2.0.0'
|
7
|
+
s.date = '2012-07-11'
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Wes Oldenbeuving"]
|
10
10
|
s.email = "narnach@gmail.com"
|
11
11
|
s.homepage = "http://www.github.com/Narnach/user_agent_sanitizer"
|
12
|
-
|
12
|
+
|
13
13
|
# Files
|
14
14
|
s.require_path = "lib"
|
15
15
|
s.files = ['user_agent_sanitizer.gemspec', 'LICENSE', 'README.md', 'Rakefile', 'lib/user_agent_sanitizer.rb', 'spec/user_agent_sanitizer_spec.rb', 'spec/spec_helper.rb']
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: user_agent_sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-07-11 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Browser user agent sanitizer, with a focus on sanitizing mobile phone
|
15
15
|
user agents to brand + model number
|