Class: Tanakai::BrowserBuilder::CupriteBuilder
- Inherits:
-
Object
- Object
- Tanakai::BrowserBuilder::CupriteBuilder
- Defined in:
- lib/tanakai/browser_builder/cuprite_builder.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#spider ⇒ Object
readonly
Returns the value of attribute spider.
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(config, spider:) ⇒ CupriteBuilder
constructor
A new instance of CupriteBuilder.
Constructor Details
#initialize(config, spider:) ⇒ CupriteBuilder
Returns a new instance of CupriteBuilder.
10 11 12 13 14 |
# File 'lib/tanakai/browser_builder/cuprite_builder.rb', line 10 def initialize(config, spider:) @config = config @spider = spider @logger = spider.logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/tanakai/browser_builder/cuprite_builder.rb', line 8 def logger @logger end |
#spider ⇒ Object (readonly)
Returns the value of attribute spider.
8 9 10 |
# File 'lib/tanakai/browser_builder/cuprite_builder.rb', line 8 def spider @spider end |
Instance Method Details
#build ⇒ Object
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 |
# File 'lib/tanakai/browser_builder/cuprite_builder.rb', line 16 def build # Register driver Capybara.register_driver :cuprite do |app| = { headless: ENV.fetch("HEADLESS", "true") == "true" } [:browser_options] = {'no-sandbox': nil} if Process.uid == 0 # root user logger.debug "BrowserBuilder (cuprite): enabled extensions" Capybara::Cuprite::Driver.new(app, ) end # Create browser instance (Capybara session) @browser = Capybara::Session.new(:cuprite) @browser.spider = spider logger.debug "BrowserBuilder (cuprite): created browser instance" # Headers if headers = @config[:headers].presence @browser.driver.headers = headers logger.debug "BrowserBuilder (cuprite): enabled custom headers" end if user_agent = @config[:user_agent].presence user_agent_string = (user_agent.class == Proc ? user_agent.call : user_agent).strip @browser.driver.headers = {"User-Agent" => user_agent_string} logger.debug "BrowserBuilder (cuprite): enabled custom user_agent" end # Cookies if = @config[:cookies].presence .each do || @browser.driver.([:name], [:value], ) end logger.debug "BrowserBuilder (cuprite): enabled custom cookies" end @browser end |