class DaFunk::FileParameter

Constants

FILEPATH

Attributes

crc[RW]
file[R]
name[R]
original[R]
remote[R]

Public Class Methods

delete(collection) click to toggle source
# File lib/da_funk/file_parameter.rb, line 7
def self.delete(collection)
  collection.each do |file_|
    begin
      file_.delete
    rescue RuntimeError
    end
  end
end
new(name, crc) click to toggle source
# File lib/da_funk/file_parameter.rb, line 16
def initialize(name, crc)
  @crc      = crc
  @original = name
  company   = check_company(name)
  @remote   = @original.sub("#{company}_", "")
  @name     = @original.sub("#{company}_", "").split(".").first
  @file     = "#{FILEPATH}/#{@remote}"
end

Public Instance Methods

crc_local() click to toggle source
# File lib/da_funk/file_parameter.rb, line 25
def crc_local
  self.exists? ? @crc_local : nil
end
delete() click to toggle source
# File lib/da_funk/file_parameter.rb, line 60
def delete
  File.delete(self.file) if exists?
end
download(force = false) click to toggle source
# File lib/da_funk/file_parameter.rb, line 43
def download(force = false)
  if force || self.outdated?
    ret = DaFunk::Transaction::Download.request_file(remote, file, self.crc_local)
    if ret == DaFunk::Transaction::Download::SUCCESS
      unless ((@crc_local = calculate_crc) == @crc)
        ret = DaFunk::Transaction::Download::COMMUNICATION_ERROR
      end
    end
  else
    ret = DaFunk::Transaction::Download::FILE_NOT_CHANGE
  end
  ret
rescue => e
  ContextLog.exception(e, e.backtrace, "Error downloading #{self.name}")
  DaFunk::Transaction::Download::IO_ERROR
end
exists?() click to toggle source
# File lib/da_funk/file_parameter.rb, line 33
def exists?
  File.exists? @file
end
outdated?(force = false) click to toggle source
# File lib/da_funk/file_parameter.rb, line 64
def outdated?(force = false)
  return true unless self.exists?
  if !self.crc_local || force
    @crc_local = calculate_crc
  end
  self.crc_local != @crc
rescue
  true
end
unzip() click to toggle source
# File lib/da_funk/file_parameter.rb, line 37
def unzip
  if zip? && exists?
    Zip.uncompress(file, FILEPATH, false, false)
  end
end
zip?() click to toggle source
# File lib/da_funk/file_parameter.rb, line 29
def zip?
  @original.to_s[-4..-1] == ".zip"
end

Private Instance Methods

calculate_crc() click to toggle source
# File lib/da_funk/file_parameter.rb, line 75
def calculate_crc
  if exists?
    Device::Crypto.file_crc16_hex(file)
  end
end
check_company(name) click to toggle source
# File lib/da_funk/file_parameter.rb, line 81
def check_company(name)
  name.split("_", 2)[0]
end
remove_company(name) click to toggle source
# File lib/da_funk/file_parameter.rb, line 85
def remove_company(name)
  name.split("_")[1..-1].join("_")
end