class UserAgent

Constants

DEFAULT_USER_AGENT
MATCHER

www.texsoft.it/index.php?m=sw.php.useragent

Attributes

comment[R]
product[R]
version[R]

Public Class Methods

new(product, version = nil, comment = nil) click to toggle source
# File lib/user_agent.rb, line 32
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.new(version)
  else
    @version = Version.new
  end

  if comment.respond_to?(:split)
    @comment = comment.split("; ")
  else
    @comment = comment
  end
end
parse(string) click to toggle source
# File lib/user_agent.rb, line 17
def self.parse(string)
  if string.nil? || string.strip == ""
    string = DEFAULT_USER_AGENT
  end

  agents = Browsers::Base.new
  while m = string.to_s.match(MATCHER)
    agents << new(m[1], m[2], m[4])
    string = string[m[0].length..-1].strip
  end
  Browsers.extend(agents)
end

Public Instance Methods

<=>(other) click to toggle source

Any comparison between two user agents with different products will always return false.

# File lib/user_agent.rb, line 60
def <=>(other)
  if @product == other.product
    @version <=> other.version
  else
    false
  end
end
detect_comment(&block) click to toggle source
# File lib/user_agent.rb, line 54
def detect_comment(&block)
  comment && comment.detect(&block)
end
eql?(other) click to toggle source
# File lib/user_agent.rb, line 68
def eql?(other)
  @product == other.product &&
    @version == other.version &&
    @comment == other.comment
end
to_s() click to toggle source
# File lib/user_agent.rb, line 74
def to_s
  to_str
end
to_str() click to toggle source
# File lib/user_agent.rb, line 78
def to_str
  if @product && !@version.nil? && @comment
    "#{@product}/#{@version} (#{@comment.join("; ")})"
  elsif @product && !@version.nil?
    "#{@product}/#{@version}"
  elsif @product && @comment
    "#{@product} (#{@comment.join("; ")})"
  else
    @product
  end
end