Skip to content

Instantly share code, notes, and snippets.

@jonathanhculver
Created January 15, 2014 02:54
Show Gist options
  • Save jonathanhculver/8429962 to your computer and use it in GitHub Desktop.
Save jonathanhculver/8429962 to your computer and use it in GitHub Desktop.
iterating through a vcf file to add a field
load 'vpim-13.11.11/lib/vpim/vcard.rb'
#read file as string
address_book = File.open('contacts.vcf')
contents = address_book.read
#load contacts
original = Vpim::Vcard.decode(contents)
#create new file
new_book = File.new("contacts_modified.vcf", "w")
new_book.write("")
new_book = File.new("contacts_modified.vcf", "a")
#loop through all contacts
original.each do |contact|
modified = Vpim::Vcard::Maker.make2 do |maker|
maker.copy(contact) do |field|
field
end
end
#adding IM address to vcard
Vpim::Vcard::Maker.make2(modified) do |maker|
first_last = contact.email[0, contact.email.index('@')]
maker.add_x_jabber(first_last+"@spark.nextjump.com")
puts 'Adding '+ first_last
end
#append contact to file
new_book.write(modified)
end
new_book.close
puts "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment