Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f61cecd8f3
Fetching contributors…

Cannot retrieve contributors at this time

file 160 lines (123 sloc) 3.356 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

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
    else ; :Unknown
    end
  end
  
  @agents = []
  
  ##
  # Map agent _name_ to _options_.
  
  def self.map name, options = {}
    @agents << [name, options]
  end

end
Something went wrong with that request. Please try again.