Class: UserAgent::ParsedUserAgent

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ ParsedUserAgent

Returns a new instance of ParsedUserAgent.



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

def initialize string
  @string = string.strip
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



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

def string
  @string
end

Class Method Details

.engine_for_user_agent(string) ⇒ Object



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

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



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

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

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



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

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

.name_for_user_agent(string) ⇒ Object



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

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



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

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



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

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



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

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



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

def == other
  string == other.string
end

#engineObject



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

def engine
  ParsedUserAgent.engine_for_user_agent string
end

#engine_versionObject



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

def engine_version
  ParsedUserAgent.engine_version_for_user_agent string
end

#inspectObject



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

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

#nameObject



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

def name
  ParsedUserAgent.name_for_user_agent string
end

#osObject



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

def os
  ParsedUserAgent.os_for_user_agent string
end

#platformObject



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

def platform
  ParsedUserAgent.platform_for_user_agent string
end

#to_sObject



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

def to_s
  string
end

#versionObject



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

def version
  ParsedUserAgent.version_for_user_agent string
end