Class: WatchDoge::Worker
- Inherits:
-
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
56
57
58
|
# File 'lib/watchdoge/worker.rb', line 23
def initialize uid, options={}
@http = Selenium::WebDriver::Remote::Http::Default.new
@browser = WatchDoge.configuration.browser
@http.read_timeout = options.delete(:timeout) || WatchDoge.configuration.http_timeout
@uid = uid
default_options = {
width: 1280,
height: 768,
headless: true,
http_client: @http
}.merge(options).merge(browser_profile)
WatchDoge::WebdriverManager.new @browser
@core = WatchDoge.configuration.engine.new @browser, default_options
@landed_page = nil
@cookie_autosave = false
@cookie_loaded = false
@width = default_options[:width]
@height = default_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
try delegate all missing methods to selenium
150
151
152
153
154
|
# File 'lib/watchdoge/worker.rb', line 150
def method_missing(method_name, *args)
result = @core.__send__(method_name, *args)
result
end
|
Instance Method Details
#close ⇒ Object
66
67
68
69
|
# File 'lib/watchdoge/worker.rb', line 66
def close
save_cookie
@core.close
end
|
#fullpage_screenshot(**kwargs) ⇒ Object
60
61
62
63
64
|
# File 'lib/watchdoge/worker.rb', line 60
def fullpage_screenshot **kwargs
resize_by_viewport(kwargs) do |resize|
return resize.screenshot
end
end
|
#goto(uri) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/watchdoge/worker.rb', line 71
def goto uri
@core.goto uri
@landed_page = uri
load_cookie unless cookie_loaded?
@core.refresh
end
|
#resize_by_viewport(**kwargs) {|@core| ... } ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/watchdoge/worker.rb', line 81
def resize_by_viewport **kwargs
width = (kwargs.delete :width) || @core.body.width
height = (kwargs.delete :height) || @core.body.height
sleep_sec = kwargs.delete :wait
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
puts " -> wait rendering #{sleep_sec} sec" if sleep_sec
sleep sleep_sec.to_i
yield(@core)
@core.window.resize_to @width, @height
end
|