Module: AppInfo::Util
- Defined in:
- lib/app_info/util.rb
Overview
AppInfo Util
Class Method Summary collapse
- .file_size(file, human_size) ⇒ Object
- .format_key(key) ⇒ Object
- .size_to_human_size(number) ⇒ Object
- .tempdir(file, prefix:) ⇒ Object
-
.unarchive(file, path: nil) ⇒ Object
Unarchive zip file.
Class Method Details
.file_size(file, human_size) ⇒ Object
50 51 52 53 |
# File 'lib/app_info/util.rb', line 50 def self.file_size(file, human_size) file_size = File.size(file) human_size ? size_to_human_size(file_size) : file_size end |
.format_key(key) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/app_info/util.rb', line 43 def self.format_key(key) key = key.to_s return key unless key.include?('_') key.split('_').map(&:capitalize).join end |
.size_to_human_size(number) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/app_info/util.rb', line 55 def self.size_to_human_size(number) if number.to_i < 1024 exponent = 0 else max_exp = FILE_SIZE_UNITS.size - 1 exponent = (Math.log(number) / Math.log(1024)).to_i exponent = max_exp if exponent > max_exp number = format('%<number>.2f', number: (number / (1024**exponent.to_f))) end "#{number} #{FILE_SIZE_UNITS[exponent]}" end |
.tempdir(file, prefix:) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/app_info/util.rb', line 89 def self.tempdir(file, prefix:) dest_path ||= File.join(File.dirname(file), prefix) dest_file = File.join(dest_path, File.basename(file)) Dir.mkdir(dest_path, 0_700) unless Dir.exist?(dest_path) dest_file end |
.unarchive(file, path: nil) ⇒ Object
Unarchive zip file
source: github.com/soffes/lagunitas/blob/master/lib/lagunitas/ipa.rb
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/app_info/util.rb', line 71 def self.unarchive(file, path: nil) path = path ? "#{path}-" : '' root_path = "#{Dir.mktmpdir}/AppInfo-#{path}#{SecureRandom.hex}" Zip::File.open(file) do |zip_file| if block_given? yield root_path, zip_file else zip_file.each do |f| f_path = File.join(root_path, f.name) FileUtils.mkdir_p(File.dirname(f_path)) zip_file.extract(f, f_path) unless File.exist?(f_path) end end end root_path end |