This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
forked from tj/user-agent
/
user_agent.rb
117 lines (101 loc) · 3.21 KB
/
user_agent.rb
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
class UserAgent
attr_reader :string
def initialize(string)
@string = string.strip
end
def name
self.class.name_for_user_agent(string)
end
def version
self.class.version_for_user_agent(string)
end
def engine
self.class.engine_for_user_agent(string)
end
def engine_version
self.class.engine_version_for_user_agent(string)
end
def os
self.class.os_for_user_agent(string)
end
def platform
self.class.platform_for_user_agent(string)
end
def to_s
string
end
def inspect
"#<UserAgent:#{name} version:#{version.inspect} engine:\"#{engine.to_s}:#{engine_version}\" os:#{os.to_s.inspect}>"
end
def ==(other)
string == other.string
end
def self.engine_version_for_user_agent(string)
if string =~ /#{engine_for_user_agent(string)}[\/ ]([\d\w\.\-]+)/i
$1
end
end
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
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
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
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
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
end