Class: WatchDoge::Worker

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

Instance Method Summary collapse

Constructor Details

#initialize(uid, options) ⇒ Worker

options:

1. width: browser's width, default to 1280
2. height: browser's height, default to 768
3. http_client: instance of Selenium::WebDriver::Remote::Http
4. all acceptable arguments of Watir::Browser class

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
# File 'lib/watchdoge/worker.rb', line 23

def initialize uid, options
  @http = Selenium::WebDriver::Remote::Http::Default.new

  # HTTP timeout default to 120 sec
  @http.read_timeout = options.delete(:timeout)

  @browser = options[:browser]

  @uid = uid

  options = {
    # Selenium HTTP configuration
    http_client: @http
  }.merge(options).merge(default_browser_profile)

  # intall required version driver
  WatchDoge::WebdriverManager.new @browser
  @core = WatchDoge.configuration.engine.new @browser, options

  # for generate uri hash
  @landed_page = nil

  # autosaving cookies while worker closed
  @cookie_autosave = false

  # flag for cookies loaded
  @cookie_loaded = false

  # width/height
  @width = options[:width]
  @height = options[:height]
  @core.window.resize_to @width, @height
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)

try delegate all missing methods to selenium


147
148
149
150
151
# File 'lib/watchdoge/worker.rb', line 147

def method_missing(method_name, *args)
  result = @core.__send__(method_name, *args)

  result
end

Instance Method Details

#closeObject


63
64
65
66
# File 'lib/watchdoge/worker.rb', line 63

def close
  save_cookie
  @core.close
end

#fullpage_screenshot(**kwargs) ⇒ Object


57
58
59
60
61
# File 'lib/watchdoge/worker.rb', line 57

def fullpage_screenshot **kwargs
  resize_by_viewport(kwargs) do |resize|
    return resize.screenshot
  end
end

#goto(uri) ⇒ Object


68
69
70
71
72
73
74
75
76
# File 'lib/watchdoge/worker.rb', line 68

def goto uri
  @core.goto uri

  @landed_page = uri

  load_cookie unless cookie_loaded?

  @core.refresh
end

#resize_by_viewport(**kwargs) {|@core| ... } ⇒ Object

Yields:

  • (@core)

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/watchdoge/worker.rb', line 78

def resize_by_viewport **kwargs
  width = (kwargs.delete :width) || @core.body.width
  height = (kwargs.delete :height) || @core.body.height

  # TODO: improve "wait" logic
  sleep_sec = kwargs.delete :wait

  # has element selector
  unless kwargs.empty?
    sleep 0.5
    target = @core.element(kwargs)
    width = [target.width, @core.body.width].max
    height = [target.height, @core.body.height].max
  end

  @core.window.resize_to [width, 5000].min, [height, 15000].min

  # TODO: improve "wait" logic
  puts "  -> wait rendering #{sleep_sec} sec" if sleep_sec
  sleep sleep_sec.to_i

  yield(@core)
  @core.window.resize_to @width, @height
end