Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit

  • Loading branch information...
commit 78e11d03d1aff70324b6c278ef2663abb9fa63d3 0 parents
TJ Holowaychuk authored August 28, 2009
4  History.rdoc
Source Rendered
... ...
@@ -0,0 +1,4 @@
  1
+
  2
+=== 0.0.1 / YYYY-MM-DD
  3
+
  4
+* Initial release
14  Manifest
... ...
@@ -0,0 +1,14 @@
  1
+History.rdoc
  2
+lib/user-agent/agent.rb
  3
+lib/user-agent/map.rb
  4
+lib/user-agent/version.rb
  5
+lib/user-agent.rb
  6
+Rakefile
  7
+README.rdoc
  8
+spec/GEM_spec.rb
  9
+spec/spec_helper.rb
  10
+tasks/docs.rake
  11
+tasks/gemspec.rake
  12
+tasks/spec.rake
  13
+Todo.rdoc
  14
+Manifest
29  README.rdoc
Source Rendered
... ...
@@ -0,0 +1,29 @@
  1
+
  2
+= GEM_NAME
  3
+
  4
+GEM_DESCRIPTION
  5
+
  6
+== License:
  7
+
  8
+(The MIT License)
  9
+
  10
+Copyright (c) 2009 GEM_AUTHOR <GEM_EMAIL>
  11
+
  12
+Permission is hereby granted, free of charge, to any person obtaining
  13
+a copy of this software and associated documentation files (the
  14
+'Software'), to deal in the Software without restriction, including
  15
+without limitation the rights to use, copy, modify, merge, publish,
  16
+distribute, sublicense, an d/or sell copies of the Software, and to
  17
+permit persons to whom the Software is furnished to do so, subject to
  18
+the following conditions:
  19
+
  20
+The above copyright notice and this permission notice shall be
  21
+included in all copies or substantial portions of the Software.
  22
+
  23
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  24
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  26
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  27
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  28
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  29
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15  Rakefile
... ...
@@ -0,0 +1,15 @@
  1
+
  2
+$:.unshift 'lib'
  3
+require 'user-agent'
  4
+require 'rubygems'
  5
+require 'rake'
  6
+require 'echoe'
  7
+
  8
+Echoe.new "user-agent", Agent::VERSION do |p|
  9
+  p.author = "TJ Holowaychuk"
  10
+  p.email = "tj@vision-media.ca"
  11
+  p.summary = "User agent parser"
  12
+  p.runtime_dependencies = []
  13
+end
  14
+
  15
+Dir['tasks/**/*.rake'].sort.each { |f| load f }
26  lib/user-agent.rb
... ...
@@ -0,0 +1,26 @@
  1
+#--
  2
+# Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
  3
+#
  4
+# Permission is hereby granted, free of charge, to any person obtaining
  5
+# a copy of this software and associated documentation files (the
  6
+# "Software"), to deal in the Software without restriction, including
  7
+# without limitation the rights to use, copy, modify, merge, publish,
  8
+# distribute, sublicense, and/or sell copies of the Software, and to
  9
+# permit persons to whom the Software is furnished to do so, subject to
  10
+# the following conditions:
  11
+#
  12
+# The above copyright notice and this permission notice shall be
  13
+# included in all copies or substantial portions of the Software.
  14
+#
  15
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22
+#++
  23
+
  24
+require 'user-agent/agent'
  25
+require 'user-agent/map'
  26
+require 'user-agent/version'
132  lib/user-agent/agent.rb
... ...
@@ -0,0 +1,132 @@
  1
+
  2
+class Agent
  3
+  
  4
+  ##
  5
+  # User agent string.
  6
+  
  7
+  attr_reader :string
  8
+  
  9
+  ##
  10
+  # Initialize with user agent _string_.
  11
+  
  12
+  def initialize string
  13
+    @string = string
  14
+  end
  15
+  
  16
+  #--
  17
+  # Instance methods
  18
+  #++
  19
+  
  20
+  ##
  21
+  # User agent name symbol.
  22
+  
  23
+  def name
  24
+    Agent.name_for_user_agent string
  25
+  end
  26
+  
  27
+  ##
  28
+  # User agent engine symbol.
  29
+  
  30
+  def engine
  31
+    Agent.engine_for_user_agent string
  32
+  end
  33
+  
  34
+  ##
  35
+  # User agent version string.
  36
+  
  37
+  def version
  38
+    Agent.version_for_user_agent string
  39
+  end
  40
+  
  41
+  ##
  42
+  # User agent os symbol.
  43
+  
  44
+  def os
  45
+    Agent.os_for_user_agent string
  46
+  end
  47
+  
  48
+  ##
  49
+  # User agent string.
  50
+  
  51
+  def to_s
  52
+    string
  53
+  end
  54
+  
  55
+  ##
  56
+  # Inspect.
  57
+  
  58
+  def inspect
  59
+    "#<Agent name:#{name.to_s.inspect} engine:#{engine.to_s.inspect} os:#{os.to_s.inspect} version:#{version.inspect}>"
  60
+  end
  61
+  
  62
+  ##
  63
+  # Check if the agent is the same as _other_ agent.
  64
+  
  65
+  def == other
  66
+    self.string == other.string
  67
+  end
  68
+  
  69
+  #--
  70
+  # Class methods
  71
+  #++
  72
+  
  73
+  ##
  74
+  # Return version for user agent _string_.
  75
+  
  76
+  def self.version_for_user_agent string
  77
