Class: AdLocalize::CsvFileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ad_localize/csv_file_manager.rb

Constant Summary collapse

CSV_CONTENT_TYPES =
%w(text/csv text/plain)

Class Method Summary collapse

Class Method Details

.csv?(file) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/ad_localize/csv_file_manager.rb', line 6

def csv?(file)
  !file.nil? && CSV_CONTENT_TYPES.include?(`file --brief --mime-type '#{file}'`.strip)
end

.delete_drive_file(file) ⇒ Object



36
37
38
# File 'lib/ad_localize/csv_file_manager.rb', line 36

def delete_drive_file(file)
  Pathname.new(file).delete unless file.nil?
end

.download_from_drive(key, sheet) ⇒ Object

Returns the downloaded file name (it is located in the current directory)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ad_localize/csv_file_manager.rb', line 11

def download_from_drive(key, sheet)
  LOGGER.log(:info, :black, "Downloading file from google drive...")
  download_string_path = "./#{key}.csv"
  begin
    File.open(download_string_path, "wb") do |saved_file|
      # the following "open" is provided by open-uri
      open(drive_download_url(key, sheet), "rb") do |read_file|
        saved_file.write(read_file.read)
      end
      File.basename saved_file
    end
  rescue => e
    delete_drive_file(download_string_path)
    LOGGER.log(:error, :red, e.message)
    exit
  end
end

.select_csvs(files) ⇒ Object



29
30
31
32
33
34
# File 'lib/ad_localize/csv_file_manager.rb', line 29

def select_csvs(files)
  files.select do |file|
    LOGGER.log(:error, :red, "#{file} is not a csv. It will be ignored") unless self.csv?(file)
    self.csv?(file)
  end
end