Module: Eaternet::Util
- Defined in:
- lib/eaternet/util.rb
Class Method Summary collapse
-
.download(source:, dest:) ⇒ File
Download a file from the network.
-
.download_and_cache(source:, dest:) ⇒ File
Download a file from the network, using a 12-hour cache.
-
.download_and_extract_zipfile(url) ⇒ String
Download a zip file and extract it into a temp directory.
- .expired?(cache_path) ⇒ Boolean
-
.extract_zipfile(path:, dest_dir:) ⇒ Object
Extract a Zip archive.
- .file_age_in_days(path) ⇒ Float
- .generate_cache_path(url) ⇒ Object
-
.make_temp_dir ⇒ String
Create a temporary directory which is automatically deleted when the program exits.
- .prepare_cache_dir ⇒ Object
Class Method Details
.download(source:, dest:) ⇒ File
Download a file from the network.
47 48 49 50 51 |
# File 'lib/eaternet/util.rb', line 47 def self.download(source:, dest:) File.open(dest, 'wb') do |file| file << HTTParty.get(source, verify: false).body end end |
.download_and_cache(source:, dest:) ⇒ File
Download a file from the network, using a 12-hour cache.
34 35 36 37 38 39 40 |
# File 'lib/eaternet/util.rb', line 34 def self.download_and_cache(source:, dest:) # download(source: source, dest: dest) cache_path = generate_cache_path(source) download(source: source, dest: cache_path) if expired? cache_path FileUtils.cp cache_path, dest File.open dest end |
.download_and_extract_zipfile(url) ⇒ String
Download a zip file and extract it into a temp directory.
11 12 13 14 15 16 17 |
# File 'lib/eaternet/util.rb', line 11 def self.download_and_extract_zipfile(url) dir = make_temp_dir zip_path = File.join(dir, 'zip-file.zip') download_and_cache(source: url, dest: zip_path) extract_zipfile(path: zip_path, dest_dir: dir) dir end |
.expired?(cache_path) ⇒ Boolean
75 76 77 |
# File 'lib/eaternet/util.rb', line 75 def self.expired?(cache_path) !File.exist?(cache_path) || file_age_in_days(cache_path) > 0.5 end |
.extract_zipfile(path:, dest_dir:) ⇒ Object
Extract a Zip archive.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/eaternet/util.rb', line 59 def self.extract_zipfile(path:, dest_dir:) open(path) do |zip_file| Zip::File.open(zip_file, 'rb') do |zip_data| zip_data.each do |entry| dest_path = File.join(dest_dir, entry.name) entry.extract(dest_path) end end end end |
.file_age_in_days(path) ⇒ Float
71 72 73 |
# File 'lib/eaternet/util.rb', line 71 def self.file_age_in_days(path) (Time.now - File.mtime(path)).to_i / 86_400.0 end |
.generate_cache_path(url) ⇒ Object
79 80 81 82 83 |
# File 'lib/eaternet/util.rb', line 79 def self.generate_cache_path(url) cache_dir = prepare_cache_dir cache_key = Base64.strict_encode64(url) File.join(cache_dir, cache_key) end |
.make_temp_dir ⇒ String
Create a temporary directory which is automatically deleted when the program exits.
23 24 25 26 27 |
# File 'lib/eaternet/util.rb', line 23 def self.make_temp_dir dir = Dir.mktmpdir at_exit { FileUtils.remove_entry dir } dir end |
.prepare_cache_dir ⇒ Object
85 86 87 88 89 |
# File 'lib/eaternet/util.rb', line 85 def self.prepare_cache_dir cache_dir = '/tmp/eaternet' `mkdir -p #{cache_dir}` cache_dir end |