Class: UserAgent
- Inherits:
-
Object
show all
- Defined in:
- lib/user_agent.rb,
lib/user_agent/version.rb
Defined Under Namespace
Modules: Browsers, Engines, OS, Platform, Versions
Constant Summary
collapse
- AttributesForInspect =
[:name, :version, :os, :platform, :engine, :engine_version]
- MobilePlatforms =
[:android, :blackberry, :ipad, :ipod, :iphone, :symbian, :windows_phone]
- VERSION =
'1.1.1'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(source) ⇒ UserAgent
Returns a new instance of UserAgent.
179
180
181
|
# File 'lib/user_agent.rb', line 179
def initialize(source)
@source = source.strip
end
|
Instance Attribute Details
#source ⇒ Object
Returns the value of attribute source.
177
178
179
|
# File 'lib/user_agent.rb', line 177
def source
@source
end
|
Class Method Details
.browser_name(string) ⇒ Object
.browser_version(string) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/user_agent.rb', line 119
def self.browser_version(string)
case name = browser_name(string)
when :chrome
$1 if string =~ Versions::Chrome
when :safari
$1 if string =~ Versions::Safari
when :ps3
$1 if string =~ Versions::Ps3
when :psp
$1 if string =~ Versions::Psp
when :lotus
$1 if string =~ Versions::Lotus
else
$1 if string =~ /#{name}[\/ ]([\d\w\.\-]+)/i
end
end
|
.engine_version(string) ⇒ Object
91
92
93
94
95
|
# File 'lib/user_agent.rb', line 91
def self.engine_version(string)
if string =~ /#{engine(string)}[\/ ]([\d\w\.\-]+)/i
$1
end
end
|
.os(string) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/user_agent.rb', line 136
def self.os(string)
case string
when OS::WindowsPhone then 'Windows Phone'
when OS::WindowsVista then 'Windows Vista'
when OS::Windows7 then 'Windows 7'
when OS::Windows2003 then 'Windows 2003'
when OS::WindowsXP then 'Windows XP'
when OS::Windows2000 then 'Windows 2000'
when OS::Windows then 'Windows'
when OS::OSX then "OS X #{$1}.#{$2}"
when OS::Linux then 'Linux'
when OS::Wii then 'Wii'
when OS::PS3 then 'Playstation'
when OS::PSP then 'Playstation'
when OS::Ipad then "iPad OS #{$1}.#{$2}"
when OS::Iphone then "iPhone OS #{$1}.#{$2}"
when Platform::Symbian then "Symbian OS"
else
'Unknown'
end
end
|
Instance Method Details
#engine ⇒ Object
191
192
193
|
# File 'lib/user_agent.rb', line 191
def engine
@engine ||= self.class.engine(source)
end
|
#engine_version ⇒ Object
195
196
197
|
# File 'lib/user_agent.rb', line 195
def engine_version
@engine_version ||= self.class.engine_version(source)
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
223
224
225
|
# File 'lib/user_agent.rb', line 223
def eql?(other)
self.class.eql?(other.class) && source == other.source
end
|
#inspect ⇒ Object
215
216
217
218
219
220
221
|
# File 'lib/user_agent.rb', line 215
def inspect
attributes_as_nice_string = AttributesForInspect.map do |name|
"#{name}: #{send(name).inspect}"
end.join(', ')
"#<#{self.class}: #{attributes_as_nice_string}>"
end
|
#mobile? ⇒ Boolean
207
208
209
|
# File 'lib/user_agent.rb', line 207
def mobile?
MobilePlatforms.include?(platform) || name == :psp
end
|
#name ⇒ Object
183
184
185
|
# File 'lib/user_agent.rb', line 183
def name
@name ||= self.class.browser_name(source)
end
|
#os ⇒ Object
199
200
201
|
# File 'lib/user_agent.rb', line 199
def os
@os ||= self.class.os(source)
end
|
203
204
205
|
# File 'lib/user_agent.rb', line 203
def platform
@platform ||= self.class.platform(source)
end
|
#to_s ⇒ Object
211
212
213
|
# File 'lib/user_agent.rb', line 211
def to_s
@source
end
|
#version ⇒ Object
187
188
189
|
# File 'lib/user_agent.rb', line 187
def version
@version ||= self.class.browser_version(source)
end
|