Class: Factorix::Progress::DownloadHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/factorix/progress/download_handler.rb

Overview

Download event handler for progress presenters

This class listens to download events and updates a progress presenter accordingly.

Instance Method Summary collapse

Constructor Details

#initialize(presenter) ⇒ DownloadHandler

Create a new download handler

Parameters:



12
# File 'lib/factorix/progress/download_handler.rb', line 12

def initialize(presenter) = @presenter = presenter

Instance Method Details

#on_cache_hit(event) ⇒ void

This method returns an undefined value.

Handle cache hit event

Parameters:

  • event (Dry::Events::Event)

    event with url, output, and total_size payload



36
37
38
39
40
41
42
43
# File 'lib/factorix/progress/download_handler.rb', line 36

def on_cache_hit(event)
  total_size = event.payload.fetch(:total_size, 1)

  # Start and complete immediately for cache hits
  @presenter.start(total: total_size)
  @presenter.update(total_size)
  @presenter.finish
end

#on_download_completed(_event) ⇒ void

This method returns an undefined value.

Handle download completed event

Parameters:

  • event (Dry::Events::Event)

    event with total_size payload



30
# File 'lib/factorix/progress/download_handler.rb', line 30

def on_download_completed(_event) = @presenter.finish

#on_download_progress(event) ⇒ void

This method returns an undefined value.

Handle download progress event

Parameters:

  • event (Dry::Events::Event)

    event with current_size payload



24
# File 'lib/factorix/progress/download_handler.rb', line 24

def on_download_progress(event) = @presenter.update(event[:current_size])

#on_download_started(event) ⇒ void

This method returns an undefined value.

Handle download started event

Parameters:

  • event (Dry::Events::Event)

    event with total_size payload



18
# File 'lib/factorix/progress/download_handler.rb', line 18

def on_download_started(event) = @presenter.start(total: event[:total_size])