forked from rubycdp/ferrum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: Update puma, jquery for tests * feat: Implement downloads
- Loading branch information
Showing
19 changed files
with
291 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ferrum | ||
class Downloads | ||
VALID_BEHAVIOR = %i[deny allow allowAndName default].freeze | ||
|
||
def initialize(page) | ||
@page = page | ||
@event = Event.new.tap(&:set) | ||
@files = {} | ||
end | ||
|
||
def files | ||
@files.values | ||
end | ||
|
||
def wait(timeout = 5) | ||
@event.reset | ||
yield if block_given? | ||
@event.wait(timeout) | ||
@event.set | ||
end | ||
|
||
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 | ||
|
||
def subscribe | ||
subscribe_download_will_begin | ||
subscribe_download_progress | ||
end | ||
|
||
def subscribe_download_will_begin | ||
@page.on("Browser.downloadWillBegin") do |params| | ||
@event.reset | ||
@files[params["guid"]] = params | ||
end | ||
end | ||
|
||
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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ferrum | ||
class Event < Concurrent::Event | ||
def iteration | ||
synchronize { @iteration } | ||
end | ||
|
||
def reset | ||
synchronize do | ||
@iteration += 1 | ||
@set = false if @set | ||
@iteration | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.