Class: Browscap

Inherits:
Object
  • Object
show all
Defined in:
lib/browscap.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename = File.join(File.dirname(__FILE__), '..', 'ini', 'default.ini')) ⇒ Browscap

Returns a new instance of Browscap.



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/browscap.rb', line 25

def initialize(filename = File.join(File.dirname(__FILE__), '..', 'ini', 'default.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.cookies = 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.is_syndication_reader = properties['isSyndicationReader'].downcase == 'true'
      browser.crawler = properties['Crawler'].downcase == 'true'
      browser.css_version = properties['CssVersion'].to_i
      browser.aol_version = properties['aolVersion'].to_i

      @@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

#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.



88
89
90
91
# File 'lib/browscap.rb', line 88

def query(user_agent)
  section = match(user_agent)
  @@user_agent_properties[section]
end