Sha256: ce38eaf99f237cdef56b475c6e1abd3f55f3ce487c0b306dd2a639b127613473

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require "schur/version"
require 'capybara'
require 'capybara/poltergeist'
require 'mechanize'
require 'selenium-webdriver'

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, { js_errors: false, timeout: 100 })
end

Capybara.register_driver :firefox do |app|
  profile = ::Selenium::WebDriver::Firefox::Profile.new
  ::Capybara::Selenium::Driver.new(app, profile: profile)
end

IP_CHECKER_URL = 'https://api.ipify.org?format=json'
class Schur
  DRIVER_TYPES = [:mechanize, :poltergeist]
  class Proxy < Struct.new(:host, :port, :id, :pw)
  end

  def self.call(driver=:mechanize, proxy=nil)
    case driver
    when :mechanize
      agent = Mechanize.new
      if proxy
        agent.set_proxy(proxy.host, proxy.port, proxy.id, proxy.pw)
      end
      agent
    when :poltergeist
      if proxy
        ::Capybara.register_driver :poltergeist_with_proxy do |app|
          ::Capybara::Poltergeist::Driver.new(app,
            phantomjs_options: ["--proxy=#{proxy.host}:#{proxy.port}", "--proxy-auth=#{proxy.id}:#{proxy.pw}"],
            js_errors: false,
            timeout: 100
          )
        end
        Capybara::Session.new(:poltergeist_with_proxy)
      else
        Capybara::Session.new(:poltergeist)
      end
    when :firefox
      if proxy
        ::Capybara.register_driver :firefox_with_proxy do |app|
          profile = ::Selenium::WebDriver::Firefox::Profile.new
          # profile.proxy = Selenium::WebDriver::Proxy.new(http: format('%s:%s', proxy.host, proxy.port))

          profile["network.proxy.type"] = 1
          profile["network.proxy.http"] = "http://#{proxy.id}:#{proxy.pw}@#{proxy.host}/"
          profile["network.proxy.http_port"] = 31280
          ::Capybara::Selenium::Driver.new(app, profile: profile)
        end

        Capybara::Session.new(:firefox_with_proxy)
      else
        Capybara::Session.new(:firefox)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schur-0.1.0 lib/schur.rb