Class: DoubleAgent::BrowserParser

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_symObject (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

#nameObject (readonly)

The browser name



6
7
8
# File 'lib/double_agent/parser.rb', line 6

def name
  @name
end

#symObject (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

#familyObject

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