Class: Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/user-agent/agent.rb,
lib/user-agent/version.rb

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Agent

Initialize with user agent string.



12
13
14
# File 'lib/user-agent/agent.rb', line 12

def initialize string
  @string = string.strip
end

Instance Attribute Details

#stringObject (readonly)

User agent string.



7
8
9
# File 'lib/user-agent/agent.rb', line 7

def string
  @string
end

Class Method Details

.engine_for_user_agent(string) ⇒ Object

Return engine symbol for user agent string.



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/user-agent/agent.rb', line 103

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

.engine_version_for_user_agent(string) ⇒ Object

Return engine version for user agent string.



83
84
85
# File 'lib/user-agent/agent.rb', line 83

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

.map(name, options = {}) ⇒ Object

Map agent name to options.



156
157
158
# File 'lib/user-agent/agent.rb', line 156

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

.name_for_user_agent(string) ⇒ Object

Return name for user agent string.



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/user-agent/agent.rb', line 138

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

.os_for_user_agent(string) ⇒ Object

Return the os for user agent string.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/user-agent/agent.rb', line 119

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

.version_for_user_agent(string) ⇒ Object

Return version for user agent string.



90
91
92
93
94
95
96
97
98
# File 'lib/user-agent/agent.rb', line 90

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

Instance Method Details

#==(other) ⇒ Object

Check if the agent is the same as other agent.



72
73
74
# File 'lib/user-agent/agent.rb', line 72

def == other
  string == other.string
end

#engineObject

User agent engine symbol.



37
38
39
# File 'lib/user-agent/agent.rb', line 37

def engine
  Agent.engine_for_user_agent string
end

#engine_versionObject

User agent engine version string.



44
45
46
# File 'lib/user-agent/agent.rb', line 44

def engine_version
  Agent.engine_version_for_user_agent string
end

#inspectObject

Inspect.



65
66
67
# File 'lib/user-agent/agent.rb', line 65

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

#nameObject

User agent name symbol.



23
24
25
# File 'lib/user-agent/agent.rb', line 23

def name
  Agent.name_for_user_agent string
end

#osObject

User agent os symbol.



51
52
53
# File 'lib/user-agent/agent.rb', line 51

def os
  Agent.os_for_user_agent string
end

#to_sObject

User agent string.



58
59
60
# File 'lib/user-agent/agent.rb', line 58

def to_s
  string
end

#versionObject

User agent version.



30
31
32
# File 'lib/user-agent/agent.rb', line 30

def version
  Agent.version_for_user_agent string
end