Class: UserAgent

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/user_agent.rb,
lib/user_agent/version.rb,
lib/user_agent/browsers.rb,
lib/user_agent/comparable.rb,
lib/user_agent/browsers/all.rb,
lib/user_agent/browsers/gecko.rb,
lib/user_agent/browsers/opera.rb,
lib/user_agent/browsers/webkit.rb,
lib/user_agent/operating_systems.rb,
lib/user_agent/browsers/internet_explorer.rb

Defined Under Namespace

Modules: Browsers, Comparable, OperatingSystems Classes: Version

Constant Summary collapse

MATCHER =
%r{
  ^([^/\s]+)        # Product
  /?([^\s]*)        # Version
  (\s\(([^\)]*)\))? # Comment
}x.freeze
DEFAULT_USER_AGENT =
"Mozilla/4.0 (compatible)"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #==, #>, #>=

Constructor Details

#initialize(product, version = nil, comment = nil) ⇒ UserAgent

Returns a new instance of UserAgent.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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 = nil
  end

  if comment.respond_to?(:split)
    @comment = comment.split("; ")
  else
    @comment = comment
  end
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



30
31
32
# File 'lib/user_agent.rb', line 30

def comment
  @comment
end

#productObject (readonly)

Returns the value of attribute product.



30
31
32
# File 'lib/user_agent.rb', line 30

def product
  @product
end

#versionObject (readonly)

Returns the value of attribute version.



30
31
32
# File 'lib/user_agent.rb', line 30

def version
  @version
end

Class Method Details

.parse(string) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/user_agent.rb', line 16

def self.parse(string)
  if string.nil? || string == ""
    string = DEFAULT_USER_AGENT
  end

  agents = []
  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)
  agents
end

Instance Method Details

#<=>(other) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/user_agent.rb', line 56

def <=>(other)
  if @product == other.product
    if @version && other.version
      @version <=> other.version
    else
      0
    end
  else
    false
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/user_agent.rb', line 68

def eql?(other)
  @product == other.product &&
    @version == other.version &&
    @comment == other.comment
end

#to_sObject



74
75
76
# File 'lib/user_agent.rb', line 74

def to_s
  to_str
end

#to_strObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/user_agent.rb', line 78

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