Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moving Opera specific parse into Opera module

  • Loading branch information...
commit ef1015ea60bcc96f6f288b803f74c2bf1c5cb4c4 1 parent ee4c07d
@bartimaeus authored
Showing with 39 additions and 25 deletions.
  1. +8 −25 lib/user_agent.rb
  2. +31 −0 lib/user_agent/browsers/opera.rb
View
33 lib/user_agent.rb
@@ -11,14 +11,6 @@ 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)
@@ -26,22 +18,13 @@ def self.parse(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 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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
View
31 lib/user_agent/browsers/opera.rb
@@ -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
@bartimaeus

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!

Please sign in to comment.
Something went wrong with that request. Please try again.