+    $1 if string =~ /#{engine_for_user_agent(string)}\/([\d\w\.]+)/i
  78
+  end
  79
+  
  80
+  ##
  81
+  # Return engine symbol for user agent _string_.
  82
+  
  83
+  def self.engine_for_user_agent string
  84
+    case string
  85
+    when /chrome/i    ; :chrome
  86
+    when /konqueror/i ; :konqueror
  87
+    when /webkit/i    ; :webkit
  88
+    when /presto/i    ; :presto
  89
+    when /gecko/i     ; :gecko
  90
+    when /msie/i      ; :msie
  91
+    else                :unknown
  92
+    end
  93
+  end
  94
+  
  95
+  ##
  96
+  # Return the os for user agent _string_.
  97
+  
  98
+  def self.os_for_user_agent string
  99
+    case string
  100
+    when /windows nt 6.1/i      ; :'Windows 7'
  101
+    when /windows nt 6.0/i      ; :'Windows Vista'
  102
+    when /windows nt 5.2/i      ; :'Windows 2003'
  103
+    when /windows nt 5.1/i      ; :'Windows XP'
  104
+    when /windows nt 5.0/i      ; :'Windows 2000'
  105
+    when /os x (\d+)[._](\d+)/i ; :"OS X #{$1}.#{$2}"
  106
+    when /linux/i               ; :Linux
  107
+    else                        ; :Unknown
  108
+    end
  109
+  end
  110
+  
  111
+  ##
  112
+  # Return name for user agent _string_.
  113
+  
  114
+  def self.name_for_user_agent string
  115
+    version = version_for_user_agent string
  116
+    engine = engine_for_user_agent string
  117
+    info = @agents.find do |name, info|
  118
+      info[:engine] == engine && info[:version].match(version)
  119
+    end
  120
+    info.first if info
  121
+  end
  122
+  
  123
+  @agents = []
  124
+  
  125
+  ##
  126
+  # Map agent _name_ to _options_.
  127
+  
  128
+  def self.map name, options = {}
  129
+    @agents << [name, options]
  130
+  end
  131
+
  132
+end
10  lib/user-agent/map.rb
... ...
@@ -0,0 +1,10 @@
  1
+
  2
+class Agent
  3
+  
  4
+  #--
  5
+  # Safari
  6
+  #++
  7
+  
  8
+  map :'Safari 3', :engine => :webkit, :version => /^52/, :popular => true
  9
+  
  10
+end
4  lib/user-agent/version.rb
... ...
@@ -0,0 +1,4 @@
  1
+
  2
+class Agent
  3
+  VERSION = '0.0.1'
  4
+end
0  spec/agent_spec.rb
No changes.
0  spec/spec_helper.rb
No changes.
13  tasks/docs.rake
... ...
@@ -0,0 +1,13 @@
  1
+
  2
+namespace :docs do
  3
+  
  4
+  desc 'Remove rdoc products'
  5
+  task :remove => [:clobber_docs]
  6
+  
  7
+  desc 'Build docs, and open in browser for viewing (specify BROWSER)'
  8
+  task :open do
  9
+    browser = ENV["BROWSER"] || "safari"
  10
+    sh "open -a #{browser} doc/index.html"
  11
+  end
  12
+  
  13
+end
3  tasks/gemspec.rake
... ...
@@ -0,0 +1,3 @@
  1
+
  2
+desc 'Build gemspec file'
  3
+task :gemspec => [:build_gemspec]
25  tasks/spec.rake
... ...
@@ -0,0 +1,25 @@
  1
+
  2
+require 'spec/rake/spectask'
  3
+  
  4
+desc "Run all specifications"
  5
+Spec::Rake::SpecTask.new(:spec) do |t|
  6
+  t.libs << "lib"
  7
+  t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"]
  8
+end
  9
+  
  10
+namespace :spec do
  11
+
  12
+  desc "Run all specifications verbosely"
  13
+  Spec::Rake::SpecTask.new(:verbose) do |t|
  14
+    t.libs << "lib"
  15
+    t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
  16
+  end
  17
+  
  18
+  desc "Run specific specification verbosely (specify SPEC)"
  19
+  Spec::Rake::SpecTask.new(:select) do |t|
  20
+    t.libs << "lib"
  21
+    t.spec_files = [ENV["SPEC"]]
  22
+    t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
  23
+  end
  24
+  
  25
+end
30  user-agent.gemspec
... ...
@@ -0,0 +1,30 @@
  1
+# -*- encoding: utf-8 -*-
  2
+
  3
+Gem::Specification.new do |s|
  4
+  s.name = %q{user-agent}
  5
+  s.version = "0.0.1"
  6
+
  7
+  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
  8
+  s.authors = ["TJ Holowaychuk"]
  9
+  s.date = %q{2009-08-28}
  10
+  s.description = %q{User agent parser}
  11
+  s.email = %q{tj@vision-media.ca}
  12
+  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"]
  13
+  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"]
  14
+  s.homepage = %q{}
  15
+  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "User-agent", "--main", "README.rdoc"]
  16
+  s.require_paths = ["lib"]
  17
+  s.rubyforge_project = %q{user-agent}
  18
+  s.rubygems_version = %q{1.3.5}
  19
+  s.summary = %q{User agent parser}
  20
+
  21
+  if s.respond_to? :specification_version then
  22
+    current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
  23
+    s.specification_version = 3
  24
+
  25
+    if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
  26
+    else
  27
+    end
  28
+  else
  29
+  end
  30
+end

0 notes on commit 78e11d0

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