Adding new user-agent gem.

unstable
Samuel Huckins 2012-09-13 12:52:33 -05:00
parent caf7619b86
commit 578b507dc7
16 changed files with 480 additions and 0 deletions

View File

@ -0,0 +1,5 @@
source :gemcutter
gem "rake"
gem "echoe"
gem "rspec"

View File

@ -0,0 +1,12 @@
=== 2010-05-07
* Support for Firefox
=== 1.0.0 / 2009-10-08
* Specs for Agent#==
=== 0.0.1 / 2009-08-28
* Initial release

View File

@ -0,0 +1,16 @@
Gemfile
History.rdoc
Manifest
README.rdoc
Rakefile
lib/user-agent.rb
lib/user-agent/agent.rb
lib/user-agent/version.rb
spec/agent_spec.rb
spec/agents_spec.rb
spec/spec.opts
spec/spec_helper.rb
tasks/docs.rake
tasks/gemspec.rake
tasks/spec.rake
user-agent.gemspec

View File

@ -0,0 +1,49 @@
= User Agent
User agent parser.
== Example
agent = Agent.new 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9'
agent.name # => :Safari
agent.version # => '4.0.3'
agent.engine # => :webkit
agent.os # => :'Windows Vista'
agent.engine_version # => '531.9'
== Supported Agents
* Safari
* Chrome
* Opera
* IE
* Konqueror
* PS3
* PSP
* Wii
== License:
(The MIT License)
Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, an d/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,15 @@
$:.unshift 'lib'
require 'user-agent'
require 'rubygems'
require 'rake'
require 'echoe'
Echoe.new "user-agent", Agent::VERSION do |p|
p.author = "TJ Holowaychuk"
p.email = "tj@vision-media.ca"
p.summary = "User agent parser"
p.runtime_dependencies = []
end
Dir['tasks/**/*.rake'].sort.each { |f| load f }

View File

@ -0,0 +1,25 @@
#--
# Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
require 'user-agent/agent'
require 'user-agent/version'

View File

@ -0,0 +1,161 @@
class Agent
##
# User agent string.
attr_reader :string
##
# Initialize with user agent _string_.
def initialize string
@string = string.strip
end
#--
# Instance methods
#++
##
# User agent name symbol.
def name
Agent.name_for_user_agent string
end
##
# User agent version.
def version
Agent.version_for_user_agent string
end
##
# User agent engine symbol.
def engine
Agent.engine_for_user_agent string
end
##
# User agent engine version string.
def engine_version
Agent.engine_version_for_user_agent string
end
##
# User agent os symbol.
def os
Agent.os_for_user_agent string
end
##
# User agent string.
def to_s
string
end
##
# Inspect.
def inspect
"#<Agent:#{name} version:#{version.inspect} engine:\"#{engine.to_s}:#{engine_version}\" os:#{os.to_s.inspect}>"
end
##
# Check if the agent is the same as _other_ agent.
def == other
string == other.string
end
#--
# Class methods
#++
##
# Return engine version for user agent _string_.
def self.engine_version_for_user_agent string
$1 if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.\-]+)/i
end
##
# Return version for user agent _string_.
def self.version_for_user_agent string
case name = name_for_user_agent(string)
when :Chrome ; $1 if string =~ /chrome\/([\d\w\.\-]+)/i
when :Safari ; $1 if string =~ /version\/([\d\w\.\-]+)/i
when :PS3 ; $1 if string =~ /([\d\w\.\-]+)\)\s*$/i
when :PSP ; $1 if string =~ /([\d\w\.\-]+)\)?\s*$/i
else $1 if string =~ /#{name}[\/ ]([\d\w\.\-]+)/i
end
end
##
# Return engine symbol for user agent _string_.
def self.engine_for_user_agent string
case string
when /webkit/i ; :webkit
when /khtml/i ; :khtml
when /konqueror/i ; :konqueror
when /chrome/i ; :chrome
when /presto/i ; :presto
when /gecko/i ; :gecko
when /msie/i ; :msie
else :unknown
end
end
##
# Return the os for user agent _string_.
def self.os_for_user_agent string
case string
when /windows nt 6\.0/i ; :'Windows Vista'
when /windows nt 6\.\d+/i ; :'Windows 7'
when /windows nt 5\.2/i ; :'Windows 2003'
when /windows nt 5\.1/i ; :'Windows XP'
when /windows nt 5\.0/i ; :'Windows 2000'
when /os x (\d+)[._](\d+)/i ; :"OS X #{$1}.#{$2}"
when /linux/i ; :Linux
when /wii/i ; :Wii
when /playstation 3/i ; :Playstation
when /playstation portable/i ; :Playstation
else ; :Unknown
end
end
##
# Return name for user agent _string_.
def self.name_for_user_agent string
case string
when /konqueror/i ; :Konqueror
when /chrome/i ; :Chrome
when /safari/i ; :Safari
when /msie/i ; :IE
when /opera/i ; :Opera
when /playstation 3/i ; :PS3
when /playstation portable/i ; :PSP
when /firefox/i ; :Firefox
else ; :Unknown
end
end
@agents = []
##
# Map agent _name_ to _options_.
def self.map name, options = {}
@agents << [name, options]
end
end

