Class: UserAgent
- Inherits:
-
Object
- Object
- UserAgent
- 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
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#product ⇒ Object
readonly
Returns the value of attribute product.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Any comparsion between two user agents with different products will always return false.
- #eql?(other) ⇒ Boolean
-
#initialize(product, version = nil, comment = nil) ⇒ UserAgent
constructor
A new instance of UserAgent.
- #to_s ⇒ Object
- #to_str ⇒ Object
Methods included from Comparable
Constructor Details
#initialize(product, version = nil, comment = 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 |
# File 'lib/user_agent.rb', line 26 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 end if comment.respond_to?(:split) @comment = comment.split("; ") else @comment = comment end end |
Instance Attribute Details
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
24 25 26 |
# File 'lib/user_agent.rb', line 24 def comment @comment end |
#product ⇒ Object (readonly)
Returns the value of attribute product.
24 25 26 |
# File 'lib/user_agent.rb', line 24 def product @product end |
#version ⇒ Object (readonly)
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]) 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.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/user_agent.rb', line 48 def <=>(other) if @product == other.product if @version && other.version @version <=> other.version else 0 end else false end end |
#eql?(other) ⇒ Boolean
60 61 62 63 64 |
# File 'lib/user_agent.rb', line 60 def eql?(other) @product == other.product && @version == other.version && @comment == other.comment end |
#to_s ⇒ Object
66 67 68 |
# File 'lib/user_agent.rb', line 66 def to_s to_str end |
#to_str ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/user_agent.rb', line 70 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 |