Class: Browscap
- Inherits:
-
Object
- Object
- Browscap
- Defined in:
- lib/browscap.rb
Instance Method Summary collapse
-
#_get_browser_props(ini, section) ⇒ Object
Recursively traverses the properties tree (based on ‘parent’ attribute of each section) and returns a dictionary of all browser properties for the given section name.
- #_match(user_agent) ⇒ Object
-
#initialize(filename = 'browscap.ini') ⇒ Browscap
constructor
A new instance of Browscap.
-
#query(user_agent) ⇒ Object
Looks up the given user agent string and returns a dictionary containing information on this browser or bot.
Constructor Details
#initialize(filename = 'browscap.ini') ⇒ Browscap
Returns a new instance of Browscap.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/browscap.rb', line 4 def initialize(filename = 'browscap.ini') @@user_agent_properties ||= {} @@user_agent_regexps ||= {} @@match_cache ||= {} if @@user_agent_properties.empty? || @@user_agent_regexps.empty? ini = IniFile.load(filename) # Remote meta sections ini.delete_section '*' ini.delete_section 'GJK_Browscap_Version' # Create a list of non-parent sections child_sections = ini.sections.dup ini.sections.each do |section| child_sections.delete ini[section]["Parent"] end # Populate user_agent_properties and user_agent_regexps child_sections.each do |section| properties = _get_browser_props(ini, section) browser = Browser.new browser.browser = properties['Browser'] browser.version = properties['Version'] browser.major_ver = properties['MajorVer'].to_i browser.minor_ver = properties['MinorVer'].to_i browser.platform = properties['Platform'] browser.alpha = properties['Alpha'].downcase == 'true' browser.beta = properties['Beta'].downcase == 'true' browser.win16 = properties['Win16'].downcase == 'true' browser.win32 = properties['Win32'].downcase == 'true' browser.win64 = properties['Win64'].downcase == 'true' browser.frames = properties['Frames'].downcase == 'true' browser.iframes = properties['IFrames'].downcase == 'true' browser.tables = properties['Tables'].downcase == 'true' browser. = properties['Cookies'].downcase == 'true' browser.background_sounds = properties['BackgroundSounds'].downcase == 'true' browser.javascript = properties['JavaScript'].downcase == 'true' browser.vbscript = properties['VBScript'].downcase == 'true' browser.java_applets = properties['JavaApplets'].downcase == 'true' browser.activex_controls = properties['ActiveXControls'].downcase == 'true' browser.is_banned = properties['isBanned'].downcase == 'true' browser.is_mobile_device = properties['isMobileDevice'].downcase == 'true' browser. = properties['isSyndicationReader'].downcase == 'true' browser.crawler = properties['Crawler'].downcase == 'true' browser.css_version = properties['CssVersion'].to_i browser.supports_css = properties['supportsCSS'].downcase == 'true' browser.aol_version = properties['aolVersion'].to_i browser.aol = properties['AOL'].downcase == 'true' @@user_agent_properties[section] = browser # Convert .ini file regexp syntax into ruby regexp syntax regexp = section.dup regexp.gsub! /([\^\$\(\)\[\]\.\-])/, "\\\\\\1" regexp.gsub! "?", "." regexp.gsub! "*", ".*?" @@user_agent_regexps[section] = Regexp.new("^%s$" % regexp) end end end |
Instance Method Details
#_get_browser_props(ini, section) ⇒ Object
Recursively traverses the properties tree (based on ‘parent’ attribute of each section) and returns a dictionary of all browser properties for the given section name. The properties lower in the tree override those higher in the tree.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/browscap.rb', line 93 def _get_browser_props(ini, section) data = {} if parent = ini[section]["Parent"] data.merge! _get_browser_props(ini, parent) end data.merge! ini[section] data end |
#_match(user_agent) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/browscap.rb', line 74 def _match(user_agent) return @@match_cache[user_agent] if @@match_cache[user_agent] matching_section = '' @@user_agent_regexps.each do |section, regexp| # Find the longest regexp that matches the given user_agent_string. The length check is needed since multiple reg-exps may match the user_agent_string. if regexp.match(user_agent) && section.length > matching_section.length matching_section = section end end @@match_cache[user_agent] = matching_section matching_section end |
#query(user_agent) ⇒ Object
Looks up the given user agent string and returns a dictionary containing information on this browser or bot.
69 70 71 72 |
# File 'lib/browscap.rb', line 69 def query(user_agent) section = _match(user_agent) @@user_agent_properties[section] end |