Skip to content

Instantly share code, notes, and snippets.

@mrded
Last active August 29, 2015 14:14
Show Gist options
  • Save mrded/17317466b3ebf71eb07a to your computer and use it in GitHub Desktop.
Save mrded/17317466b3ebf71eb07a to your computer and use it in GitHub Desktop.
Ruby: QuickTapSurvey to MailChimp converter.
#!/usr/bin/env ruby
# https://gist.github.com/mrded/17317466b3ebf71eb07a
require 'csv'
class MailChimpConverter
attr_accessor :columns
COLUMNS_HEADER = {
:email => 'Email Address',
:industry => 'Career interests',
:graduation_year => 'Graduation year',
:source => 'Source',
}
def initialize(options = {})
@files = options[:files]
@columns = []
options[:files].each { |file| fill(file) }
end
def save
CSV.open("output.csv", "wb") do |csv|
# Add header
csv << COLUMNS_HEADER.values
@columns.each { |column| csv << column.values }
end
end
private
def fill(file)
CSV.read(file).each do |column|
email = column[6]
@columns.push(
:email => email,
:industry => fix_industry(column[4]),
:graduation_year => column[5],
:source => File.basename(file, ".csv"),
) if email_is_valid?(email)
end
end
def email_is_valid?(email)
!/\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/.match(email).nil?
end
def fix_industry(value)
# Escaping commas.
industries = []
CSV.parse_line(value).each do |industry|
industries += [industry.gsub(/\,/, '\,')] # Replace ',' to '\,'
end
return industries.join(', ')
end
end
MailChimpConverter.new(files: ARGV).save
@mrded
Copy link
Author

mrded commented Mar 19, 2015

Hi Tishan, unfortunately QuickTapSurvey doesn't support MailChimp Groups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment