Class: WurflDevice::UserAgent

Inherits:
String
  • Object
show all
Defined in:
lib/wurfl_device/user_agent.rb

Instance Method Summary collapse

Constructor Details

#initialize(str = '') ⇒ UserAgent

Returns a new instance of UserAgent.



4
5
6
7
8
9
10
11
12
13
# File 'lib/wurfl_device/user_agent.rb', line 4

def initialize(str='')
  super(str)
  unless self.valid_encoding?
    # TODO need to convert this String#encode, depreciated in ruby 1.9.3
    require 'iconv'
    ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
    self.replace ic.iconv(self << ' ')[0..-2]
  end
  self.strip!
end

Instance Method Details

#cleanedObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/wurfl_device/user_agent.rb', line 133

def cleaned
  user_agent = self.strip.dup
  user_agent.sub!('UP.Link', '')
  user_agent.replace($1) if user_agent =~ /^(.+)NOKIA-MSISDN\:\ (.+)$/i
  user_agent.sub!("'M', 'Y' 'P', 'H', 'O', 'N', 'E'", "MyPhone")
  user_agent.sub!('(null)', '')

  # remove serial numbers
  user_agent.sub!(/\/SN\d{15}/, '/SNXXXXXXXXXXXXXXX')
  user_agent.sub!(/\[(ST|TF|NT)\d+\]/, '')

  # remove locale identifiers
  user_agent.sub!(/([ ;])[a-zA-Z]{2}-[a-zA-Z]{2}([ ;\)])/, '\1xx-xx\2')

  pos = user_agent.index('BlackBerry')
  user_agent.replace(user_agent.slice(pos, user_agent.length-pos)) unless pos.nil?

  user_agent.sub!(/(Android \d\.\d)([^; \/\)]+)/, '\1')
  return user_agent
end

#contains(find, ignore_case = false) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wurfl_device/user_agent.rb', line 71

def contains(find, ignore_case=false)
  user_agent = self.downcase if ignore_case
  user_agent = self unless ignore_case
  if find.kind_of?(Array)
    find.map do |needle|
      needle = needle.downcase if ignore_case
      return true unless user_agent.index(needle).nil?
    end
    return false
  else
    find = find.downcase if ignore_case
    return true unless user_agent.index(find).nil?
    return false
  end
end

#first_open_parenObject



65
66
67
68
69
# File 'lib/wurfl_device/user_agent.rb', line 65

def first_open_paren
  pos = self.index('(')
  return self.length if pos.nil?
  return pos
end

#first_slashObject



41
42
43
44
45
# File 'lib/wurfl_device/user_agent.rb', line 41

def first_slash
  pos = self.index('/')
  return self.length if pos.nil?
  return pos
end

#first_spaceObject



59
60
61
62
63
# File 'lib/wurfl_device/user_agent.rb', line 59

def first_space
  pos = self.index(' ')
  return self.length if pos.nil?
  return pos
end

#index_of_or_length(target, starting_index = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/wurfl_device/user_agent.rb', line 103

def index_of_or_length(target, starting_index=nil)
  length = self.length
  return length if starting_index.nil?
  if target.kind_of?(Array)
    target.map do |needle|
      next if needle.nil?
      pos = self.index(needle, starting_index)
      return pos unless pos.nil?
    end
    return length
  else
    pos = self.index(target, starting_index)
    return length if pos.nil?
    return pos unless pos.nil?
  end
end

#is_desktop_browser?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/wurfl_device/user_agent.rb', line 15

def is_desktop_browser?
  ua = self.downcase
  Settings::DESKTOP_BROWSERS.each do |sig|
    return true if ua.index(sig)
  end
  return false
end

#is_mobile_browser?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/wurfl_device/user_agent.rb', line 23

def is_mobile_browser?
  ua = self.downcase
  return false if self.is_desktop_browser?
  return true if ua =~ /[^\d]\d{3}x\d{3}/
  Settings::DESKTOP_BROWSERS.each do |sig|
    return true if ua.index(sig)
  end
  return false
end

#is_robot?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/wurfl_device/user_agent.rb', line 33

def is_robot?
  ua = self.downcase
  Settings::ROBOTS.each do |sig|
    return true if ua.index(sig)
  end
  return false
end

#manufacturerObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/wurfl_device/user_agent.rb', line 154

