Class: Ferrum::Downloads

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/downloads.rb

Constant Summary collapse

VALID_BEHAVIOR =
%i[deny allow allowAndName default].freeze

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Downloads

Returns a new instance of Downloads.



7
8
9
10
11
# File 'lib/ferrum/downloads.rb', line 7

def initialize(page)
  @page = page
  @event = Utils::Event.new.tap(&:set)
  @files = {}
end

Instance Method Details

#filesObject



13
14
15
# File 'lib/ferrum/downloads.rb', line 13

def files
  @files.values
end

#set_behavior(save_path:, behavior: :allow) ⇒ Object

Raises:

  • (ArgumentError)


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

#subscribeObject



35
36
37
38
# File 'lib/ferrum/downloads.rb', line 35

def subscribe
  subscribe_download_will_begin
  subscribe_download_progress
end

#subscribe_download_progressObject



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_beginObject



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