useragent_parser 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require "useragent_parser/version"
2
2
  require "useragent_parser/parser"
3
+ require "useragent_parser/user_agent"
3
4
 
4
5
  module UseragentParser
5
6
  USER_AGENT_PARSERS = []
@@ -56,7 +57,7 @@ module UseragentParser
56
57
  if js_user_agent_string && js_user_agent_string.include?('Chrome/') && user_agent_string.include?('chromeframe')
57
58
  family = 'Chrome Frame (%s %s)' % [ family, v1 ]
58
59
  js_ua = UseragentParser.parse(js_user_agent_string)
59
- cf_family, v1, v2, v3 = js_ua['family'], js_ua['v1'], js_ua['v2'], js_ua['v3']
60
+ cf_family, v1, v2, v3 = js_ua.browser_family, js_ua.browser_major_version, js_ua.browser_minor_version, js_ua.browser_patch_version
60
61
  end
61
62
 
62
63
  OS_PARSERS.each do |parser|
@@ -64,16 +65,7 @@ module UseragentParser
64
65
  break unless os_family.nil?
65
66
  end
66
67
 
67
- {
68
- 'family' => family || 'Other',
69
- 'v1' => v1,
70
- 'v2' => v2,
71
- 'v3' => v3,
72
- 'os_family' => os_family || 'Other',
73
- 'os_v1' => os_v1,
74
- 'os_v2' => os_v2,
75
- 'os_v3' => os_v3
76
- }
68
+ UseragentParser::UserAgent.new({ 'family' => family, 'v1' => v1, 'v2' => v2, 'v3' => v3, 'os_family' => os_family, 'os_v1' => os_v1, 'os_v2' => os_v2, 'os_v3' => os_v3 })
77
69
  end
78
70
  end
79
71
 
@@ -0,0 +1,88 @@
1
+ module UseragentParser
2
+ class UserAgent
3
+ attr_reader :browser, :browser_family, :browser_version, :browser_major_version, :browser_minor_version, :browser_patch_version
4
+ attr_reader :os, :os_family, :os_version, :os_major_version, :os_minor_version, :os_patch_version
5
+
6
+ def initialize(details = {})
7
+ @browser_family = details['family'] || 'Other'
8
+ @browser_major_version = details['v1']
9
+ @browser_minor_version = details['v2']
10
+ @browser_patch_version = details['v3']
11
+
12
+ @os_family = details['os_family'] || 'Other'
13
+ @os_major_version = details['os_v1']
14
+ @os_minor_version = details['os_v2']
15
+ @os_patch_version = details['os_v3']
16
+ end
17
+
18
+ def browser_version
19
+ @browser_version ||= "#{browser_major_version}.#{browser_minor_version}.#{browser_patch_version}".gsub(/\.+$/, '').strip
20
+ end
21
+
22
+ def os_version
23
+ @browser_version ||= "#{os_major_version}.#{os_minor_version}.#{os_patch_version}".gsub(/\.+$/, '').strip
24
+ end
25
+
26
+ def os
27
+ @os ||= os_name
28
+ end
29
+
30
+ def browser
31
+ @browser ||= browser_name
32
+ end
33
+
34
+ protected
35
+
36
+ def browser_name
37
+ "#{browser_family} #{browser_version}"
38
+ end
39
+
40
+ def os_name
41
+ case os_family
42
+ when 'Windows' then os_names_win
43
+ when 'Windows NT' then os_names_winnt
44
+ when 'Mac OS X' then os_names_osx
45
+ else "#{os_family} #{os_version}".strip
46
+ end
47
+ end
48
+
49
+ def os_release
50
+ @release ||= "#{os_major_version}.#{os_minor_version}".gsub(/\.$/, '')
51
+ end
52
+
53
+ def os_names_win
54
+ "Microsoft Windows #{os_version}"
55
+ end
56
+
57
+ def os_names_winnt
58
+ prefix = "Microsoft Windows"
59
+ case os_release
60
+ when "3.1" then "#{prefix} NT 3.1"
61
+ when "3.5" then "#{prefix} NT 3.5"
62
+ when "4.0" then "#{prefix} NT 4.0"
63
+ when "5.0" then "#{prefix} 2000"
64
+ when "5.1" then "#{prefix} XP"
65
+ when "5.2" then "#{prefix} XP 64bit / Server 2003"
66
+ when "6.0" then "#{prefix} Vista / Server 2008"
67
+ when "6.1" then "#{prefix} 7 / Server 2008 R2"
68
+ when "6.2" then "#{prefix} 8"
69
+ else "#{prefix} #{os_version}"
70
+ end
71
+ end
72
+
73
+ def os_names_osx
74
+ prefix = "Apple Mac OS X"
75
+ case os_release
76
+ when "10.0" then "#{prefix} #{os_version} (Cheetah)"
77
+ when "10.1" then "#{prefix} #{os_version} (Puma)"
78
+ when "10.2" then "#{prefix} #{os_version} (Jaguar)"
79
+ when "10.3" then "#{prefix} #{os_version} (Panther)"
80
+ when "10.4" then "#{prefix} #{os_version} (Tiger)"
81
+ when "10.5" then "#{prefix} #{os_version} (Leopard)"
82
+ when "10.6" then "#{prefix} #{os_version} (Snow Leopard)"
83
+ when "10.7" then "#{prefix} #{os_version} (Lion)"
84
+ else "#{prefix} #{os_version}"
85
+ end
86
+ end
87
+ end
88
+ end
@@ -1,3 +1,3 @@
1
1
  module UseragentParser
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'multi_json'
5
+ require 'useragent_parser'
5
6
 
