Class: Ferrum::Downloads
- Inherits:
-
Object
- Object
- Ferrum::Downloads
- Defined in:
- lib/ferrum/downloads.rb
Constant Summary collapse
- VALID_BEHAVIOR =
%i[deny allow allowAndName default].freeze
Instance Method Summary collapse
- #files ⇒ Object
-
#initialize(page) ⇒ Downloads
constructor
A new instance of Downloads.
- #set_behavior(save_path:, behavior: :allow) ⇒ Object
- #subscribe ⇒ Object
- #subscribe_download_progress ⇒ Object
- #subscribe_download_will_begin ⇒ Object
- #wait(timeout = 5) ⇒ Object
Constructor Details
Instance Method Details
#files ⇒ Object
13 14 15 |
# File 'lib/ferrum/downloads.rb', line 13 def files @files.values end |
#set_behavior(save_path:, behavior: :allow) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ferrum/downloads.rb', line 24 def set_behavior(save_path:, behavior: :allow) raise ArgumentError unless VALID_BEHAVIOR.include?(behavior.to_sym) raise Error, "supply absolute path for `:save_path` option" unless Pathname.new(save_path.to_s).absolute? @page.command("Browser.setDownloadBehavior", browserContextId: @page.context_id, downloadPath: save_path, behavior: behavior, eventsEnabled: true) end |
#subscribe ⇒ Object
35 36 37 38 |
# File 'lib/ferrum/downloads.rb', line 35 def subscribe subscribe_download_will_begin subscribe_download_progress end |
#subscribe_download_progress ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ferrum/downloads.rb', line 47 def subscribe_download_progress @page.on("Browser.downloadProgress") do |params| @files[params["guid"]].merge!(params) case params["state"] when "completed", "canceled" @event.set else @event.reset end end end |
#subscribe_download_will_begin ⇒ Object
40 41 42 43 44 45 |
# File 'lib/ferrum/downloads.rb', line 40 def subscribe_download_will_begin @page.on("Browser.downloadWillBegin") do |params| @event.reset @files[params["guid"]] = params end end |
#wait(timeout = 5) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/ferrum/downloads.rb', line 17 def wait(timeout = 5) @event.reset yield if block_given? @event.wait(timeout) @event.set end |