View File

@ -0,0 +1,4 @@
class Agent
VERSION = '1.0.0'
end

View File

@ -0,0 +1,64 @@
require File.dirname(__FILE__) + '/spec_helper'
describe Agent do
before :each do
@agent = Agent.new 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_4; en-us) AppleWebKit/528.4+ (KHTML, like Gecko) Version/4.0dp1 Safari/526.11.2'
end
describe "#initialize" do
it "should allow a user agent string to be passed" do
Agent.new('foo').string.should == 'foo'
end
end
describe "#os" do
it "should return operating system symbol" do
@agent.os.should == :'OS X 10.5'
end
end
describe "#engine" do
it "should return engine symbol" do
@agent.engine.should == :webkit
end
end
describe "#engine_version" do
it "should return engine version" do
@agent.engine_version.should == '528.4'
end
end
describe "#to_s" do
it "should return the user agent string" do
@agent.to_s.should == @agent.string
end
end
describe "#inspect" do
it "should return string presenting the engine, os, version, etc" do
@agent.inspect.should == '#<Agent:Safari version:"4.0dp1" engine:"webkit:528.4" os:"OS X 10.5">'
end
end
describe "#name" do
it "should return the agent name symbol" do
@agent.name.should == :'Safari'
end
end
describe "#==" do
it "should be equal when the user agent strings are the same" do
a = Agent.new 'foo'
b = Agent.new 'foo'
a.should == b
end
it "should not be equal when user agent strings are different" do
a = Agent.new 'foo'
b = Agent.new 'bar'
a.should_not == b
end
end
end

View File

