Sha256: 32ab189f1ebb18701cbe0799a33476d48ade251e8b5405ed219913437fc0a9c7
Contents?: true
Size: 1.67 KB
Versions: 2
Compression:
Stored size: 1.67 KB
Contents
require 'user_agent/comparable' require 'user_agent/browsers' require 'user_agent/operating_systems' class UserAgent # http://www.texsoft.it/index.php?m=sw.php.useragent MATCHER = %r{ ^([^/\s]+) # Product /?([^\s]*) # Version (\s\(([^\)]*)\))? # Comment }x.freeze def self.parse(string) agents = [] while m = string.match(MATCHER) agent = new(m[1], m[2], m[4]) agents << agent string = string.sub(m[0], '').strip end Browsers.extend(agents) agents end attr_reader :product, :version, :comment def initialize(product, version = nil, comment = nil) if product @product = product else raise ArgumentError, "expected a value for product" end if version && !version.empty? @version = version else @version = nil end if comment.respond_to?(:split) @comment = comment.split("; ") else @comment = comment end end include Comparable # Any comparsion between two user agents with different products will # always return false. def <=>(other) if @product == other.product if @version && other.version @version <=> other.version else 0 end else false end end def eql?(other) @product == other.product && @version == other.version && @comment == other.comment end def to_s to_str end def to_str if @product && @version && @comment "#{@product}/#{@version} (#{@comment.join("; ")})" elsif @product && @version "#{@product}/#{@version}" elsif @product && @comment "#{@product} (#{@comment.join("; ")})" else @product end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
useragent-0.1.1 | lib/user_agent.rb |
useragent-0.1.0 | lib/user_agent.rb |