def manufacturer
  # Process MOBILE user agents
  unless self.is_desktop_browser?
    return 'Nokia' if self.contains('Nokia')
    return 'Samsung' if self.contains(['Samsung/SGH', 'SAMSUNG-SGH']) || self.starts_with(['SEC-', 'Samsung', 'SAMSUNG', 'SPH', 'SGH', 'SCH']) || self.starts_with('samsung', true)
    return 'BlackBerry' if self.contains('blackberry', true)
    return 'SonyEricsson' if self.contains('Sony')
    return 'Motorola' if self.starts_with(['Mot-', 'MOT-', 'MOTO', 'moto']) || self.contains('Motorola')

    return 'Alcatel' if self.starts_with('alcatel', true)
    return 'Apple' if self.contains(['iPhone', 'iPod', 'iPad', '(iphone'])
    return 'BenQ' if self.starts_with('benq', true)
    return 'DoCoMo' if self.starts_with('DoCoMo')
    return 'Grundig' if self.starts_with('grundig', true)
    return 'HTC' if self.contains(['HTC', 'XV6875'])
    return 'Kddi' if self.contains('KDDI-')
    return 'Kyocera' if self.starts_with(['kyocera', 'QC-', 'KWC-'])
    return 'LG' if self.starts_with('lg', true)
    return 'Mitsubishi' if self.starts_with('Mitsu')
    return 'Nec' if self.starts_with(['NEC-', 'KGT'])
    return 'Nintendo' if self.contains('Nintendo') || (self.starts_with('Mozilla/') && self.starts_with('Nitro') && self.starts_with('Opera'))
    return 'Panasonic' if self.contains('Panasonic')
    return 'Pantech' if self.starts_with(['Pantech', 'PT-', 'PANTECH', 'PG-'])
    return 'Philips' if self.starts_with('philips', true)
    return 'Portalmmm' if self.starts_with('portalmmm')
    return 'Qtek' if self.starts_with('Qtek')
    return 'Sagem' if self.starts_with('sagem', true)
    return 'Sharp' if self.starts_with('sharp', true)
    return 'Siemens' if self.starts_with('SIE-')
    return 'SPV' if self.starts_with('SPV')
    return 'Toshiba' if self.starts_with('Toshiba')
    return 'Vodafone' if self.starts_with('Vodafone')

    # mobile browsers
    return 'Android' if self.contains('Android')
    return 'OperaMini' if self.contains(['Opera Mini', 'Opera Mobi'])
    return 'WindowsCE' if self.contains('Mozilla/') && self.contains('Windows CE')
  end

  # Process Robots (Web Crawlers and the like)
  return 'Bot' if self.is_robot?

  # Process NON-MOBILE user agents
  unless self.is_mobile_browser?
    return 'MSIE' if self.starts_with('Mozilla') && self.contains('MSIE') && !self.contains(['Opera', 'armv', 'MOTO', 'BREW'])
    return 'Firefox' if self.contains('Firefox') && !self.contains(['Sony', 'Novarra', 'Opera'])
    return 'Chrome' if self.contains('Chrome') 
    return 'Konqueror' if self.contains('Konqueror')
    return 'Opera' if self.contains('Opera')
    return 'Safari' if self.starts_with('Mozilla') && self.contains('Safari')
    return 'AOL' if self.contains(['AOL', 'America Online']) || self.contains('aol 9', true)
  end

  return 'CatchAll'
end

#num_slashesObject



55
56
57
# File 'lib/wurfl_device/user_agent.rb', line 55

def num_slashes
  self.count('/')
end

#ordinal_index_of(needle, ordinal) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wurfl_device/user_agent.rb', line 120

def ordinal_index_of(needle, ordinal)
  return -1 if self.nil? || self.empty? || ordinal.is_a?(Integer)
  found = 0
  index = -1
  begin
    index = self.index(needle, index + 1)
    index = index.is_a?(Integer) ? index : -1
    return index if index < 0
    found = found + 1
  end while found < ordinal
  return index
end

#second_slashObject



47
48
49
50
51
52
53
# File 'lib/wurfl_device/user_agent.rb', line 47

def second_slash
  first = self.index('/')
  return self.length if first.nil?
  second = self.index('/', first + 1)
  return self.length if second.nil?
  return second
end

#starts_with(find, ignore_case = false) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wurfl_device/user_agent.rb', line 87

def starts_with(find, ignore_case=false)
  user_agent = self.downcase if ignore_case
  user_agent = self unless ignore_case
  if find.kind_of?(Array)
    find.map do |needle|
      needle = needle.downcase if ignore_case
      return true if user_agent.index(needle) == 0
    end
    return false
  else
    find = find.downcase if ignore_case
    return true if user_agent.index(find) == 0
    return false
  end
end