Skip to content

Instantly share code, notes, and snippets.

@DeskSupport
Created January 14, 2012 01:25
Show Gist options
  • Save DeskSupport/1609752 to your computer and use it in GitHub Desktop.
Save DeskSupport/1609752 to your computer and use it in GitHub Desktop.
Import Customer CSV
=begin
This file will look for a CSV file named "customers.csv" in the same directory as this file.
The format of the CSV file should be Email Address,Name,Phone Number.
All fields are required and the email address must be formatted correctly.
The email address must be unique on your Assistly system or user creation will fail.
If you do not have data for the name and phone number fields, pass in a placeholder such as UNKNOWN.
There should be no spaces between columns... just commas.
The first row should NOT have column names. It should be the first row of the data to import.
A logfile named "create_customers_output.csv" will be generated in the same directory as this file.
REFERENCES:
http://www.devdaily.com/blog/post/ruby/example-split-csv-rows-data-into-fields-commas-ruby
http://dev.assistly.com/docs/api/customers/create
=end
require "rubygems"
require "oauth"
########################################################################
# MAKE EDITS TO THIS SECTION
@@site = "https://subdomain.assistly.com"
@@import_file = "customers.csv"
@@output_file = "create_customers_output.csv"
@@consumer = OAuth::Consumer.new(
"your public key",
"your public secret",
:site => @@site,
:scheme => :header
)
@@access_token = OAuth::AccessToken.from_hash(
@@consumer,
:oauth_token => "your token",
:oauth_token_secret => "your token secret"
)
# END EDITS
########################################################################
def process_file(filename)
if File.exists?(filename) then
@@file = File.open(filename, "r") #read-only
#customers = Array.new
# loop through each record in the csv file,
# adding each record to the array
@@counter = 0
@@file.each do |line|
fields = line.split(',')
#c = Customer.new
# remove double quotes
#c.name = fields[0].tr_s('"', '').strip
#c.email_address = fields[1].tr_s('"', '').strip
#c.phone_number = fields[2].tr_s('"', '').strip
puts fields.inspect
fields.each do |field|
# remove double quotes
#field.tr_s!('"', '').strip!
#field.gsub!('"', '').strip!
field.strip!
#puts field
end
#puts fields
#create_customer(fields[0], fields[1], fields[2], fields[3], fields[4])
update_customer(fields[0], fields[1], fields[2], fields[3], fields[4])
@@counter += 1
end
@@file.close
else
puts "ERROR: File #{@@import_file} does not exist.\n"
exit
end
end
def create_customer(name, external_id, email_address, phone_number, custom_customertier)
#puts name
#puts external_id
#puts email_address
#puts phone_number
#puts custom_customertier
puts "Creating record for #{email_address}..."
response = @@access_token.post("#{@@site}/api/v1/customers.json",
{
:email => email_address,
:name => name,
:phone => phone_number,
:external_id => external_id,
:custom_customertier => custom_customertier
}
)
#puts response.body.split(",")
handle_response(email_address, response.body)
#puts "-------#{response.body}--------"
end
def update_customer(name, external_id, email_address, phone_number, custom_customertier)
#puts name
#puts external_id
#puts email_address
#puts phone_number
#puts custom_customertier
puts "Updating record for #{email_address}..."
response = @@access_token.post("#{@@site}/api/v1/customers.json?email=" + email_address)
#puts response.body.split(",")
handle_response(email_address, response.body)
#puts "-------#{response.body}--------"
end
def startup
@@success = 0
@@failure = 0
@@unknown = 0
@@start_time = Time.now.to_i
@@log_file = File.open(@@output_file, "a") #Append to file, file will be created if it does not exist
@@log_file.puts "===================================================="
@@log_file.puts "IMPORT STARTED AT #{Time.now}"
end
def handle_response(email_address, response)
#puts response
if response.include? "error" then
response.gsub!('"','')
response.gsub!('{','')
response.gsub!('}','')
#puts response
puts "-----> Creation of #{email_address} failed! (#{response}) See logfile (#{@@output_file}) for details."
@@log_file.puts "#{Time.now} | #{email_address} | #{response}"
@@failure += 1
elsif response.include? '"success":true' then
### STILL WORKING ON THIS SECTION
puts "-----> Creation of #{email_address} successful!"
@@log_file.puts "#{Time.now} | #{email_address} | Success"
@@success += 1
#response = response.split(",")
#puts response
#puts response[0]
else # Unexpected response
puts "-----> Unknown response!"
@@log_file.puts "#{Time.now} | #{email_address} | Unknown response from API"
@@unknown += 1
end
# Write summary of import attempt.
end
def teardown
end_time = Time.now.to_i
duration = (end_time - @@start_time)
throughput = (@@counter.to_f / duration.to_f)
message = "IMPORT ENDED AT #{Time.now}.\n"
message += "TOTAL DURATION: #{duration} seconds.\n"
message += "THROUGHPUT: #{throughput} items per second.\n"
message += "Success: (#{@@success}) | Fail: (#{@@failure}) | Unknown: (#{@@unknown}) | Total: #{@@counter}"
puts message
@@log_file.puts message
#@@log_file.puts "IMPORT ENDED AT #{Time.now}."
#@@log_file.puts "TOTAL DURATION: #{duration} seconds. "
#@@log_file.puts "THROUGHPUT: #{throughput} items per second."
#@@log_file.puts "Success: (#{@@success}) | Fail: (#{@@failure}) | Unknown: (#{@@unknown}) | Total: #{@@counter}"
@@log_file.puts "====================================================\n\n"
@@log_file.close
end
startup
process_file(@@import_file)
teardown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment