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.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Agent

Returns a new instance of Agent.



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

def initialize string
  @string = string.strip
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



4
5
6
# File 'lib/user-agent/agent.rb', line 4

def string
  @string
end

Class Method Details

.engine_for_user_agent(string) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/user-agent/agent.rb', line 62

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



48
49
50
# File 'lib/user-agent/agent.rb', line 48

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

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



122
123
124
# File 'lib/user-agent/agent.rb', line 122

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

.name_for_user_agent(string) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/user-agent/agent.rb', line 106

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

.os_for_user_agent(string) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/user-agent/agent.rb', line 75

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

.platform_for_user_agent(string) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/user-agent/agent.rb', line 93

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

.version_for_user_agent(string) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/user-agent/agent.rb', line 52

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



42
43
44
# File 'lib/user-agent/agent.rb', line 42

def == other
  string == other.string
end

#engineObject



18
19
20
# File 'lib/user-agent/agent.rb', line 18

def engine
  Agent.engine_for_user_agent string
end

#engine_versionObject



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

def engine_version
  Agent.engine_version_for_user_agent string
end

#inspectObject



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

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

#nameObject



10
11
12
# File 'lib/user-agent/agent.rb', line 10

def name
  Agent.name_for_user_agent string
end

#osObject



26
27
28
# File 'lib/user-agent/agent.rb', line 26

def os
  Agent.os_for_user_agent string
end

#platformObject



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

def platform
  Agent.platform_for_user_agent string
end

#to_sObject



34
35
36
# File 'lib/user-agent/agent.rb', line 34

def to_s
  string
end

#versionObject



14
15
16
# File 'lib/user-agent/agent.rb', line 14

def version
  Agent.version_for_user_agent string
end