Class: Browscap::Parser
- Inherits:
-
Object
- Object
- Browscap::Parser
- Defined in:
- lib/browscap/parser.rb
Instance Method Summary collapse
-
#initialize(filename = File.join(File.dirname(__FILE__), '../..', 'ini', 'default.ini'), opts = {}) ⇒ Parser
constructor
A new instance of Parser.
-
#query(user_agent) ⇒ Object
(also: #=~)
Looks up the given user agent string and returns a dictionary containing information on this browser or bot.
Constructor Details
#initialize(filename = File.join(File.dirname(__FILE__), '../..', 'ini', 'default.ini'), opts = {}) ⇒ Parser
Returns a new instance of Parser.
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 67 68 69 |
# File 'lib/browscap/parser.rb', line 6 def initialize(filename = File.join(File.dirname(__FILE__), '../..', 'ini', 'default.ini'), opts={}) @@user_agent_properties ||= {} @@user_agent_regexps ||= {} @@match_cache ||= {} @@encoding = opts[:encoding] || 'ISO-8859-1' if @@user_agent_properties.empty? || @@user_agent_regexps.empty? ini = IniFile.load(filename, :encoding => @@encoding) # 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.banned = properties['isBanned'].downcase == 'true' browser.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).force_encoding(@@encoding)) end end end |
Instance Method Details
#query(user_agent) ⇒ Object Also known as: =~
Looks up the given user agent string and returns a dictionary containing information on this browser or bot.
72 73 74 75 |
# File 'lib/browscap/parser.rb', line 72 def query(user_agent) section = match(user_agent) @@user_agent_properties[section] end |