Skip to content

Instantly share code, notes, and snippets.

@scott

scott/gist:143871

Created Jul 9, 2009
Embed
What would you like to do?
namespace :highrise do
desc 'import contacs from csv'
task :import_contacts => :environment do
require 'csv'
puts 'Importing CSV file. This may take a while if you have a lot of contacts.'
CSV.foreach("#{RAILS_ROOT}/db/highrise/contacts.csv") do |row|
if row[0] == "Person"
Lead.delete_observers
Lead.create(
:user_id => 1,
:access => "Public",
:first_name => row[2],
:last_name => row[3],
:company => row[4],
:title => row[5],
:email => row[28],
:blog => row[31],
:address => row[6].to_s + "\n" + row[7].to_s + ", " + row[8].to_s + " " + row[9].to_s + "\n" + row[10].to_s)
end
end
end
desc 'import contact notes from highrise yml format'
task :import_records => :environment do
require 'yaml'
current_user = User.find(1)
puts "Importing contact notes via YAML files"
Dir["#{RAILS_ROOT}/db/highrise/*.txt"].each do |file|
comments = YAML.load_file(file)
current_user = User.find(1)
if comments[1]
contact = comments[1]["Contact"].inject({}) do |hash,value|
hash[value[0]] = value[1]
hash
end
lead = Lead.find_by_full_name(comments[0]['Name'])
if lead.blank?
lead = Lead.find_by_email(contact["Email_addresses"])
end
puts "inserting records for #{comments[0]['Name']}"
# loop through notes
if comments[2]
emails = comments[2].map do |note|
note.invert.keys.flatten.inject({}) do |a,v|
a.merge v
end
end
end
if lead
comments.each_with_index do |email,i|
#puts emails[i]["Body"]
unless emails[i].nil?
Lead.delete_observers
sql = ActiveRecord::Base.connection()
sql.execute "INSERT INTO `comments` (`commentable_type`, `updated_at`, `title`, `private`, `commentable_id`, `user_id`, `comment`, `created_at`)
VALUES('Lead', '#{emails[i]["Written"].to_datetime}', '', NULL, #{lead.id}, 1, '#{(emails[i]['Body'].to_s).gsub(/[']/, '\\\\\'')}', '#{emails[i]["Written"].to_datetime}')"
end
end
lead.update_attribute(:status, 'contacted')
lead.update_attribute(:created_at, lead.comments.last.created_at)
lead.update_attribute(:updated_at, lead.comments.last.created_at)
end
end
end
end
desc 'import CSV and yaml files from Highrise'
task :import do
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment