Skip to content

Instantly share code, notes, and snippets.

@AustinHay
Created May 11, 2017 00:40
Show Gist options
  • Save AustinHay/7f0711959b40613689ac640d46ae6872 to your computer and use it in GitHub Desktop.
Save AustinHay/7f0711959b40613689ac640d46ae6872 to your computer and use it in GitHub Desktop.
Export API Download Script
#!/usr/bin/ruby
# HBE Ventures, LLC 2017
# Austin Hay
# austin@hbe.io
#########################################
# Ruby Dependencies
require 'net/http'
require 'uri'
require 'open-uri'
require 'JSON'
require 'date'
########################################
########################################
############### SCRIPT METHODS #########
########################################
########################################
# Create Export Dates Method
def createExportDates(first_export_date, last_export_date)
$export_dates = [first_export_date]
for day in 1..(Date.parse(last_export_date) - Date.parse(first_export_date)).to_i
$export_dates << (Date.parse(first_export_date)+day).strftime("%Y-%m-%d")
end
p $export_dates
end
########################################
# Download Method
def downloadUrl(uri, file_name)
download = open('uri')
IO.copy_stream(download, '~/file_name.csv.gz')
end
########################################
# Single Date Export Method
def getSingleDayExportData(uri, export_date)
$export_data = {}
res = JSON.parse(Net::HTTP.get(URI(uri + export_date)))
$export_data[export_date] = [res["links_export_url"]]
$export_data[export_date] << res["events_export_url"]
p "Export Data:"
p $export_data
end
########################################
# Multi-Day Export Method
def getMultiDayExportData (uri, export_dates)
$export_data = {}
export_dates.each do |date|
p uri + date
p res = JSON.parse(Net::HTTP.get(URI(uri + date)))
$export_data[date] = [res["links_export_url"]]
$export_data[date] << res["events_export_url"]
end
p "Export Data:"
p $export_data
end
#######################################
# User Input Block - Production only
# puts 'Enter Branch Live Key.'
# branch_key = gets.chomp.to_s
# puts 'Enter Branch Secret Key.'
# branch_secret = gets.chomp.to_s
########################################
# Test Values - Testing only
branch_key = ''
branch_secret = ''
export_date = '2017-04-30'
export_dates = ["2017-05-01", "2017-05-02", "2017-05-03"]
# Setup Request URI Data
uri = 'https://api.branch.io/v2/export/' + branch_key + '?' + 'branch_secret=' + branch_secret + '&' + 'export_date='
########################################
# User Execution
puts 'What would you like to do?'
puts 'Options: Single Day Export (S), Multi-Day Export (M), Download (D)'
execution_response = gets.chomp.to_s
if execution_response == 'S'
puts 'Enter single export date as YYYY-MM-DD.'
export_date = gets.chomp.to_s
getSingleDayExportData(uri, export_date)
elsif execution_response =='M'
puts 'Enter first export date in the range as YYYY-MM-DD.'
first_export_date = gets.chomp.to_s
puts 'Enter last export date in the range as YYYY-MM-DD.'
last_export_date = gets.chomp.to_s
createExportDates(first_export_date, last_export_date)
getMultiDayExportData(uri, $export_dates)
else
puts "No execute option requested. Ending script."
return
end
########################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment