Method: ThreeSixty::Account#download_campaigns

Defined in:
lib/three-sixty/account.rb

#download_campaigns(campaign_ids) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/three-sixty/account.rb', line 24

def download_campaigns(campaign_ids)
  file_id = get_all_objects(campaign_ids)['fileId']

  retry_number = 0
  file_state = get_file_state(file_id)
  while report_generating?(file_state["isGenerated"])
    begin
      @logger.debug "Waiting for report #{file_id} on retry number #{retry_number}"
      sleep report_generating_backoff.call(retry_number)
      retry_number += 1
      file_state = get_file_state(file_id)
    rescue TypeError => e
      e.message = "Waiting too long to generate the report #{file_id}" # Re-raise with a better error message
      raise e
    end
  end

  @logger.info "Found file #{file_state["filePath"]} with size #{"%.2f" % (file_state["fileSize"] / 2**20)} MB"
  download_filestream(file_state["filePath"]) { |chunk| yield chunk }
  @logger.info "Finished downloading file #{file_state["filePath"]}"
end