Class: Archaeo::DownloadState
- Inherits:
-
Object
- Object
- Archaeo::DownloadState
- Defined in:
- lib/archaeo/download_state.rb
Overview
Tracks download progress for resume support.
Persists completed snapshot metadata to a JSONL state file within the output directory, allowing interrupted downloads to resume without re-fetching already downloaded snapshots.
Constant Summary collapse
- STATE_FILE =
".archaeo-state"
Instance Attribute Summary collapse
-
#output_dir ⇒ Object
readonly
Returns the value of attribute output_dir.
Instance Method Summary collapse
- #clear ⇒ Object
- #completed?(timestamp) ⇒ Boolean
- #entry_for(timestamp) ⇒ Object
-
#initialize(output_dir) ⇒ DownloadState
constructor
A new instance of DownloadState.
- #mark_completed(timestamp, url: nil, bytes: nil) ⇒ Object
- #total_bytes ⇒ Object
Constructor Details
#initialize(output_dir) ⇒ DownloadState
Returns a new instance of DownloadState.
17 18 19 20 |
# File 'lib/archaeo/download_state.rb', line 17 def initialize(output_dir) @output_dir = output_dir @path = File.join(output_dir, STATE_FILE) end |
Instance Attribute Details
#output_dir ⇒ Object (readonly)
Returns the value of attribute output_dir.
15 16 17 |
# File 'lib/archaeo/download_state.rb', line 15 def output_dir @output_dir end |
Instance Method Details
#clear ⇒ Object
46 47 48 49 50 |
# File 'lib/archaeo/download_state.rb', line 46 def clear @entries = [] @entries_key = nil FileUtils.rm_f(@path) end |
#completed?(timestamp) ⇒ Boolean
22 23 24 |
# File 'lib/archaeo/download_state.rb', line 22 def completed?() entries_key.include?(.to_s) end |
#entry_for(timestamp) ⇒ Object
38 39 40 |
# File 'lib/archaeo/download_state.rb', line 38 def entry_for() entries.find { |e| e["ts"] == .to_s } end |
#mark_completed(timestamp, url: nil, bytes: nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/archaeo/download_state.rb', line 26 def mark_completed(, url: nil, bytes: nil) ts = .to_s return if entries_key.include?(ts) entry = { "ts" => ts, "at" => Time.now.utc.iso8601 } entry["url"] = url if url entry["bytes"] = bytes if bytes entries << entry @entries_key = nil save end |
#total_bytes ⇒ Object
42 43 44 |
# File 'lib/archaeo/download_state.rb', line 42 def total_bytes entries.sum { |e| e["bytes"].to_i } end |