class Holistics::Import

Public Instance Methods

api_client() click to toggle source
# File lib/import.rb, line 51
def api_client
  @api_client ||= ApiClient.new
end
csv() click to toggle source
# File lib/import.rb, line 13
def csv
  api_client.import_csv(options.dup)
end
execute() click to toggle source
# File lib/import.rb, line 25
def execute
  range_mode = options[:custom_range]
  params = {
    import_id: options[:import_id]
  }
  if range_mode
    raise Thor::Error.new('Error, please specify a start range or end range') if !options[:range_start] && !options[:range_end]
    params[:range_start] = options[:range_start]
    params[:range_end] = options[:range_end]
    params[:execution_mode] = 'custom_range'
    if options[:split_mode] == 'daily'
      return daily_splitted_import(params.dup)
    end
  end
  send_import(params)
rescue => e
  STDERR.puts e.message.red
  exit 1
end
list() click to toggle source
# File lib/import.rb, line 46
def list
  api_client.import_list
end

Private Instance Methods

daily_splitted_import(params) click to toggle source
# File lib/import.rb, line 58
def daily_splitted_import(params)
  start_date = Date.parse(params[:range_start]) rescue raise("Invalid range start")
  if params[:range_end]
    end_date = Date.parse(params[:range_end]) rescue raise("Invalid range end")
  else
    end_date = Date.today
  end
  while start_date + 1 < end_date
    params[:range_start] = start_date.strftime('%Y-%m-%d')
    params[:range_end] = (start_date + 1).strftime('%Y-%m-%d')
    puts "Importing range '#{params[:range_start]}' - '#{params[:range_end]}'".green
    send_import(params, tries: 2)
    start_date = start_date + 1
  end
end
send_import(params, tries: 0) click to toggle source
# File lib/import.rb, line 74
def send_import(params, tries: 0)
  total_trials ||= tries
  api_client.send_import(params.dup)
rescue ApiClient::ImportError => e
  sleep 2 ** (total_trials - tries) unless Holistics.test?
  if (tries -= 1) >= 0
    puts 'Retrying...'.orange
    retry
  end
  puts "Exceeded retry count of #{total_trials}...".orange if total_trials > 0
  raise
end