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) ⇒ UserAgent

Returns a new instance of UserAgent.



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

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

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



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

def comment
  @comment
end

#productObject (readonly)

Returns the value of attribute product.



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

def product
  @product
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.parse(string) ⇒ Object



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

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



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

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)


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

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

#to_sObject



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

def to_s
  to_str
end

#to_strObject



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

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