6
7
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
7
8
 
@@ -0,0 +1,161 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe UseragentParser::UserAgent do
6
+ describe "for browser details" do
7
+ it "should report the browser family" do
8
+ UseragentParser::UserAgent.new({ 'family' => 'Chrome', 'v1' => '14', 'v2' => '0', 'v3' => '835' }).browser_family.should == 'Chrome'
9
+ end
10
+
11
+ it "should report the browser major version" do
12
+ UseragentParser::UserAgent.new({ 'family' => 'Chrome', 'v1' => '14', 'v2' => '0', 'v3' => '835' }).browser_major_version.should == '14'
13
+ end
14
+
15
+ it "should report the browser minor version" do
16
+ UseragentParser::UserAgent.new({ 'family' => 'Chrome', 'v1' => '14', 'v2' => '0', 'v3' => '835' }).browser_minor_version.should == '0'
17
+ end
18
+
19
+ it "should report the browser patch version" do
20
+ UseragentParser::UserAgent.new({ 'family' => 'Chrome', 'v1' => '14', 'v2' => '0', 'v3' => '835' }).browser_patch_version.should == '835'
21
+ end
22
+
23
+ it "should report the browser version" do
24
+ UseragentParser::UserAgent.new({ 'family' => 'Chrome', 'v1' => '14', 'v2' => '0', 'v3' => '835' }).browser_version.should == '14.0.835'
25
+ end
26
+
27
+ it "should report the browser version without patch level" do
28
+ UseragentParser::UserAgent.new({ 'family' => 'Firefox', 'v1' => '5', 'v2' => '0' }).browser_version.should == '5.0'
29
+ end
30
+
31
+ it "should report the browser version without minor version" do
32
+ UseragentParser::UserAgent.new({ 'family' => 'Firefox', 'v1' => '5' }).browser_version.should == '5'
33
+ end
34
+
35
+ it "should report the browser family without version" do
36
+ UseragentParser::UserAgent.new({ 'family' => 'Opera' }).browser_version.should == ''
37
+ end
38
+ end
39
+
40
+ describe "for operating system details" do
41
+ it "should report the os family" do
42
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '3', 'os_v2' => '1' }).os_family.should == 'Windows NT'
43
+ end
44
+
45
+ it "should report the major os version" do
46
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '3', 'os_v2' => '1' }).os_major_version.should == '3'
47
+ end
48
+
49
+ it "should report the minor os version" do
50
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '3', 'os_v2' => '1' }).os_minor_version.should == '1'
51
+ end
52
+
53
+ it "should report the os patchlevel" do
54
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '3', 'os_v2' => '1' }).os_patch_version.should be_nil
55
+ end
56
+
57
+ it "should report the os version" do
58
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '3', 'os_v2' => '1' }).os_version.should == '3.1'
59
+ end
60
+
61
+ it "should know the code name for Windows NT 3.1" do
62
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '3', 'os_v2' => '1' }).os.should == 'Microsoft Windows NT 3.1'
63
+ end
64
+
65
+ it "should know the code name for Windows NT 3.5" do
66
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '3', 'os_v2' => '5' }).os.should == 'Microsoft Windows NT 3.5'
67
+ end
68
+
69
+ it "should know the code name for Windows NT 4.0" do
70
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '4', 'os_v2' => '0' }).os.should == 'Microsoft Windows NT 4.0'
71
+ end
72
+
73
+ it "should know the code name for Windows 2000" do
74
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '5', 'os_v2' => '0' }).os.should == 'Microsoft Windows 2000'
75
+ end
76
+
77
+ it "should know the code name for Windows XP" do
78
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '5', 'os_v2' => '1' }).os.should == 'Microsoft Windows XP'
79
+ end
80
+
81
+ it "should know the code name for Windows XP 64bit / Server 2003" do
82
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '5', 'os_v2' => '2' }).os.should == 'Microsoft Windows XP 64bit / Server 2003'
83
+ end
84
+
85
+ it "should know the code name for Windows Vista / Server 2008" do
86
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '6', 'os_v2' => '0' }).os.should == 'Microsoft Windows Vista / Server 2008'
87
+ end
88
+
89
+ it "should know the code name for Windows 7 / Server 2008 R2" do
90
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '6', 'os_v2' => '1' }).os.should == 'Microsoft Windows 7 / Server 2008 R2'
91
+ end
92
+
93
+ it "should know the code name for Windows 8" do
94
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows NT', 'os_v1' => '6', 'os_v2' => '2' }).os.should == 'Microsoft Windows 8'
95
+ end
96
+
97
+ it "should know the code name for Windows 3.1" do
98
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows', 'os_v1' => '3', 'os_v2' => '1' }).os.should == 'Microsoft Windows 3.1'
99
+ end
100
+
101
+ it "should know the code name for Windows 95" do
102
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows', 'os_v1' => '95' }).os.should == 'Microsoft Windows 95'
103
+ end
104
+
105
+ it "should know the code name for Windows 98" do
106
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows', 'os_v1' => '98' }).os.should == 'Microsoft Windows 98'
107
+ end
108
+
109
+ it "should know the code name for Windows ME" do
110
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows', 'os_v1' => 'ME' }).os.should == 'Microsoft Windows ME'
111
+ end
112
+
113
+ it "should know the code name for Windows 9x" do
114
+ UseragentParser::UserAgent.new({ 'os_family' => 'Windows', 'os_v1' => '9x' }).os.should == 'Microsoft Windows 9x'
115
+ end
116
+
117
+ it "should know the code name for Mac OS X 10.0" do
118
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '0' }).os.should == 'Apple Mac OS X 10.0 (Cheetah)'
119
+ end
120
+
121
+ it "should know the code name for Mac OS X 10.0" do
122
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '0', 'os_v3' => 2 }).os.should == 'Apple Mac OS X 10.0.2 (Cheetah)'
123
+ end
124
+
125
+ it "should know the code name for Mac OS X 10.1" do
126
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '1', 'os_v3' => 2 }).os.should == 'Apple Mac OS X 10.1.2 (Puma)'
127
+ end
128
+
129
+ it "should know the code name for Mac OS X 10.2" do
130
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '2', 'os_v3' => 2 }).os.should == 'Apple Mac OS X 10.2.2 (Jaguar)'
131
+ end
132
+
133
+ it "should know the code name for Mac OS X 10.3" do
134
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '3', 'os_v3' => 2 }).os.should == 'Apple Mac OS X 10.3.2 (Panther)'
135
+ end
136
+
137
+ it "should know the code name for Mac OS X 10.4" do
138
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '4', 'os_v3' => 2 }).os.should == 'Apple Mac OS X 10.4.2 (Tiger)'
139
+ end
140
+
141
+ it "should know the code name for Mac OS X 10.5" do
142
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '5', 'os_v3' => 2 }).os.should == 'Apple Mac OS X 10.5.2 (Leopard)'
143
+ end
144
+
145
+ it "should know the code name for Mac OS X 10.6" do
146
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '6', 'os_v3' => 2 }).os.should == 'Apple Mac OS X 10.6.2 (Snow Leopard)'
147
+ end
148
+
149
+ it "should know the code name for Mac OS X 10.7" do
150
+ UseragentParser::UserAgent.new({ 'os_family' => 'Mac OS X', 'os_v1' => '10', 'os_v2' => '7', 'os_v3' => '2' }).os.should == 'Apple Mac OS X 10.7.2 (Lion)'
151
+ end
152
+
153
+ it "should provide a generic os/version for other operating systems" do
154
+ UseragentParser::UserAgent.new({ 'os_family' => 'Linux', 'os_v1' => '2', 'os_v2' => '6', 'os_v3' => '32' }).os.should == 'Linux 2.6.32'
155
+ end
156
+
157
+ it "should provide a generic os name without version" do
158
+ UseragentParser::UserAgent.new({ 'os_family' => 'FreeBSD' }).os.should == 'FreeBSD'
159
+ end
160
+ end
161
+ end
@@ -1,7 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require 'useragent_parser'
5
4
 
