Class: UserAgent

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/user_agent.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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparable

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

Constructor Details

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

Returns a new instance of UserAgent.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/user_agent.rb', line 26

def initialize(product, version = nil, comment = nil, original = nil)
  if product
    @product = product
  else
    raise ArgumentError, "expected a value for product"
  end

  if version && version.any?
    @version = version
  end

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

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



24
25
26
# File 'lib/user_agent.rb', line 24

def comment
  @comment
end

#originalObject

Returns the value of attribute original.



24
25
26
# File 'lib/user_agent.rb', line 24

def original
  @original
end

#productObject

Returns the value of attribute product.



24
25
26
# File 'lib/user_agent.rb', line 24

def product
  @product
end

#versionObject

Returns the value of attribute version.



24
25
26
# File 'lib/user_agent.rb', line 24

def version
  @version
end

Class Method Details

.parse(string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/user_agent.rb', line 13

def self.parse(string)
  agents = []
  while m = string.match(MATCHER)
    agent = new(m[1], m[2], m[4], string)
    agents << agent
    string = string.sub(agent.to_s, '').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.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/user_agent.rb', line 50

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)


62
63
64
65
66
# File 'lib/user_agent.rb', line 62

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

#to_sObject



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

def to_s
  to_str
end

#to_strObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/user_agent.rb', line 72

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