Skip to content

Instantly share code, notes, and snippets.

@franciscoj
Created July 24, 2013 08:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save franciscoj/6069028 to your computer and use it in GitHub Desktop.
Save franciscoj/6069028 to your computer and use it in GitHub Desktop.
Small ruby script to transform a CSV file into a VCF file.
# Requires fastercsv and vpim gems to be installed
require 'rubygems'
require 'fastercsv'
require 'vpim/vcard'
card = Vpim::DirectoryInfo.create([Vpim::DirectoryInfo::Field.create('VERSION', '2.1')], 'VCARD')
FasterCSV.foreach('contactos.csv') do |row|
first_name = row[1]
last_name = row[3]
mobile = row[13]
general = row[14]
email = row[15]
Vpim::Vcard::Maker.make2(card) do |maker|
maker.name do |n|
n.given = first_name unless first_name.nil?
n.family = last_name unless last_name.nil?
end
maker.add_tel(mobile) { |t| t.location = 'mobile'; t.preferred = true } unless mobile.nil?
maker.add_tel(general) { |t| t.location = 'mobile'} unless general.nil?
maker.add_email email unless email.nil?
end
end
puts card
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment