Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Untitled #2

Closed
wants to merge 16 commits into from
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
.bundle
Gemfile.lock
*.gem
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm --create 1.9.2@user-agent
6 changes: 2 additions & 4 deletions Gemfile
@@ -1,5 +1,3 @@
source :gemcutter
source :rubygems

gem "rake"
gem "echoe"
gem "rspec"
gemspec
12 changes: 0 additions & 12 deletions History.rdoc

This file was deleted.

16 changes: 0 additions & 16 deletions Manifest

This file was deleted.

11 changes: 7 additions & 4 deletions README.rdoc
Expand Up @@ -6,10 +6,11 @@ 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.name # => :Safari
agent.version # => '4.0.3'
agent.engine # => :webkit
agent.os # => :'Windows Vista'
agent.platform # => :Windows
agent.engine_version # => '531.9'

== Supported Agents
Expand All @@ -22,6 +23,8 @@ User agent parser.
* PS3
* PSP
* Wii
* iPad
* iPhone

== License:

Expand Down
16 changes: 3 additions & 13 deletions Rakefile
@@ -1,15 +1,5 @@
require "bundler/gem_tasks"

$:.unshift 'lib'
require 'user-agent'
require 'rubygems'
require 'rake'
require 'echoe'
Dir['tasks/**/*.rake'].sort.each { |f| load f }

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 }
task :default => :spec
23 changes: 23 additions & 0 deletions cv-user-agent.gemspec
@@ -0,0 +1,23 @@
$:.push File.expand_path('../lib', __FILE__)
require 'user-agent/version'

Gem::Specification.new do |s|

s.name = 'cv-user-agent'
s.version = UserAgent::VERSION
s.authors = ['Carlos Villela']
s.email = 'cvillela@thoughtworks.com'
s.homepage = 'http://github.com/cv/user-agent'
s.summary = 'User agent parser'
s.description = 'user-agent is a user agent parser support most of the commonly used browsers today.'
s.rubyforge_project = 'cv-user-agent'
s.require_paths = ['lib']

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- spec/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }

s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'

end
10 changes: 9 additions & 1 deletion lib/user-agent.rb
Expand Up @@ -22,4 +22,12 @@
#++

require 'user-agent/agent'
require 'user-agent/version'
require 'user-agent/version'

module UserAgent

def self.parse string
ParsedUserAgent.new string
end

end
233 changes: 99 additions & 134 deletions lib/user-agent/agent.rb
@@ -1,161 +1,126 @@
module UserAgent

class Agent
class ParsedUserAgent

##
# User agent string.
attr_reader :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
def initialize string
@string = string.strip
end

##
# User agent string.
def name
ParsedUserAgent.name_for_user_agent string
end

def to_s
string
end
def version
ParsedUserAgent.version_for_user_agent string
end

##
# Inspect.
def engine
ParsedUserAgent.engine_for_user_agent string
end

def inspect
"#<Agent:#{name} version:#{version.inspect} engine:\"#{engine.to_s}:#{engine_version}\" os:#{os.to_s.inspect}>"
end
def engine_version
ParsedUserAgent.engine_version_for_user_agent string
end

##
# Check if the agent is the same as _other_ agent.
def os
ParsedUserAgent.os_for_user_agent string
end

def == other
string == other.string
end
def platform
ParsedUserAgent.platform_for_user_agent string
end

#--
# Class methods
#++
def to_s
string
end

##
# Return engine version for user agent _string_.
def inspect
"#<ParsedUserAgent:#{name} version:#{version.inspect} engine:\"#{engine.to_s}:#{engine_version}\" os:#{os.to_s.inspect}>"
end

def self.engine_version_for_user_agent string
$1 if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.\-]+)/i
end
def == other
string == other.string
end

##
# Return 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

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
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
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

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
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
when /\(ipad.*os (\d+)[._](\d+)/i ; :"iPad OS #{$1}.#{$2}"
when /\(iphone.*os (\d+)[._](\d+)/i ; :"iPhone OS #{$1}.#{$2}"
else ; :Unknown
end
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
def self.platform_for_user_agent string
case string
when /windows/i ; :Windows
when /macintosh/i ; :Macintosh
when /linux/i ; :Linux
when /wii/i ; :Wii
when /playstation/i ; :Playstation
when /ipad/i ; :iPad
when /iphone/i ; :iPhone
else :Unknown
end
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
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

@agents = []
@agents = []

##
# Map agent _name_ to _options_.
def self.map name, options = {}
@agents << [name, options]
end

def self.map name, options = {}
@agents << [name, options]
end

end
5 changes: 2 additions & 3 deletions lib/user-agent/version.rb
@@ -1,4 +1,3 @@

class Agent
VERSION = '1.0.0'
module UserAgent
VERSION = '1.1.0'
end