Class: CyberarmEngine::Cache::DownloadManager::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/cache/download_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, save_as:, callback: nil) ⇒ Download

Returns a new instance of Download.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 60

def initialize(uri:, save_as:, callback: nil)
  @uri = uri
  @save_as = save_as
  @callback = callback

  @status = :pending

  @remaining_bytes = 0.0
  @total_downloaded_bytes = 0.0
  @total_bytes = 0.0

  @error_message = ""
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def callback
  @callback
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def error_message
  @error_message
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def finished_at
  @finished_at
end

#remaining_bytesObject (readonly)

Returns the value of attribute remaining_bytes.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def remaining_bytes
  @remaining_bytes
end

#save_asObject (readonly)

Returns the value of attribute save_as.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def save_as
  @save_as
end

#started_atObject (readonly)

Returns the value of attribute started_at.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def started_at
  @started_at
end

#statusObject

Returns the value of attribute status.



56
57
58
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 56

def status
  @status
end

#total_bytesObject (readonly)

Returns the value of attribute total_bytes.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def total_bytes
  @total_bytes
end

#total_downloaded_bytesObject (readonly)

Returns the value of attribute total_downloaded_bytes.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def total_downloaded_bytes
  @total_downloaded_bytes
end

#uriObject (readonly)

Returns the value of attribute uri.



57
58
59
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 57

def uri
  @uri
end

Instance Method Details

#downloadObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 81

def download
  @status = :downloading
  @started_at = Time.now # TODO: monotonic time

  io = File.open(@save_as, "w")
  streamer = lambda do |chunk, remaining_bytes, total_bytes|
    io.write(chunk)

    @remaining_bytes = remaining_bytes.to_f
    @total_downloaded_bytes += chunk.size
    @total_bytes = total_bytes.to_f
  end

  begin
    response = Excon.get(
      @uri.to_s,
      middlewares: Excon.defaults[:middlewares] + [Excon::Middleware::RedirectFollower],
      response_block: streamer
    )

    if response.status == 200
      @status = :finished
      @finished_at = Time.now # TODO: monotonic time
      @callback.call(self) if @callback
    else
      @error_message = "Got a non 200 HTTP status of #{response.status}"
      @status = :failed
      @finished_at = Time.now # TODO: monotonic time
      @callback.call(self) if @callback
    end
  rescue StandardError => e # TODO: cherrypick errors to cature
    @status = :failed
    @finished_at = Time.now # TODO: monotonic time
    @error_message = e.message
    @callback.call(self) if @callback
  end
ensure
  io.close if io
end

#progressObject



74
75
76
77
78
79
# File 'lib/cyberarm_engine/cache/download_manager.rb', line 74

def progress
  v = 1.0 - (@remaining_bytes.to_f / total_bytes)
  return 0.0 if v.nan?

  v
end