ua-utils 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Harald Walker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # UA-UTILS
2
+
3
+ Utilities to parse user-agent strings.
4
+
5
+ First implementation is mainly focusing on most common operating systems and device types, so that content can be optimized.
6
+
7
+ ## Usage
8
+
9
+ ### Rails
10
+
11
+ In your Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ua-utils'
15
+ ```
16
+
17
+ `bundle install` and restart your server to make the files available.
18
+
19
+
20
+ ### Example
21
+
22
+ user_agent = UaUtils::UserAgent.new(user_agent_string)
23
+ or
24
+ os = UaUtils::OperatingSystem.new(user_agent_string)
25
+
26
+ ## Operating systems
27
+
28
+ Following operating systems can be detected:
29
+ windows, windows phone, mac os x, iOS, android, bada, meego, symbian
30
+
31
+ ## Devices
32
+
33
+ Following device types can be detected:
34
+ desktop, tablet, mobile, tv, bot
35
+
data/lib/ua-utils.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'ua-utils/user_agent'
2
+ require 'ua-utils/operating_system'
3
+
4
+ module UaUtils
5
+
6
+ VERSION = '0.3.3'
7
+
8
+ def self.logger
9
+ @logger ||= (rails_logger || default_logger)
10
+ end
11
+
12
+ def self.default_logger
13
+ require 'logger'
14
+ l = Logger.new(STDOUT)
15
+ l.level = Logger::INFO
16
+ l
17
+ end
18
+
19
+ def self.logger=(logger)
20
+ @logger = logger
21
+ end
22
+
23
+ end
@@ -0,0 +1,80 @@
1
+ module UaUtils
2
+
3
+ # The operating system that can be detected based on the user agent string.
4
+ #
5
+ # === Example
6
+ #
7
+ # os = UaUtils::OperatingSystem.new(
8
+ # 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4')
9
+ #
10
+ # if os.tablet?
11
+ # (... do something for tablet devices ...)
12
+ # end
13
+ #
14
+ # if os.platform == :ios
15
+ # render "/ios/show"
16
+ # end
17
+
18
+ class OperatingSystem
19
+
20
+ attr_reader :platform, :device
21
+
22
+ # Initializes operating system for parsing with the given user agent string.
23
+ def initialize(user_agent_string)
24
+ @platform, @device = detect_device(user_agent_string)
25
+ end
26
+
27
+ # Returns string representation of the operating system.
28
+ # @return [String]
29
+ def to_s
30
+ @platform.to_s
31
+ end
32
+
33
+ # Returns true if the operating system is a mobile device.
34
+ # @return [Boolean]
35
+ def mobile?
36
+ @device == :mobile
37
+ end
38
+
39
+ # Returns true if the operating system is a tablet device.
40
+ # @return [Boolean]
41
+ def tablet?
42
+ @device == :tablet
43
+ end
44
+
45
+ # Returns true if the operating system is a desktop device.
46
+ # @return [Boolean]
47
+ def desktop?
48
+ @device == :desktop
49
+ end
50
+
51
+ # Returns true if the operating system is a webTV device.
52
+ # @return [Boolean]
53
+ def webtv?
54
+ @device == :webtv
55
+ end
56
+
57
+ private
58
+
59
+ def detect_device(ua_string) #:nodoc:
60
+ case ua_string
61
+ when /(windows nt).*(touch)/i ; return :windows, :tablet
62
+ when /windows nt/i ; return :windows, :desktop
63
+ when /ipad/i ; return :ios, :tablet
64
+ when /iphone/i ; return :ios, :mobile
65
+ when /mac os x/i ; return :macosx, :desktop #after ios
66
+ when /android(?!.*kindle).*(mobile safari)/i ; return :android, :mobile
67
+ when /(android).*(safari)/i ; return :android, :tablet #after mobile android
68
+ when /windows phone/i ; return :windows_phone, :mobile
69
+ when /bada/i ; return :bada, :mobile
70
+ when /meego/i ; return :meego, :mobile
71
+ when /symbian|s60/i ; return :symbian, :mobile
72
+ when /googletv/i ; return :android, :tv
73
+ when /bot/i ; return :unknown, :bot
74
+ else ; return :unknown, :unknown
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,36 @@
1
+ module UaUtils
2
+
3
+ # The user agent object.
4
+ #
5
+ # === Example
6
+ #
7
+ # user_agent = UaUtils::UserAgent.new(
8
+ # 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4')
9
+ #
10
+ # if user_agent.os.tablet?
11
+ # (... do something for tablet devices ...)
12
+ # end
13
+ #
14
+ # if user_agent.os.platform == :ios
15
+ # render "/ios/show"
16
+ # end
17
+ #
18
+ class UserAgent
19
+
20
+ attr_reader :os
21
+
22
+ # Initializes a user agent for parsing with a given user agent string
23
+ # @param [Object] user_agent_string
24
+ def initialize(user_agent_string)
25
+ @user_agent_string = user_agent_string
26
+ end
27
+
28
+ # Returns operating system of the user agent.
29
+ # @return [UaUtils::OperatingSystem] the detected operating system of the user agent string
30
+ def os
31
+ @os ||= UaUtils::OperatingSystem.new(@user_agent_string) # operating system
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,3 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib/ua-utils')
2
+ require 'user_agent'
3
+ require 'operating_system'
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+
3
+ module UaUtils
4
+ describe 'UserAgent' do
5
+
6
+ describe 'detects mobile devices' do
7
+
8
+ it 'recognizes iOS phones' do
9
+ test([
10
+ 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; nl-nl) AppleWebKit/420.1 (KHTML, like Gecko)',
11
+ 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko)',
12
+ 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3'
13
+ ],
14
+ { platform: :ios, device: :mobile })
15
+
16
+ end
17
+
18
+ it 'recognizes Android phones' do
19
+ test([
20
+ 'Mozilla/5.0 (Linux; U; Android 2.0; en-gb; Milestone Build/SHOLS_U2_01.03.1) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17',
21
+ 'Mozilla/5.0 (Linux; U; Android 4.0.1; en-us; Galaxy Nexus Build/ICL41) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
22
+ ],
23
+ { platform: :android, device: :mobile })
24
+ end
25
+
26
+ it 'recognizes Windows phones' do
27
+ test([
28
+ 'Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0) Asus;Galaxy6',
29
+ 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)',
30
+ 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)',
31
+ 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 820)'
32
+ ],
33
+ { platform: :windows_phone, device: :mobile })
34
+ end
35
+
36
+ it 'recognizes Samsung bada phones' do
37
+ test([
38
+ 'Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8500/S8500NEJE5; U; Bada/1.0; fr-fr) AppleWebKit/533.1 (KHTML, like Gecko) Dolfin/2.0 Mobile WVGA SMM-MMS/1.2.0 NexPlayer/3.0 profile/MIDP-2.1 configuration/CLDC-1.1 OPN-B',
39
+ 'Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8500/S8500XXJL2; U; Bada/1.2; de-de) AppleWebKit/533.1 (KHTML, like Gecko) Dolfin/2.2 Mobile WVGA SMM-MMS/1.2.0 OPN-B'
40
+ ],
41
+ { platform: :bada, device: :mobile })
42
+ end
43
+
44
+ it 'recognizes Nokia MeeGo phones' do
45
+ test([
46
+ 'Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13'
47
+ ],
48
+ { platform: :meego, device: :mobile })
49
+ end
50
+
51
+ it 'recognizes Symbian phones' do
52
+ test([
53
+ 'Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95/10.0.018; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413',
54
+ 'NokiaN80-3/1.0552.0.7Series60/3.0Profile/MIDP-2.0Configuration/CLDC-1.1',
55
+ 'NokiaN90-1/3.0545.5.1 Series60/2.8 Profile/MIDP-2.0 Configuration/CLDC-1.1'
56
+ ],
57
+ { platform: :symbian, device: :mobile })
58
+ end
59
+
60
+ end
61
+
62
+ describe 'detects tablet devices' do
63
+ it 'recognized iPads' do
64
+ test([
65
+ 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10',
66
+ 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10',
67
+ 'Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5',
68
+ 'Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3' ],
69
+ { platform: :ios, device: :tablet })
70
+ end
71
+
72
+ it 'recognized popular android tablets' do
73
+ test([
74
+ 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13',
75
+ 'Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HRI66) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13',
76
+ 'Mozilla/5.0 (Linux; U; Android 3.1; en-us; GT-P7510 Build/HMJ37) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13',
77
+ 'Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; Transformer TF101 Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30',
78
+ 'Mozilla/5.0 (Linux; U; Android-4.0.3; en-us; Xoom Build/IML77) AppleWebKit/535.7 (KHTML, like Gecko) CrMo/16.0.912.75 Safari/535.7',
79
+ 'Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; ASUS Transformer Pad TF700T Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30',
80
+ 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Kindle Fire Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'
81
+ ],
82
+ { platform: :android, device: :tablet })
83
+ end
84
+
85
+ it 'recognized windows 8 devices with touch support' do
86
+ test([
87
+ 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)'
88
+ ],{ platform: :windows, device: :tablet })
89
+ end
90
+ end
91
+
92
+ describe 'detect web-tv devices' do
93
+
94
+ it 'recognizes Google TV devices' do
95
+ test([
96
+ 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Large Screen Safari/533.4 GoogleTV/161242',
97
+ 'Mozilla/5.0 (X11; U: Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Large Screen Safari/533.4 GoogleTV/162671', # Sony
98
+ 'Mozilla/5.0 (X11; U: Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.127 Large Screen Safari/533.4 GoogleTV/b39389' #Logitech Revue
99
+ ],
100
+ { platform: :android, device: :tv })
101
+ end
102
+
103
+ end
104
+
105
+ describe 'detect desktop devices' do
106
+
107
+ it 'recognizes Windows computers' do
108
+ test([
109
+ 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 ( .NET CLR 3.5.30729)',
110
+ 'Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.2; WOW64; .NET CLR 2.0.50727)',
111
+ 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506)',
112
+ 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC; MSOffice 12)',
113
+ 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Media Center PC 5.0; SLCC1; InfoPath.2)'
114
+ ],
115
+ { platform: :windows, device: :desktop })
116
+ end
117
+
118
+ it 'recognizes Mac OS X computers' do
119
+ test([
120
+ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4',
121
+ 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; da-dk) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1'
122
+ ],
123
+ { platform: :macosx, device: :desktop })
124
+ end
125
+ end
126
+
127
+ # helper method to perform the tests for user-agent strings
128
+ def test(user_agent_strings, expect = { })
129
+ user_agent_strings.each do |user_agent_string|
130
+ user_agent = UserAgent.new(user_agent_string)
131
+ user_agent.os.platform.should == expect[:platform]
132
+ case expect[:device]
133
+ when :mobile
134
+ user_agent.os.mobile?.should be_true
135
+ user_agent.os.tablet?.should_not be_true
136
+ user_agent.os.desktop?.should_not be_true
137
+ user_agent.os.webtv?.should_not be_true
138
+ when :tablet
139
+ user_agent.os.tablet?.should be_true
140
+ user_agent.os.mobile?.should_not be_true
141
+ user_agent.os.desktop?.should_not be_true
142
+ user_agent.os.webtv?.should_not be_true
143
+ when :desktop
144
+ user_agent.os.desktop?.should be_true
145
+ user_agent.os.mobile?.should_not be_true
146
+ user_agent.os.tablet?.should_not be_true
147
+ user_agent.os.webtv?.should_not be_true
148
+ when :webtv
149
+ user_agent.os.webtv?.should be_true
150
+ user_agent.os.mobile?.should_not be_true
151
+ user_agent.os.tablet?.should_not be_true
152
+ user_agent.os.desktop?.should_not be_true
153
+ end
154
+ user_agent.os.device.should == expect[:device]
155
+ end
156
+ end
157
+
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ua-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ! '''Harald'
9
+ - Walker'
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-03-23 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Utility for parsing and handling user-agent strings.
48
+ email:
49
+ - ! '''harald.walker@gmail.com'''
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/ua-utils/operating_system.rb
55
+ - lib/ua-utils/user_agent.rb
56
+ - lib/ua-utils.rb
57
+ - spec/spec_helper.rb
58
+ - spec/ua-utils/user_agent_spec.rb
59
+ - LICENSE
60
+ - README.md
61
+ homepage: https://github.com/HaraldWalker/ua-utils
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '1.9'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.24
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Utility for parsing and handling user-agent strings.
85
+ test_files:
86
+ - spec/spec_helper.rb
87
+ - spec/ua-utils/user_agent_spec.rb
88
+ has_rdoc: