Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 28, 2009
0 parents commit 78e11d0
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 0 deletions.
4 changes: 4 additions & 0 deletions History.rdoc
@@ -0,0 +1,4 @@

=== 0.0.1 / YYYY-MM-DD

* Initial release
14 changes: 14 additions & 0 deletions Manifest
@@ -0,0 +1,14 @@
History.rdoc
lib/user-agent/agent.rb
lib/user-agent/map.rb
lib/user-agent/version.rb
lib/user-agent.rb
Rakefile
README.rdoc
spec/GEM_spec.rb
spec/spec_helper.rb
tasks/docs.rake
tasks/gemspec.rake
tasks/spec.rake
Todo.rdoc
Manifest
29 changes: 29 additions & 0 deletions README.rdoc
@@ -0,0 +1,29 @@

= GEM_NAME

GEM_DESCRIPTION

== License:

(The MIT License)

Copyright (c) 2009 GEM_AUTHOR <GEM_EMAIL>

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.
15 changes: 15 additions & 0 deletions Rakefile
@@ -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 }
26 changes: 26 additions & 0 deletions lib/user-agent.rb
@@ -0,0 +1,26 @@
#--
# 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/map'
require 'user-agent/version'
132 changes: 132 additions & 0 deletions lib/user-agent/agent.rb
@@ -0,0 +1,132 @@

class Agent

##
# User agent string.

attr_reader :string

##
# Initialize with user agent _string_.

def initialize string
@string = string
end

#--
# Instance methods
#++

##
# User agent name symbol.

def name
Agent.name_for_user_agent string
end

##
# User agent engine symbol.

def engine
Agent.engine_for_user_agent string
end

##
# User agent version string.

def version
Agent.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:#{name.to_s.inspect} engine:#{engine.to_s.inspect} os:#{os.to_s.inspect} version:#{version.inspect}>"
end

##
# Check if the agent is the same as _other_ agent.

def == other
self.string == other.string
end

#--
# Class methods
#++

##
# Return version for user agent _string_.

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

##
# Return engine symbol for user agent _string_.

def self.engine_for_user_agent string
case string
when /chrome/i ; :chrome
when /konqueror/i ; :konqueror
when /webkit/i ; :webkit
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.1/i ; :'Windows 7'
when /windows nt 6.0/i ; :'Windows Vista'
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
else ; :Unknown
end
end

##
# Return name for user agent _string_.

def self.name_for_user_agent string
version = version_for_user_agent string
engine = engine_for_user_agent string
info = @agents.find do |name, info|
info[:engine] == engine && info[:version].match(version)
end
info.first if info
end

@agents = []

##
# Map agent _name_ to _options_.

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

end
10 changes: 10 additions & 0 deletions lib/user-agent/map.rb
@@ -0,0 +1,10 @@

class Agent

#--
# Safari
#++

map :'Safari 3', :engine => :webkit, :version => /^52/, :popular => true

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

class Agent
VERSION = '0.0.1'
end
Empty file added spec/agent_spec.rb
Empty file.
Empty file added spec/spec_helper.rb
Empty file.
13 changes: 13 additions & 0 deletions tasks/docs.rake
@@ -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
3 changes: 3 additions & 0 deletions tasks/gemspec.rake
@@ -0,0 +1,3 @@

desc 'Build gemspec file'
task :gemspec => [:build_gemspec]
25 changes: 25 additions & 0 deletions tasks/spec.rake
@@ -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
30 changes: 30 additions & 0 deletions user-agent.gemspec
@@ -0,0 +1,30 @@
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{user-agent}
s.version = "0.0.1"

s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["TJ Holowaychuk"]
s.date = %q{2009-08-28}
s.description = %q{User agent parser}
s.email = %q{tj@vision-media.ca}
s.extra_rdoc_files = ["lib/user-agent/agent.rb", "lib/user-agent/map.rb", "lib/user-agent/version.rb", "lib/user-agent.rb", "README.rdoc", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
s.files = ["History.rdoc", "lib/user-agent/agent.rb", "lib/user-agent/map.rb", "lib/user-agent/version.rb", "lib/user-agent.rb", "Rakefile", "README.rdoc", "spec/GEM_spec.rb", "spec/spec_helper.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "Todo.rdoc", "Manifest", "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

0 comments on commit 78e11d0

Please sign in to comment.