6
5
  describe UseragentParser do
7
6
  YAML.load_file(File.expand_path(File.dirname(__FILE__)) + "/../fixtures/test_user_agent_parser.yaml")['test_cases'].each do |testcase|
@@ -11,14 +10,14 @@ describe UseragentParser do
11
10
  js_ua = [ js_ua['js_user_agent_string'], js_ua['js_user_agent_family'], js_ua['js_user_agent_v1'], js_ua['js_user_agent_v2'], js_ua['js_user_agent_v3'] ]
12
11
  end
13
12
  result = UseragentParser.parse(testcase['user_agent_string'], *js_ua)
14
- result['family'].should == testcase['family']
15
- result['v1'].should == testcase['v1']
16
- result['v2'].should == testcase['v2']
17
- result['v3'].should == testcase['v3']
18
- result['os_family'].should == testcase['os_family']
19
- result['os_v1'].should == testcase['os_v1']
20
- result['os_v2'].should == testcase['os_v2']
21
- result['os_v3'].should == testcase['os_v3']
13
+ result.browser_family.should == testcase['family']
14
+ result.browser_major_version.should == testcase['v1']
15
+ result.browser_minor_version.should == testcase['v2']
16
+ result.browser_patch_version.should == testcase['v3']
17
+ result.os_family.should == testcase['os_family']
18
+ result.os_major_version.should == testcase['os_v1']
19
+ result.os_minor_version.should == testcase['os_v2']
20
+ result.os_patch_version.should == testcase['os_v3']
22
21
  end
