This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Cleanup/style.

  • Loading branch information...
jnunemaker committed Jun 21, 2011
1 parent 9d97f1f commit a9227e32f57c9894e3f29348ed993140631e03b1
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -1,4 +1,3 @@
-
-class Agent
+class UserAgent
VERSION = '1.0.0'
end
View
@@ -1,6 +1,6 @@
$:.unshift(File.expand_path('../../lib', __FILE__))
-require 'user-agent'
+require 'user_agent'
require 'pathname'
require 'logger'
Oops, something went wrong.

0 comments on commit a9227e3

Please sign in to comment.