Skip to content
Browse files

Cleanup/style.

  • Loading branch information...
1 parent 9d97f1f commit a9227e32f57c9894e3f29348ed993140631e03b1 @jnunemaker committed Jun 21, 2011
View
30 README.rdoc
@@ -1,17 +1,16 @@
-
= 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 = UserAgent.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
@@ -22,28 +21,3 @@ User agent parser.
* 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
25 lib/user-agent.rb
@@ -1,25 +0,0 @@
-#--
-# 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
161 lib/user-agent/agent.rb
@@ -1,161 +0,0 @@
-
-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
98 lib/user_agent.rb
@@ -0,0 +1,98 @@
+class UserAgent
+ attr_reader :string
+
+ def initialize(string)
+ @string = string.strip
+ end
+
+ def name
+ self.class.name_for_user_agent(string)
+ end
+
+ def version
+ self.class.version_for_user_agent(string)
+ end
+
+ def engine
+ self.class.engine_for_user_agent(string)
+ end
+
+ def engine_version
+ self.class.engine_version_for_user_agent(string)
+ end
+
+ def os
+ self.class.os_for_user_agent(string)
+ end
+
+ def to_s
+ string
+ end
+
+ def inspect
+ "#<UserAgent:#{name} version:#{version.inspect} engine:\"#{engine.to_s}:#{engine_version}\" os:#{os.to_s.inspect}>"
+ end
+
+ def ==(other)
+ string == other.string
+ end
+
+ def self.engine_version_for_user_agent(string)
+ if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.\-]+)/i
+ $1
+ end
+ end
+
+ 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
+
+ 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
+
+ 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
+
+ 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
+end
View
3 lib/user-agent/version.rb → lib/user_agent/version.rb
@@ -1,4 +1,3 @@
-
-class Agent
+class UserAgent
VERSION = '1.0.0'
end
View
2 spec/spec_helper.rb
@@ -1,6 +1,6 @@
$:.unshift(File.expand_path('../../lib', __FILE__))
-require 'user-agent'
+require 'user_agent'
require 'pathname'
require 'logger'
View
39 spec/agent_spec.rb → spec/user_agent_spec.rb
@@ -1,63 +1,62 @@
+require File.expand_path('../spec_helper', __FILE__)
-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'
+describe UserAgent do
+ before(:each) do
+ @agent = UserAgent.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'
+ UserAgent.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">'
+ @agent.inspect.should == '#<UserAgent: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 = UserAgent.new 'foo'
+ b = UserAgent.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 = UserAgent.new 'foo'
+ b = UserAgent.new 'bar'
a.should_not == b
end
end
View
9 spec/agents_spec.rb → spec/user_agents_spec.rb
@@ -1,9 +1,8 @@
+require File.expand_path('../spec_helper', __FILE__)
-require File.dirname(__FILE__) + '/spec_helper'
-
-def test name, version, os, engine, engine_version, string
+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 = UserAgent.new string
agent.name.should == name
agent.os.should == os
agent.engine.should == engine
@@ -12,7 +11,7 @@ def test name, version, os, engine, engine_version, string
end
end
-describe Agent do
+describe UserAgent 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'
View
13 tasks/docs.rake
@@ -1,13 +0,0 @@
-
-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
3 tasks/gemspec.rake
@@ -1,3 +0,0 @@
-
-desc 'Build gemspec file'
-task :gemspec => [:build_gemspec]
View
25 tasks/spec.rake
@@ -1,25 +0,0 @@
-
-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
10 user-agent.gemspec → user_agent.gemspec
@@ -1,13 +1,13 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
-require "user-agent/version"
+require "user_agent/version"
Gem::Specification.new do |s|
- s.name = "user-agent"
- s.version = Agent::VERSION
+ s.name = "user_agent"
+ s.version = UserAgent::VERSION
s.platform = Gem::Platform::RUBY
- s.authors = ["TJ Holowaychuk"]
- s.email = ['tj@vision-media.ca']
+ s.authors = ['TJ Holowaychuk', 'John Nunemaker']
+ s.email = ['tj@vision-media.ca', 'nunemaker@gmail.com']
s.homepage = ''
s.summary = %q{User agent parser}
s.description = %q{User agent parser}

0 comments on commit a9227e3

Please sign in to comment.
Something went wrong with that request. Please try again.