Skip to content

Instantly share code, notes, and snippets.

@pfeiffer
Created September 27, 2012 09:54
Show Gist options
  • Save pfeiffer/3793210 to your computer and use it in GitHub Desktop.
Save pfeiffer/3793210 to your computer and use it in GitHub Desktop.
def self.import(subscribers, list_id)
# `subscribers` should be an array of hashed subscribers with :email as key and optionally :data as well.
# Generate CSV:
content = CSV.generate(:force_quotes => true, :col_sep => ";") do |csv|
# First row is the headers row:
fields = [:email]
# Add additional data keys:
if data_keys = subscribers.first[:data].try(:keys)
fields += data_keys.collect { |key| "Data: #{key}" }
end
# Add the headers to the file:
csv << fields
# Add a row for each subscriber:
subscribers.each do |subscriber|
fields = []
fields << subscriber[:email]
data_keys.each do |key|
fields << subscriber[:data][key]
end
# Add the fields to the file:
csv << fields
end
end
# Done:
filename = "ubivox_import.csv"
content = XMLRPC::Base64.encode(content)
call('ubivox.import_new', filename, content, list_id, 'Test', true, false, true, false)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment