Module: UserAgent

Defined in:
lib/webdriver-user-agent-randomizer.rb

Class Method Summary collapse

Class Method Details

.driver(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/webdriver-user-agent-randomizer.rb', line 9

def self.driver options={} 
  options[:browser] ||= :firefox

  user_agent = ua_pool[Random.rand(ua_pool.size)]

  case options[:browser]
    when :firefox
      options[:profile] ||= Selenium::WebDriver::Firefox::Profile.new
      options[:profile]['general.useragent.override'] = user_agent
    when :chrome
      options[:switches] ||= []
      options[:switches] << "--user-agent=#{user_agent}"
    else
      raise "WebDriver UserAgent currently only supports :firefox and :chrome."
  end
  driver = Selenium::WebDriver.for options[:browser], options.except(:browser)
  driver
end

.ua_poolObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/webdriver-user-agent-randomizer.rb', line 28

def self.ua_pool
  @ua_pool = []
  ua_file = File.open(File.expand_path(UA_FILE, __FILE__), 'r') do |f|
    f.readlines
  end
  ua_file.each do |ua|
    @ua_pool << ua
  end
  @ua_pool
end