Skip to content

Instantly share code, notes, and snippets.

@chrismear
Last active December 12, 2015 06:38
Show Gist options
  • Save chrismear/4730250 to your computer and use it in GitHub Desktop.
Save chrismear/4730250 to your computer and use it in GitHub Desktop.
# Copyright (c) 2009, 2013 Chris Mear <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
contacts = []
catch :invalid_contacts_format do
# Detect the line ending
c = ""
until c == "\r"[0] || c == "\n"[0] || c.nil?
c = data.getc
end
line_ending = nil
if c
c = c.chr
n = data.getc
if n == "\r"[0] || n == "\n"[0]
n = n.chr
line_ending = c + n
else
line_ending = c
end
else
line_ending = c
end
throw :invalid_contacts_format unless line_ending
data.rewind
# Guess whether this is a comma-separated or tab-delimited file
header_line = data.gets(line_ending)
tab_count = header_line.count("\t")
comma_count = header_line.count(',')
if tab_count > comma_count
Rails.logger.info("Parsing tab-delimited values with #{line_ending.inspect} line endings.")
field_separator = "\t"
else
Rails.logger.info("Parsing comma-separated values with #{line_ending.inspect} line endings.")
field_separator = ','
end
begin
header_row = CSV::parse_line(header_line, field_separator, line_ending)
rescue CSV::IllegalFormatError
throw :invalid_contacts_format
end
first_name_index = header_row.index('First Name')
last_name_index = header_row.index('Last Name')
email_indices = []
[
# Outlook CSV headers
'E-mail Address',
'E-mail 2 Address',
'E-mail 3 Address',
# Entourage TSV headers
'Email Address 1',
'Email Address 2',
'Email Address 3',
'Email Address 4',
'Email Address 5',
'Email Address 6',
'Email Address 7',
'Email Address 8',
'Email Address 9',
'Email Address 10',
'Email Address 11',
'Email Address 12',
'Email Address 13'
].each do |column_name|
index = header_row.index(column_name)
email_indices.push(index) if index
end
throw :invalid_contacts_format unless (first_name_index && last_name_index && !email_indices.empty?)
while line = data.gets(line_ending)
begin
row = CSV.parse_line(line, field_separator, line_ending)
name = []
name.push(row[first_name_index].to_s)
name.push(row[last_name_index].to_s)
name = name.join(' ')
email = nil
email_indices.each do |email_index|
unless email
email = row[email_index]
end
end
contacts.push({:email => email, :name => name}) unless email.blank?
rescue CSV::IllegalFormatError
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment