josh-useragent 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Joshua Peek
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,21 @@
1
+ UserAgent
2
+ =========
3
+
4
+ UserAgent is a Ruby library that parses and compares HTTP User Agents.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Browser = Struct.new(:browser, :version)
11
+ SupportedBrowsers = [
12
+ Browser.new("Safari", "3.1.1"),
13
+ Browser.new("Firefox", "2.0.0.14"),
14
+ Browser.new("Internet Explorer", "7.0")
15
+ ]
16
+
17
+ user_agent = UserAgent.parse(request.user_agent)
18
+ SupportedBrowsers.detect { |browser| user_agent >= browser }
19
+
20
+
21
+ Copyright (c) 2008 Joshua Peek, released under the MIT license
data/lib/user_agent.rb ADDED
@@ -0,0 +1,81 @@
1
+ require "#{File.dirname(__FILE__)}/user_agent/comparable"
2
+ require "#{File.dirname(__FILE__)}/user_agent/browsers"
3
+ require "#{File.dirname(__FILE__)}/user_agent/operating_systems"
4
+
5
+ class UserAgent
6
+ # http://www.texsoft.it/index.php?m=sw.php.useragent
7
+ MATCHER = %r{
8
+ ^([^/\s]+) # Product
9
+ /?([^\s]*) # Version
10
+ (\s\(([^\)]*)\))? # Comment
11
+ }x.freeze unless defined? MATCHER
12
+
13
+ def self.parse(string)
14
+ agents = []
15
+ while m = string.match(MATCHER)
16
+ agent = new(m[1], m[2], m[4])
17
+ agents << agent
18
+ string = string.sub(agent.to_s, '').strip
19
+ end
20
+ Browsers.extend(agents)
21
+ agents
22
+ end
23
+
24
+ attr_reader :product, :version, :comment
25
+
26
+ def initialize(product, version = nil, comment = nil)
27
+ if product
28
+ @product = product
29
+ else
30
+ raise ArgumentError, "expected a value for product"
31
+ end
32
+
33
+ if version && !version.empty?
34
+ @version = version
35
+ end
36
+
37
+ if comment.respond_to?(:split)
38
+ @comment = comment.split("; ")
39
+ else
40
+ @comment = comment
41
+ end
42
+ end
43
+
44
+ include Comparable
45
+
46
+ # Any comparsion between two user agents with different products will
47
+ # always return false.
48
+ def <=>(other)
49
+ if @product == other.product
50
+ if @version && other.version
51
+ @version <=> other.version
52
+ else
53
+ 0
54
+ end
55
+ else
56
+ false
57
+ end
58
+ end
59
+
60
+ def eql?(other)
61
+ @product == other.product &&
62
+ @version == other.version &&
63
+ @comment == other.comment
64
+ end
65
+
66
+ def to_s
67
+ to_str
68
+ end
69
+
70
+ def to_str
71
+ if @product && @version && @comment
72
+ "#{@product}/#{@version} (#{@comment.join("; ")})"
73
+ elsif @product && @version
74
+ "#{@product}/#{@version}"
75
+ elsif @product && @comment
76
+ "#{@product} (#{@comment.join("; ")})"
77
+ else
78
+ @product
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,24 @@
1
+ Dir["#{File.dirname(__FILE__)}/browsers/*.rb"].each do |browser|
2
+ require browser
3
+ end
4
+
5
+ class UserAgent
6
+ module Browsers
7
+ Security = {
8
+ "N" => :none,
9
+ "U" => :strong,
10
+ "I" => :weak
11
+ }.freeze unless defined? Security
12
+
13
+ def self.all
14
+ [InternetExplorer, Webkit, Opera, Gecko]
15
+ end
16
+
17
+ def self.extend(array)
18
+ array.extend(All)
19
+ all.each do |extension|
20
+ return array.extend(extension) if extension.extend?(array)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,53 @@
1
+ class UserAgent
2
+ module Browsers
3
+ module All
4
+ include Comparable
5
+
6
+ def <=>(other)
7
+ if respond_to?(:browser) && other.respond_to?(:browser) &&
8
+ browser == other.browser
9
+ version <=> other.version
10
+ else
11
+ false
12
+ end
13
+ end
14
+
15
+ def eql?(other)
16
+ self == other
17
+ end
18
+
19
+ def to_s
20
+ to_str
21
+ end
22
+
23
+ def to_str
24
+ join(" ")
25
+ end
26
+
27
+ def application
28
+ first
29
+ end
30
+
31
+ def browser
32
+ application.product
33
+ end
34
+
35
+ def version
36
+ application.version
37
+ end
38
+
39
+ def respond_to?(symbol)
40
+ detect_product(symbol) ? true : super
41
+ end
42
+
43
+ def method_missing(method, *args, &block)
44
+ detect_product(method) || super
45
+ end
46
+
47
+ private
48
+ def detect_product(product)
49
+ detect { |useragent| useragent.product.to_s.downcase == product.to_s.downcase }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,38 @@
1
+ class UserAgent
2
+ module Browsers
3
+ module Gecko
4
+ def self.extend?(agent)
5
+ agent.application && agent.application.product == "Mozilla"
6
+ end
7
+
8
+ GeckoBrowsers = %w(
9
+ Firefox
10
+ Camino
11
+ ).freeze unless defined? GeckoBrowsers
12
+
13
+ def browser
14
+ GeckoBrowsers.detect { |browser| respond_to?(browser) } || super
15
+ end
16
+
17
+ def version
18
+ send(browser).version || super
19
+ end
20
+
21
+ def platform
22
+ application.comment[0]
23
+ end
24
+
25
+ def security
26
+ Security[application.comment[1]]
27
+ end
28
+
29
+ def os
30
+ OperatingSystems.normalize_os(application.comment[2])
31
+ end
32
+
33
+ def localization
34
+ application.comment[3]
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ class UserAgent
2
+ module Browsers
3
+ module InternetExplorer
4
+ def self.extend?(agent)
5
+ agent.application &&
6
+ agent.application.comment &&
7
+ agent.application.comment[0] == "compatible"
8
+ end
9
+
10
+ def browser
11
+ "Internet Explorer"
12
+ end
13
+
14
+ def version
15
+ application.comment[1].sub("MSIE ", "")
16
+ end
17
+
18
+ def compatibility
19
+ application.comment[0]
20
+ end
21
+
22
+ def compatible?
23
+ compatibility == "compatible"
24
+ end
25
+
26
+ def platform
27
+ "Windows"
28
+ end
29
+
30
+ def os
31
+ OperatingSystems.normalize_os(application.comment[2])
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ class UserAgent
2
+ module Browsers
3
+ module Opera
4
+ def self.extend?(agent)
5
+ agent.application && agent.application.product == "Opera"
6
+ end
7
+
8
+ def platform
9
+ if application.comment[0] =~ /Windows/
10
+ "Windows"
11
+ else
12
+ application.comment[0]
13
+ end
14
+ end
15
+
16
+ def security
17
+ if platform == "Macintosh"
18
+ Security[application.comment[2]]
19
+ else
20
+ Security[application.comment[1]]
21
+ end
22
+ end
23
+
24
+ def os
25
+ if application.comment[0] =~ /Windows/
26
+ OperatingSystems.normalize_os(application.comment[0])
27
+ else
28
+ application.comment[1]
29
+ end
30
+ end
31
+
32
+ def localization
33
+ if platform == "Macintosh"
34
+ application.comment[3]
35
+ else
36
+ application.comment[2]
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,62 @@
1
+ class UserAgent
2
+ module Browsers
3
+ module Webkit
4
+ def self.extend?(agent)
5
+ agent.detect { |useragent| useragent.product == "Safari" }
6
+ end
7
+
8
+ def browser
9
+ if detect_product("Chrome")
10
+ "Chrome"
11
+ else
12
+ "Safari"
13
+ end
14
+ end
15
+
16
+ def build
17
+ safari.version
18
+ end
19
+
20
+ BuildVersions = {
21
+ "125.12" => "1.2.4",
22
+ "312.6" => "1.3.2",
23
+ "412.2.2" => "2.0",
24
+ "412.5" => "2.0.1",
25
+ "416.13" => "2.0.2",
26
+ "417.9.3" => "2.0.3",
27
+ "419.3" => "2.0.4"
28
+ }.freeze unless defined? BuildVersions
29
+
30
+ # Prior to Safari 3, the user agent did not include a version number
31
+ def version
32
+ if browser == "Chrome"
33
+ chrome.version
34
+ elsif product = detect_product("Version")
35
+ product.version
36
+ else
37
+ BuildVersions[build]
38
+ end
39
+ end
40
+
41
+ def platform
42
+ application.comment[0]
43
+ end
44
+
45
+ def webkit
46
+ detect { |useragent| useragent.product == "AppleWebKit" }
47
+ end
48
+
49
+ def security
50
+ Security[application.comment[1]]
51
+ end
52
+
53
+ def os
54
+ OperatingSystems.normalize_os(application.comment[2])
55
+ end
56
+
57
+ def localization
58
+ application.comment[3]
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,25 @@
1
+ class UserAgent
2
+ # A custom Comparable module that will always return false
3
+ # if the <=> returns false
4
+ module Comparable
5
+ def <(other)
6
+ (c = self <=> other) ? c == -1 : false
7
+ end
8
+
9
+ def <=(other)
10
+ (c = self <=> other) ? c == -1 || c == 0 : false
11
+ end
12
+
13
+ def ==(other)
14
+ (c = self <=> other) ? c == 0 : false
15
+ end
16
+
17
+ def >(other)
18
+ (c = self <=> other) ? c == 1 : false
19
+ end
20
+
21
+ def >=(other)
22
+ (c = self <=> other) ? c == 1 || c == 0 : false
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ class UserAgent
2
+ module OperatingSystems
3
+ Windows = {
4
+ "Windows NT 6.0" => "Windows Vista",
5
+ "Windows NT 5.2" => "Windows XP x64 Edition",
6
+ "Windows NT 5.1" => "Windows XP",
7
+ "Windows NT 5.01" => "Windows 2000, Service Pack 1 (SP1)",
8
+ "Windows NT 5.0" => "Windows 2000",
9
+ "Windows NT 4.0" => "Windows NT 4.0",
10
+ "Windows 98" => "Windows 98",
11
+ "Windows 95" => "Windows 95",
12
+ "Windows CE" => "Windows CE"
13
+ }.freeze unless defined? Windows
14
+
15
+ def self.normalize_os(os)
16
+ Windows[os] || os
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: josh-useragent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Peek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-30 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: UserAgent is a Ruby library that parses and compares HTTP User Agents.
17
+ email: josh@joshpeek.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - MIT-LICENSE
27
+ - lib/user_agent
28
+ - lib/user_agent/browsers.rb
29
+ - lib/user_agent/operating_systems.rb
30
+ - lib/user_agent/comparable.rb
31
+ - lib/user_agent/browsers
32
+ - lib/user_agent/browsers/webkit.rb
33
+ - lib/user_agent/browsers/gecko.rb
34
+ - lib/user_agent/browsers/internet_explorer.rb
35
+ - lib/user_agent/browsers/all.rb
36
+ - lib/user_agent/browsers/opera.rb
37
+ - lib/user_agent.rb
38
+ has_rdoc: false
39
+ homepage: http://github.com/josh/useragent
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: HTTP User Agent parser
64
+ test_files: []
65
+