Class: DoubleAgent::BrowserParser
- Inherits:
-
Object
- Object
- DoubleAgent::BrowserParser
- Defined in:
- lib/double_agent/parser.rb
Overview
Each browser in BROWSER_DATA gets its own BrowserParser object. These parser objects are then used to parse specific data out of a user agent string.
Instance Attribute Summary collapse
-
#family_sym ⇒ Object
readonly
The browser family name as a symbol.
-
#name ⇒ Object
readonly
The browser name.
-
#sym ⇒ Object
readonly
The browser name as a symbol.
Instance Method Summary collapse
-
#browser(ua = nil) ⇒ Object
Returns the browser’s name.
-
#family ⇒ Object
Returns the BrowserParser for this BrowserParser object’s Family.
-
#initialize(sym, attrs = {}) ⇒ BrowserParser
constructor
Instantiate a new BrowserParser using a “browser family” element from BROWSER_DATA.
-
#version(ua) ⇒ Object
Attempts to parse and return the browser’s version from a user agent string.
Constructor Details
#initialize(sym, attrs = {}) ⇒ BrowserParser
Instantiate a new BrowserParser using a “browser family” element from BROWSER_DATA
13 14 15 16 17 18 19 20 |
# File 'lib/double_agent/parser.rb', line 13 def initialize(sym, attrs={}) @sym = sym @family_sym = attrs[:family_sym] || @sym @name = attrs[:name] if attrs[:version] @version_pattern = Regexp.new(attrs[:version], Regexp::IGNORECASE) end end |
Instance Attribute Details
#family_sym ⇒ Object (readonly)
The browser family name as a symbol
10 11 12 |
# File 'lib/double_agent/parser.rb', line 10 def family_sym @family_sym end |
#name ⇒ Object (readonly)
The browser name
6 7 8 |
# File 'lib/double_agent/parser.rb', line 6 def name @name end |
#sym ⇒ Object (readonly)
The browser name as a symbol
8 9 10 |
# File 'lib/double_agent/parser.rb', line 8 def sym @sym end |
Instance Method Details
#browser(ua = nil) ⇒ Object
Returns the browser’s name. If you provide a user agent string as an argument, it will attempt to also return the major version number. E.g. “Firefox 4”.
24 25 26 |
# File 'lib/double_agent/parser.rb', line 24 def browser(ua=nil) (ua and @version_pattern) ? "#{name} #{version(ua)}" : name end |
#family ⇒ Object
Returns the BrowserParser for this BrowserParser object’s Family. E.g. the Chrome BrowserParser would return the Chromium BrowserParser. For browsers that are their own family (e.g. Firefox, IE) it will end up returning itself.
37 38 39 |
# File 'lib/double_agent/parser.rb', line 37 def family BROWSER_PARSERS[family_sym] end |
#version(ua) ⇒ Object
Attempts to parse and return the browser’s version from a user agent string. Returns nil if nothing is found.
30 31 32 |
# File 'lib/double_agent/parser.rb', line 30 def version(ua) ua.slice(@version_pattern) end |