fivepointssolutions-uaid 0.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.
@@ -0,0 +1,49 @@
1
+ module Uaid
2
+ class UserAgent
3
+ SUPPORTED, UNSUPPORTED = true, false
4
+
5
+ attr_reader :agent, :name, :version
6
+
7
+ def initialize(agent)
8
+ @agent = agent
9
+ @name, @version, @supported = extract(@agent ? @agent.strip : '')
10
+ end
11
+
12
+ def supported?
13
+ @supported
14
+ end
15
+
16
+ def unsupported?
17
+ !supported?
18
+ end
19
+
20
+ def method_missing(method, *args)
21
+ if method.to_s =~ /^(\w+)\?$/ && EXTRACTIONS.find {|patterns, values| values.first == $1}
22
+ name == $1
23
+ else
24
+ super
25
+ end
26
+ end
27
+
28
+ protected
29
+ EXTRACTIONS = [
30
+ [[/MSIE/, /MSIE 7/], ['ie', '7', SUPPORTED]],
31
+ [[/MSIE/, /MSIE 8/], ['ie', '8', SUPPORTED]],
32
+ [[/MSIE/, /MSIE 6/], ['ie', '6', UNSUPPORTED]],
33
+ [[/iPhone/, /Version\/3/], ['mobilesafari', '3', SUPPORTED]],
34
+ [[/Safari/, /Version\/3/], ['webkit', '3', SUPPORTED]],
35
+ [[/AppleWebKit/, /525/], ['webkit', '3', SUPPORTED]],
36
+ [[/AppleWebKit/, /528/], ['webkit', '4', SUPPORTED]],
37
+ [[/Gecko/, /Firefox\/3/], ['gecko', '3', SUPPORTED]],
38
+ [[/Gecko/, /Firefox\/2|BonEcho\/2/], ['gecko', '2', SUPPORTED]],
39
+ [[/Gecko/, /Shiretoko/], ['gecko', '3.5', SUPPORTED]],
40
+ [[/googlebot|msnbot|ia_archiver/i], ['bot', 'x', SUPPORTED]],
41
+ [[/^$/], ['noagent', 'x', UNSUPPORTED]],
42
+ [[/.*/], ['unknown', 'x', UNSUPPORTED]]
43
+ ]
44
+
45
+ def extract(agent)
46
+ EXTRACTIONS.detect {|patterns,| patterns.inject(true) {|m,pattern| m && agent =~ pattern} }.last
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ module Uaid
2
+ module ViewHelper
3
+ def user_agent
4
+ @user_agent ||= UserAgent.new(request.headers['user-agent'])
5
+ end
6
+ end
7
+ end
data/lib/uaid.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'uaid/user_agent'
2
+ require 'uaid/view_helper'
3
+
4
+ module Uaid
5
+ end
data/spec/spec.opts ADDED
@@ -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,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
+
3
+ describe Uaid::UserAgent do
4
+ {
5
+ ["noagent", "x", false] => [nil, '', ' '],
6
+ ["webkit", "3", true] => [
7
+ "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",
8
+ "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.17",
9
+ "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",
10
+ "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"],
11
+ ["webkit", "4", true] => [
12
+ "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"],
13
+ ["gecko", "2", true] => [
14
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14",
15
+ "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20060601 Firefox/2.0.0.6 (Ubuntu-edgy)",
16
+ "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.4) Gecko/20080721 BonEcho/2.0.0.4"],
17
+ ["gecko", "3", true] => [
18
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1"],
19
+ ["gecko", "3.5", true] => [
20
+ "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1b3pre) Gecko/20090109 Shiretoko/3.1b3pre"],
21
+ ["ie", "8", true] => [
22
+ "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)"],
23
+ ["ie", "7", true] => [
24
+ "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)",
25
+ "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"],
26
+ ["ie", "6", false] => [
27
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"],
28
+ ["mobilesafari", "3", true] => [
29
+ "Mozilla/5.0 (iPhone; U; XXXXX like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/241 Safari/419.3"],
30
+ ["unknown", "x", false] => [
31
+ "Mozilla/4.0",
32
+ "VB Project"],
33
+ ["bot", "x", true] => [
34
+ "ia_archiver",
35
+ "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
36
+ "Googlebot/2.1 (+http://www.googlebot.com/bot.html)",
37
+ "Googlebot/2.1 (+http://www.google.com/bot.html)",
38
+ "msnbot/1.0 (+http://search.msn.com/msnbot.htm)"]
39
+ }.each do |expected_name_version_supported, agents| agents.each do |agent|
40
+
41
+ it "should indicate #{agent.inspect} as #{expected_name_version_supported[0..1].inspect}, #{expected_name_version_supported.last ? 'a supported' : 'an unsupported'} browser" do
42
+ user_agent = Uaid::UserAgent.new(agent)
43
+ user_agent.name.should == expected_name_version_supported[0]
44
+ user_agent.version.should == expected_name_version_supported[1]
45
+ user_agent.supported?.should == expected_name_version_supported[2]
46
+ end
47
+
48
+ end; end
49
+ end
50
+
51
+ describe Uaid::UserAgent, 'name question' do
52
+ it 'should answer true when agent has name' do
53
+ user_agent = Uaid::UserAgent.new('unknown')
54
+ user_agent.webkit?.should be_false
55
+ user_agent.unknown?.should be_true
56
+ end
57
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
+ require 'ostruct'
3
+
4
+ describe Uaid::ViewHelper do
5
+ include Uaid::ViewHelper
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.name.should == 'unknown'
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fivepointssolutions-uaid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A small library useful to Rack-based Ruby applications for obtaining information about the user agent
17
+ email: adam@thewilliams.ws
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/uaid
26
+ - lib/uaid/user_agent.rb
27
+ - lib/uaid/view_helper.rb
28
+ - lib/uaid.rb
29
+ - spec/spec.opts
30
+ - spec/spec_helper.rb
31
+ - spec/user_agent_spec.rb
32
+ - spec/view_helper_spec.rb
33
+ has_rdoc: false
34
+ homepage: http://github.com/fivepointssolutions/uaid
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: A small library useful to Rack-based Ruby applications for obtaining information about the user agent
59
+ test_files: []
60
+