@ -0,0 +1,53 @@
require File.dirname(__FILE__) + '/spec_helper'
def test name, version, os, engine, engine_version, string
it "should parse #{name} #{version} on #{os} with engine #{engine} #{engine_version}" do
agent = Agent.new string
agent.name.should == name
agent.os.should == os
agent.engine.should == engine
agent.version.should == version
agent.engine_version.should == engine_version
end
end
describe Agent do
test :Safari, '4.0dp1', :'Windows XP', :webkit, '526.9', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en) AppleWebKit/526.9 (KHTML, like Gecko) Version/4.0dp1 Safari/526.8'
test :Safari, '4.0.3', :'Windows Vista', :webkit, '531.9', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9'
test :Safari, '4.0.2', :'Windows 7', :webkit, '532', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532+ (KHTML, like Gecko) Version/4.0.2 Safari/530.19.1'
test :Safari, '4.0.1', :'OS X 10.5', :webkit, '531.2', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/4.0.1 Safari/530.18'
test :Safari, '4.0', :'Windows Vista', :webkit, '528.16', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru-RU) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16'
test :Safari, '3.2.3', :'Windows XP', :webkit, '525.28.3', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; cs-CZ) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.29'
test :IE, '8.0', :'Windows 7', :msie, '8.0', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)'
test :IE, '7.0b', :'Windows 2003', :msie, '7.0b', 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)'
test :IE, '6.0b', :'Windows XP', :msie, '6.0b', 'Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)'
test :IE, '6.0', :'Windows XP', :msie, '6.0', 'Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'
test :Opera, '9.99', :'Windows XP', :presto, '9.9.9', 'Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9'
test :Opera, '9.70', :Linux, :gecko, '20061208', 'Mozilla/5.0 (Linux i686 ; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.70'
test :Opera, '9.64', :Linux, :presto, '2.1.1', 'Opera/9.64 (X11; Linux i686; U; Linux Mint; it) Presto/2.1.1'
test :Opera, '9.00', :Wii, :unknown, nil, 'Opera/9.00 (Nintindo Wii; U; ; 103858; Wii Shop Channel/1.0; en)'
test :Chrome, '4.0.202.2', :Linux, :webkit, '532.0', 'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/4.0.202.2 Safari/532.0'
test :Chrome, '0.2.149.27', :'Windows 2003', :webkit, '525.13', 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13'
test :Chrome, '0.2.149.30', :'Windows Vista', :webkit, '525.13', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.30 Safari/525.13'
test :Konqueror, '4.2', :Linux, :khtml, '4.2.4', 'Mozilla/5.0 (compatible; Konqueror/4.2; Linux; X11; x86_64) KHTML/4.2.4 (like Gecko) Fedora/4.2.4-2.fc11'
test :Konqueror, '3.1-rc6', :Linux, :konqueror, '3.1-rc6', 'Mozilla/5.0 (compatible; Konqueror/3.1-rc6; i686 Linux; 20021105)'
test :PS3, '2.00', :Playstation, :unknown, nil, 'Mozilla/5.0 (PLAYSTATION 3; 2.00)'
test :PS3, '1.10', :Playstation, :unknown, nil, 'Mozilla/5.0 (PLAYSTATION 3; 1.10)'
test :PSP, '2.00', :Playstation, :unknown, nil, 'PSP (PlayStation Portable); 2.00'
test :PSP, '2.00', :Playstation, :unknown, nil, 'Mozilla/4.0 (PSP (PlayStation Portable); 2.00)'
test :Firefox, '3.5', :Linux, :gecko, '20090624', 'Mozilla/5.0 (X11;U; Linux i686; en-GB; rv:1.9.1) Gecko/20090624 Ubuntu/9.04 (jaunty) Firefox/3.5'
test :Firefox, '3.5', :'Windows 7', :gecko, '20090612', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1) Gecko/20090612 Firefox/3.5'
test :Firefox, '3.1', :'Windows XP', :gecko, '2009011606', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6pre) Gecko/2009011606 Firefox/3.1'
test :Firefox, '3.0', :Linux, :gecko, '2008062315', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9) Gecko/2008062315 (Gentoo) Firefox/3.0'
test :Firefox, '2.0', :Linux, :gecko, '20061202', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20061202 Firefox/2.0'
end

View File

@ -0,0 +1,2 @@
--color
--format specdoc

View File

@ -0,0 +1,3 @@
$:.unshift File.dirname(__FILE__) + '/../lib'
require 'user-agent'

View File

@ -0,0 +1,13 @@
namespace :docs do
desc 'Remove rdoc products'
task :remove => [:clobber_docs]
desc 'Build docs, and open in browser for viewing (specify BROWSER)'
task :open do
browser = ENV["BROWSER"] || "safari"
sh "open -a #{browser} doc/index.html"
end
end

View File

@ -0,0 +1,3 @@
desc 'Build gemspec file'
task :gemspec => [:build_gemspec]

View File

@ -0,0 +1,25 @@
require 'spec/rake/spectask'
desc "Run all specifications"
Spec::Rake::SpecTask.new(:spec) do |t|
t.libs << "lib"
t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"]
end
namespace :spec do
desc "Run all specifications verbosely"
Spec::Rake::SpecTask.new(:verbose) do |t|
t.libs << "lib"
t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
end
desc "Run specific specification verbosely (specify SPEC)"
Spec::Rake::SpecTask.new(:select) do |t|
t.libs << "lib"
t.spec_files = [ENV["SPEC"]]
t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
end
end

View File

@ -0,0 +1,30 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = %q{user-agent}
s.version = "1.0.0"
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["TJ Holowaychuk"]
s.date = %q{2009-10-08}
s.description = %q{User agent parser}
s.email = %q{tj@vision-media.ca}
s.extra_rdoc_files = ["README.rdoc", "lib/user-agent.rb", "lib/user-agent/agent.rb", "lib/user-agent/version.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
s.files = ["History.rdoc", "Manifest", "README.rdoc", "Rakefile", "lib/user-agent.rb", "lib/user-agent/agent.rb", "lib/user-agent/version.rb", "spec/agent_spec.rb", "spec/agents_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "user-agent.gemspec"]
s.homepage = %q{}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "User-agent", "--main", "README.rdoc"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{user-agent}
s.rubygems_version = %q{1.3.5}
s.summary = %q{User agent parser}
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
else
end
else
end
end