23
22
  end
24
23
 
@@ -29,10 +28,10 @@ describe UseragentParser do
29
28
  js_ua = eval(testcase['js_ua'].gsub("': '", "' => '")).values
30
29
  end
31
30
  result = UseragentParser.parse(testcase['user_agent_string'], *js_ua)
32
- result['family'].should == testcase['family']
33
- result['v1'].should == testcase['v1']
34
- result['v2'].should == testcase['v2']
35
- result['v3'].should == testcase['v3']
31
+ result.browser_family.should == testcase['family']
32
+ result.browser_major_version.should == testcase['v1']
33
+ result.browser_minor_version.should == testcase['v2']
34
+ result.browser_patch_version.should == testcase['v3']
36
35
  end
37
36
  end
38
37
 
@@ -43,10 +42,10 @@ describe UseragentParser do
43
42
  js_ua = eval(testcase['js_ua'].gsub("': '", "' => '")).values
44
43
  end
45
44
  result = UseragentParser.parse(testcase['user_agent_string'], *js_ua)
46
- result['family'].should == testcase['family']
47
- result['v1'].should == testcase['v1']
48
- result['v2'].should == testcase['v2']
49
- result['v3'].should == testcase['v3']
45
+ result.browser_family.should == testcase['family']
46
+ result.browser_major_version.should == testcase['v1']
47
+ result.browser_minor_version.should == testcase['v2']
48
+ result.browser_patch_version.should == testcase['v3']
50
49
  end
