Class: UserAgent

Inherits:
Object
  • 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

#sourceObject (readonly)

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/user_agent.rb', line 97

def self.browser_name(string)
  case string
    when Browsers::Konqueror   then :konqueror
    when Browsers::Chrome      then :chrome
    when Browsers::Safari      then :safari
    when Browsers::Opera       then :opera
    when Browsers::PS3         then :ps3
    when Browsers::PSP         then :psp
    when Browsers::Firefox     then :firefox
    when Browsers::Lotus       then :lotus
    when Browsers::Netscape    then :netscape
    when Browsers::SeaMonkey   then :seamonkey
    when Browsers::Thunderbird then :thunderbird
    when Browsers::Outlook     then :outlook
    when Browsers::Evolution   then :evolution
    when Browsers::IEMobile    then :iemobile
    when Browsers::IE          then :ie
    else
      :unknown
  end
end

.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(string) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/user_agent.rb', line 76

def self.engine(string)
  case string
    when Engines::Webkit    then :webkit
    when Engines::Khtml     then :khtml
    when Engines::Konqueror then :konqueror
    when Engines::Chrome    then :chrome
    when Engines::Presto    then :presto
    when Engines::Gecko     then :gecko
    when /opera/i           then :unknown
    when Engines::Msie      then :msie
    else
      :unknown
  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

.platform(string) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/user_agent.rb', line 158

def self.platform(string)
  case string
    when Platform::WindowsPhone  then :windows_phone
    when Platform::Windows       then :windows
    when Platform::Mac           then :macintosh
    when Platform::Android       then :android
    when Platform::Blackberry    then :blackberry
    when Platform::Linux         then :linux
    when Platform::Wii           then :wii
    when Platform::Playstation   then :playstation
    when Platform::Ipad          then :ipad
    when Platform::Ipod          then :ipod
    when Platform::Iphone        then :iphone
    when Platform::Symbian       then :symbian
    else
      :unknown
  end
end

Instance Method Details

#engineObject



191
192
193
# File 'lib/user_agent.rb', line 191

def engine
  @engine ||= self.class.engine(source)
end

#engine_versionObject



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: ==

Returns:

  • (Boolean)


223
224
225
# File 'lib/user_agent.rb', line 223

def eql?(other)
  self.class.eql?(other.class) && source == other.source
end

#inspectObject



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

Returns:

  • (Boolean)


207
208
209
# File 'lib/user_agent.rb', line 207

def mobile?
  MobilePlatforms.include?(platform) || name == :psp
end

#nameObject



183
184
185
# File 'lib/user_agent.rb', line 183

def name
  @name ||= self.class.browser_name(source)
end

#osObject



199
200
201
# File 'lib/user_agent.rb', line 199

def os
  @os ||= self.class.os(source)
end

#platformObject



203
204
205
# File 'lib/user_agent.rb', line 203

def platform
  @platform ||= self.class.platform(source)
end

#to_sObject



211
212
213
# File 'lib/user_agent.rb', line 211

def to_s
  @source
end

#versionObject



187
188
189
# File 'lib/user_agent.rb', line 187

def version
  @version ||= self.class.browser_version(source)
end