Permalink
Browse files

Moving Opera specific parse into Opera module

  • Loading branch information...
1 parent ee4c07d commit ef1015ea60bcc96f6f288b803f74c2bf1c5cb4c4 @bartimaeus committed Mar 24, 2012
Showing with 39 additions and 25 deletions.
  1. +8 −25 lib/user_agent.rb
  2. +31 −0 lib/user_agent/browsers/opera.rb
View
@@ -11,37 +11,20 @@ class UserAgent
(\s\(([^\)]*)\))? # Comment
}x.freeze
- # http://my.opera.com/community/openweb/idopera/
- OPERA_MATCHER = %r{
- ^([^/\s]+) # Product
- /?([^\s]*) # Old Version
- (\s\(([^\)]*)\))? # Comment
- .*(Version/([^/\s]+)) # Version
- }x.freeze
-
DEFAULT_USER_AGENT = "Mozilla/4.0 (compatible)"
def self.parse(string)
if string.nil? || string == ""
string = DEFAULT_USER_AGENT
end
- agents = []
-
- # Opera has a different user agent since version 10
- # Check for that version and then continue matching
- product = string.to_s.match(MATCHER)[1]
- if product && product == "Opera" && string =~ /Version\/\d+/
- match = string.to_s.match(OPERA_MATCHER)
- agents << new(match[1], match[6], match[4])
- # Trim the string based on the original matcher and continue
- standard_match = string.to_s.match(MATCHER)
- string = string[standard_match[0].length..-1].strip
- end
-
- while m = string.to_s.match(MATCHER)
- agents << new(m[1], m[2], m[4])
- string = string[m[0].length..-1].strip
+ agents = UserAgent::Browsers::Opera.parse(string)
bartimaeus
bartimaeus Mar 24, 2012 Owner

I'm not sure if this is the best way to do it, but I've moved this into the Opera module. Please let me know how I can make this better. Thanks!

+ if agents.nil?
+ agents = []
+ while m = string.to_s.match(MATCHER)
+ agents << new(m[1], m[2], m[4])
+ string = string[m[0].length..-1].strip
+ end
end
Browsers.extend(agents)
agents
@@ -106,4 +89,4 @@ def to_str
@product
end
end
-end
+end
@@ -1,6 +1,37 @@
class UserAgent
module Browsers
module Opera
+ # http://my.opera.com/community/openweb/idopera/
+ OPERA_MATCHER = %r{
+ ^([^/\s]+) # Product
+ /?([^\s]*) # Old Version
+ (\s\(([^\)]*)\))? # Comment
+ .*(Version/([^/\s]+)) # Version
+ }x.freeze
+
+ def self.parse(string)
+ return nil if string.nil? || string == ""
+
+ agents = []
+ product = string.to_s.match(MATCHER)[1]
+ if product && product == "Opera" && string =~ /Version\/\d+/
+ match = string.to_s.match(OPERA_MATCHER)
+ agents << UserAgent.new(match[1], match[6], match[4])
+
+ # Trim the string based on the original matcher and continue
+ standard_match = string.to_s.match(MATCHER)
+ string = string[standard_match[0].length..-1].strip
+
+ while m = string.to_s.match(MATCHER)
+ agents << UserAgent.new(m[1], m[2], m[4])
+ string = string[m[0].length..-1].strip
+ end
+ agents
+ else
+ nil
+ end
+ end
+
def self.extend?(agent)
agent.application && agent.application.product == "Opera"
end

0 comments on commit ef1015e

Please sign in to comment.