51
50
  end
52
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: useragent_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-22 00:00:00.000000000Z
12
+ date: 2011-09-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70355423603360 !ruby/object:Gem::Requirement
16
+ requirement: &70149734670220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.9.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70355423603360
24
+ version_requirements: *70149734670220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70355423602860 !ruby/object:Gem::Requirement
27
+ requirement: &70149734667680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.6.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70355423602860
35
+ version_requirements: *70149734667680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: multi_json
38
- requirement: &70355423602380 !ruby/object:Gem::Requirement
38
+ requirement: &70149734667040 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 1.0.1
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70355423602380
46
+ version_requirements: *70149734667040
47
47
  description: UseragentParser extracts browser and operating system information from
48
48
  the headers sent by most browsers and email clients
49
49
  email:
@@ -61,11 +61,13 @@ files:
61
61
  - config/user_agent_parser.yaml
62
62
  - lib/useragent_parser.rb
63
63
  - lib/useragent_parser/parser.rb
64
+ - lib/useragent_parser/user_agent.rb
64
65
  - lib/useragent_parser/version.rb
65
66
  - spec/fixtures/firefox_user_agent_strings.yaml
66
67
  - spec/fixtures/pgts_browser_list.yaml
67
68
  - spec/fixtures/test_user_agent_parser.yaml
68
69
  - spec/spec_helper.rb
70
+ - spec/useragent_parser/user_agent_spec.rb
69
71
  - spec/useragent_parser/useragent_parser_spec.rb
70
72
  - useragent_parser.gemspec
71
73
  homepage: https://github.com/yabawock/useragent_parser
@@ -82,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
84
  version: '0'
83
85
  segments:
84
86
  - 0
85
- hash: -4296264269086320975
87
+ hash: -1317810903120692867
86
88
  required_rubygems_version: !ruby/object:Gem::Requirement
87
89
  none: false
88
90
  requirements:
@@ -91,10 +93,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
93
  version: '0'
92
94
  segments:
93
95
  - 0
94
- hash: -4296264269086320975
96
+ hash: -1317810903120692867
95
97
  requirements: []
96
98
  rubyforge_project: useragent_parser
97
- rubygems_version: 1.8.6
99
+ rubygems_version: 1.8.10
98
100
  signing_key:
99
101
  specification_version: 3
100
102
  summary: A library to extract informtion from Useragent headers
@@ -103,4 +105,5 @@ test_files:
103
105
  - spec/fixtures/pgts_browser_list.yaml
104
106
  - spec/fixtures/test_user_agent_parser.yaml
105
107
  - spec/spec_helper.rb
108
+ - spec/useragent_parser/user_agent_spec.rb
106
109
  - spec/useragent_parser/useragent_parser_spec.rb