Sha256: 57566f45c734269d45e8d049118da9763fd11bd1040cbf12cc5c868815e8c493

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'zip'
require 'fileutils'
require 'securerandom'

module AppInfo
  # AppInfo Util
  module Util
    FILE_SIZE_UNITS = %w[B KB MB GB TB]

    def self.format_key(key)
      key = key.to_s
      return key unless key.include?('_')

      key.split('_').map(&:capitalize).join('')
    end

    def self.file_size(file, humanable)
      file_size = File.size(file)
      humanable ? size_to_humanable(file_size) : file_size
    end

    def self.size_to_humanable(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

    # Unarchive zip file
    #
    # source: https://github.com/soffes/lagunitas/blob/master/lib/lagunitas/ipa.rb
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
app-info-2.5.4 lib/app_info/util.rb
app-info-2.5.3 lib/app_info/util.rb