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/base.rb,
lib/user_agent/browsers/edge.rb,
lib/user_agent/browsers/gecko.rb,
lib/user_agent/browsers/opera.rb,
lib/user_agent/browsers/chrome.rb,
lib/user_agent/browsers/itunes.rb,
lib/user_agent/browsers/webkit.rb,
lib/user_agent/browsers/vivaldi.rb,
lib/user_agent/operating_systems.rb,
lib/user_agent/browsers/libavformat.rb,
lib/user_agent/browsers/playstation.rb,
lib/user_agent/browsers/podcast_addict.rb,
lib/user_agent/browsers/wechat_browser.rb,
lib/user_agent/browsers/apple_core_media.rb,
lib/user_agent/browsers/internet_explorer.rb,
lib/user_agent/browsers/windows_media_player.rb

Defined Under Namespace

Modules: Browsers, Comparable, OperatingSystems Classes: Version

Constant Summary collapse

MATCHER =
%r{
  ^['"]*                         # Possible opening quote(s)
  ([^/\s]+)                      # Product
  /?([^\s,]*)                    # Version
  (\s\(([^\)]*)\)|,gzip\(gfe\))? # 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


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 = Version.new
  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


17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#<=>(other) ⇒ Object

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


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

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

#detect_comment(&block) ⇒ Object


54
55
56
# File 'lib/user_agent.rb', line 54

def detect_comment(&block)
  comment && comment.detect(&block)
end

#eql?(other) ⇒ 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.nil? && @comment
    "#{@product}/#{@version} (#{@comment.join("; ")})"
  elsif @product && !@version.nil?
    "#{@product}/#{@version}"
  elsif @product && @comment
    "#{@product} (#{@comment.join("; ")})"
  else
    @product
  end
end