uaid 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ pkg/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Five Points Solutions, Inc.
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,10 @@
1
+ uaid
2
+ =========
3
+
4
+ A small library useful to Rack-based Ruby applications for obtaining
5
+ information about the user agent.
6
+
7
+ COPYRIGHT
8
+ =========
9
+
10
+ Copyright (c) 2009 Five Points Solutions, Inc. See LICENSE for details.
@@ -0,0 +1,34 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+ require 'spec/rake/spectask'
4
+
5
+ task :default => :spec
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |s|
10
+ s.name = "uaid"
11
+ s.summary = "A small library useful to Rack-based Ruby applications for obtaining information about the user agent"
12
+ s.email = "adam@thewilliams.ws"
13
+ s.homepage = "http://github.com/fivepointssolutions/uaid"
14
+ s.description = s.summary
15
+ s.authors = ["Adam Williams"]
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
20
+ end
21
+
22
+ desc "Run all specs"
23
+ Spec::Rake::SpecTask.new do |t|
24
+ t.spec_files = FileList['spec/**/*_spec.rb']
25
+ t.spec_opts = ['--options', 'spec/spec.opts']
26
+ end
27
+
28
+ Rake::RDocTask.new do |rdoc|
29
+ rdoc.rdoc_dir = 'rdoc'
30
+ rdoc.title = 'uaid'
31
+ rdoc.options << '--line-numbers' << '--inline-source'
32
+ rdoc.rdoc_files.include('README*')
33
+ rdoc.rdoc_files.include('lib/**/*.rb')
34
+ end
@@ -0,0 +1,5 @@
1
+ ---
2
+ :patch: 3
3
+ :build:
4
+ :major: 0
5
+ :minor: 1
@@ -0,0 +1,6 @@
1
+ require 'uaid/extractor'
2
+ require 'uaid/user_agent'
3
+ require 'uaid/helper'
4
+
5
+ module Uaid
6
+ end
@@ -0,0 +1,116 @@
1
+ module Uaid
2
+ def Uaid.extractor
3
+ @extractor ||= Extractor.new
4
+ end
5
+
6
+ def Uaid.extractor=(e)
7
+ @extractor = e
8
+ end
9
+
10
+ class Extractor
11
+ def engines
12
+ @engines ||= %w(ie webkit gecko)
13
+ end
14
+
15
+ def products
16
+ @products ||= product_extractions.collect {|product,| product}
17
+ end
18
+
19
+ def extract(agent)
20
+ product = extract_product(agent)
21
+ engine = extract_engine(agent, product)
22
+ version = extract_version(agent, product, engine)
23
+ [engine, product, version]
24
+ end
25
+
26
+ protected
27
+ def extract_product(agent)
28
+ find_match_in_extractions(agent, product_extractions) || 'unknown'
29
+ end
30
+
31
+ def extract_engine(agent, product)
32
+ case extractions = engine_extractions(product)
33
+ when String: extractions
34
+ when Array: find_match_in_extractions(agent, extractions) || 'unknown'
35
+ when nil: 'unknown'
36
+ end
37
+ end
38
+
39
+ def extract_version(agent, product, engine)
40
+ case extractions = version_extractions(product)
41
+ when Array
42
+ match = extractions.detect {|version, regexp| agent =~ regexp}
43
+ match ? match[0] : 'x'
44
+ when nil: 'x'
45
+ end
46
+ end
47
+
48
+ def product_extractions
49
+ @product_extractions ||= [
50
+ ['ie', [/MSIE/]],
51
+ ['mobilesafari', [/iPhone.*?Version.*?Safari/]],
52
+ ['safari', [/Version.*?Safari/]],
53
+ ['chrome', [/Chrome.*?Safari/]],
54
+ ['firefox', [/Firefox|BonEcho|Shiretoko/]],
55
+ ['android', [/Android/]],
56
+ ['bot', [/googlebot|msnbot|ia_archiver/i]],
57
+ ['noagent', [/^$/]],
58
+ ]
59
+ end
60
+
61
+ def engine_extractions(product)
62
+ @engine_extractions ||= {
63
+ 'ie' => 'ie',
64
+ 'safari' => 'webkit',
65
+ 'mobilesafari' => 'webkit',
66
+ 'chrome' => 'webkit',
67
+ 'firefox' => 'gecko',
68
+ 'android' => 'webkit',
69
+ }
70
+ @engine_extractions[product]
71
+ end
72
+
73
+ def version_extractions(product)
74
+ @version_extractions ||= {
75
+ 'ie' => [
76
+ ['7', /MSIE 7/],
77
+ ['8', /MSIE 8/],
78
+ ['6', /MSIE 6/],
79
+ ],
80
+ 'firefox' => [
81
+ ['3', /Firefox\/3|Shiretoko/],
82
+ ['2', /Firefox\/2|BonEcho\/2/],
83
+ ],
84
+ 'safari' => [
85
+ ['3', /Version\/3/],
86
+ ['4', /Version\/4/],
87
+ ['5', /Version\/5/],
88
+ ],
89
+ 'mobilesafari' => [
90
+ ['3', /Version\/3/],
91
+ ],
92
+ 'chrome' => [
93
+ ['1', /Chrome\/[01]\./],
94
+ ],
95
+ 'android' => [
96
+ ['0', /Android 0\./],
97
+ ]
98
+ }
99
+ @version_extractions[product]
100
+ end
101
+
102
+ # Answers the first matching extraction. Keep this in mind when adding
103
+ # patterns!
104
+ #
105
+ def find_match_in_extractions(agent, extractions)
106
+ match = nil
107
+ extractions.each do |answer, patterns|
108
+ matches = patterns.inject(true) {|m,pattern| m && agent =~ pattern}
109
+ next unless matches
110
+ match = answer
111
+ break
112
+ end
113
+ match
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,7 @@
1
+ module Uaid
2
+ module Helper
3
+ def user_agent
4
+ @user_agent ||= UserAgent.new(request.headers['user-agent'])
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,61 @@
1
+ module Uaid
2
+ def Uaid.supported_agents
3
+ @supported ||= [
4
+ /safari [345]/,
5
+ /chrome/,
6
+ /firefox [23]/,
7
+ /ie [678]/,
8
+ /android/,
9
+ /bot/
10
+ ]
11
+ end
12
+
13
+ # Define the set of supported agents. You may also concat to the existing
14
+ # list.
15
+ #
16
+ def Uaid.supported_agents=(s)
17
+ @supported = s
18
+ end
19
+
20
+ class UserAgent
21
+ attr_reader :agent, :engine, :product, :version, :supported
22
+
23
+ def initialize(agent, extractor = Uaid.extractor)
24
+ @agent, @extractor = agent, extractor
25
+ @engine, @product, @version = @extractor.extract(@agent ? @agent.strip : '')
26
+ end
27
+
28
+ def identifier
29
+ [engine, product, product + version].join(' ')
30
+ end
31
+
32
+ def supported?
33
+ !!Uaid.supported_agents.detect do |agent_match|
34
+ case agent_match
35
+ when Array: agent_match == [engine, product, version]
36
+ when Regexp: [engine, product, version].join(' ') =~ agent_match
37
+ end
38
+ end
39
+ end
40
+
41
+ def unknown?
42
+ engine == 'unknown' || product == 'unknown' || version == 'x'
43
+ end
44
+
45
+ def unsupported?
46
+ !supported?
47
+ end
48
+
49
+ def version?(e)
50
+ version == e
51
+ end
52
+
53
+ def method_missing(method, *args)
54
+ if method.to_s =~ /^(\w+)\?$/
55
+ engine == $1 || product == $1
56
+ else
57
+ super
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
+
3
+ describe Uaid::Extractor do
4
+ before :all do
5
+ @extractor = Uaid::Extractor.new
6
+ end
7
+
8
+ {
9
+ ["unknown", "unknown", "x"] => [nil, '', ' '],
10
+ ["webkit", "safari", "3"] => [
11
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_2; en-us) AppleWebKit/526+ (KHTML, like Gecko) Version/3.1.1 Safari/525.18",
12
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.17"],
13
+ ["webkit", "chrome", "1"] => [
14
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13",
15
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.36 Safari/525.19"],
16
+ ["webkit", "safari", "4"] => [
17
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16"],
18
+ ["webkit", "safari", "5"] => [
19
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16"],
20
+ ["gecko", "firefox", "2"] => [
21
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14",
22
+ "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20060601 Firefox/2.0.0.6 (Ubuntu-edgy)",
23
+ "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.4) Gecko/20080721 BonEcho/2.0.0.4"],
24
+ ["gecko", "firefox", "3"] => [
25
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1",
26
+ "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1b3pre) Gecko/20090109 Shiretoko/3.1b3pre"],
27
+ ["ie", "ie", "8"] => [
28
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0; .NET CLR 1.1.4322)"],
29
+ ["ie", "ie", "7"] => [
30
+ "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; InfoPath.1)",
31
+ "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"],
32
+ ["ie", "ie", "6"] => [
33
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"],
34
+ ["webkit", "mobilesafari", "3"] => [
35
+ "Mozilla/5.0 (iPhone; U; XXXXX like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/241 Safari/419.3"],
36
+ ["webkit", "android", "0"] => [
37
+ "Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3"],
38
+ ["unknown", "unknown", "x"] => [
39
+ "Mozilla/4.0",
40
+ "VB Project"],
41
+ ["unknown", "bot", "x"] => [
42
+ "ia_archiver",
43
+ "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
44
+ "Googlebot/2.1 (+http://www.googlebot.com/bot.html)",
45
+ "Googlebot/2.1 (+http://www.google.com/bot.html)",
46
+ "msnbot/1.0 (+http://search.msn.com/msnbot.htm)"]
47
+ }.each do |expected, agents| agents.each do |agent|
48
+
49
+ it "should identify #{agent.inspect} as #{expected.inspect}" do
50
+ @extractor.extract(agent).should == expected
51
+ end
52
+
53
+ end; end
54
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
+ require 'ostruct'
3
+
4
+ describe Uaid::Helper do
5
+ include Uaid::Helper
6
+
7
+ attr_reader :request
8
+
9
+ it 'should provide a user_agent from the current request' do
10
+ @request = OpenStruct.new(:headers => {'user-agent' => 'whatever'})
11
+ user_agent.should_not be_nil
12
+ user_agent.product.should == 'unknown'
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ --backtrace
@@ -0,0 +1,12 @@
1
+ SPEC_ROOT = File.expand_path(File.dirname(__FILE__))
2
+ $LOAD_PATH << (SPEC_ROOT + '/../lib')
3
+
4
+ require 'rubygems'
5
+ require 'rr'
6
+ require 'spec'
7
+
8
+ Spec::Runner.configure do |config|
9
+ config.mock_with :rr
10
+ end
11
+
12
+ require 'uaid'
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
+
3
+ describe Uaid::UserAgent, 'identification questions' do
4
+ before do
5
+ @user_agent = Uaid::UserAgent.new('unknown')
6
+ end
7
+
8
+ after do
9
+ Uaid.supported_agents = nil
10
+ end
11
+
12
+ [
13
+ ['unknown', 'bot', '1'],
14
+ ['webkit', 'unknown', '1'],
15
+ ['webkit', 'bot', 'x'],
16
+ ].each do |attributes|
17
+ engine, product, version = attributes
18
+ it "should answer true for unknown when unknown as #{attributes.inspect}" do
19
+ user_agent = Uaid::UserAgent.new('unknown')
20
+ stub(@user_agent).engine { engine }
21
+ stub(@user_agent).product { product }
22
+ stub(@user_agent).version { version }
23
+ @user_agent.unknown?.should be_true
24
+ end
25
+ end
26
+
27
+ it 'should answer false for unknown when all attributes are known' do
28
+ stub(@user_agent).engine { 'someengine' }
29
+ stub(@user_agent).product { 'someproduct' }
30
+ stub(@user_agent).version { '1' }
31
+ @user_agent.unknown?.should be_false
32
+ end
33
+
34
+ it 'should answer for engine' do
35
+ stub(@user_agent).engine { 'someengine' }
36
+ @user_agent.someengine?.should be_true
37
+ @user_agent.otherengine?.should be_false
38
+ end
39
+
40
+ it 'should answer for product' do
41
+ stub(@user_agent).product { 'someproduct' }
42
+ @user_agent.someproduct?.should be_true
43
+ @user_agent.otherproduct?.should be_false
44
+
45
+ stub(@user_agent).product { 'noagent' }
46
+ @user_agent.bot?.should be_false
47
+ end
48
+
49
+ it 'should answer for version' do
50
+ stub(@user_agent).version { '2' }
51
+ @user_agent.version?('2').should be_true
52
+ end
53
+
54
+ it 'should answer an identifier which include the engine, product and version' do
55
+ stub(@user_agent).engine { 'someengine' }
56
+ stub(@user_agent).product { 'someproduct' }
57
+ stub(@user_agent).version { '1' }
58
+ @user_agent.identifier.should == 'someengine someproduct someproduct1'
59
+ end
60
+
61
+ it 'should answer supported as true when all attributes match a supported agent' do
62
+ stub(@user_agent).engine { 'webkit' }
63
+ stub(@user_agent).product { 'safari' }
64
+ stub(@user_agent).version { '3' }
65
+ @user_agent.should be_supported
66
+ end
67
+
68
+ it 'should answer supported when an entry in the supported list is a regular expression' do
69
+ Uaid.supported_agents = [/safari [34]/]
70
+ stub(@user_agent).product { 'safari' }
71
+ stub(@user_agent).version { '3' }
72
+ @user_agent.should be_supported
73
+
74
+ stub(@user_agent).version { '4' }
75
+ @user_agent.should be_supported
76
+
77
+ stub(@user_agent).version { '2' }
78
+ @user_agent.should_not be_supported
79
+ end
80
+ end
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{uaid}
8
+ s.version = "0.1.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Adam Williams"]
12
+ s.date = %q{2010-06-17}
13
+ s.description = %q{A small library useful to Rack-based Ruby applications for obtaining information about the user agent}
14
+ s.email = %q{adam@thewilliams.ws}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION.yml",
25
+ "lib/uaid.rb",
26
+ "lib/uaid/extractor.rb",
27
+ "lib/uaid/helper.rb",
28
+ "lib/uaid/user_agent.rb",
29
+ "spec/extractor_spec.rb",
30
+ "spec/helper_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb",
33
+ "spec/user_agent_spec.rb",
34
+ "uaid.gemspec"
35
+ ]
36
+ s.homepage = %q{http://github.com/fivepointssolutions/uaid}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.6}
40
+ s.summary = %q{A small library useful to Rack-based Ruby applications for obtaining information about the user agent}
41
+ s.test_files = [
42
+ "spec/extractor_spec.rb",
43
+ "spec/helper_spec.rb",
44
+ "spec/spec_helper.rb",
45
+ "spec/user_agent_spec.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ else
54
+ end
55
+ else
56
+ end
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uaid
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 3
9
+ version: 0.1.3
10
+ platform: ruby
11
+ authors:
12
+ - Adam Williams
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-17 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A small library useful to Rack-based Ruby applications for obtaining information about the user agent
22
+ email: adam@thewilliams.ws
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README
30
+ files:
31
+ - .gitignore
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - VERSION.yml
36
+ - lib/uaid.rb
37
+ - lib/uaid/extractor.rb
38
+ - lib/uaid/helper.rb
39
+ - lib/uaid/user_agent.rb
40
+ - spec/extractor_spec.rb
41
+ - spec/helper_spec.rb
42
+ - spec/spec.opts
43
+ - spec/spec_helper.rb
44
+ - spec/user_agent_spec.rb
45
+ - uaid.gemspec
46
+ has_rdoc: true
47
+ homepage: http://github.com/fivepointssolutions/uaid
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.6
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A small library useful to Rack-based Ruby applications for obtaining information about the user agent
76
+ test_files:
77
+ - spec/extractor_spec.rb
78
+ - spec/helper_spec.rb
79
+ - spec/spec_helper.rb
80
+ - spec/user_agent_spec.rb