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



43
44
45
# File 'lib/ad_localize/csv_file_manager.rb', line 43

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

.download_from_drive(key, sheet, use_service_account = false) ⇒ 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
28
29
30
31
32
33
34
# File 'lib/ad_localize/csv_file_manager.rb', line 11

def download_from_drive(key, sheet, =false)
  LOGGER.log(:info, :green, "Downloading file from google drive...")
  download_string_path = "./#{key}.csv"
  begin
    File.open(download_string_path, "wb") do |saved_file|
      download_url = drive_download_url(key, sheet)
      headers = {}
      if 
        LOGGER.log(:debug, :green, "Using a service account...")
        token = ()
        headers["Authorization"] = "#{token["token_type"]} #{token["access_token"]}"
      end
      # the following "open" is provided by open-uri
      open(download_url, "rb", headers) 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



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

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