vCardigan is a ruby library for building and parsing vCards that supports both v3.0 and v4.0.
Note: This library is no longer called vCardMate!
gem install vcardigan
vcard = VCardigan.create(:version => '3.0')
vCardigan supports vCard versions 3.0
and 4.0
. Support for 2.1
is not
currently expected. You may pass the version number as an argument to
VCardigan.create
; the default is 4.0
. You may also change the version at
any time:
vcard.version '4.0'
vcard.to_s
vcard = VCardigan.create
vcard.name 'Strummer', 'Joe'
vcard.fullname 'Joe Strummer'
vcard.photo 'http://strummer.com/joe.jpg', :type => 'uri'
vcard.email 'joe@strummer.com', :type => ['work', 'internet'], :preferred => 1
vcard[:item1].url 'http://strummer.com'
vcard[:item1].label 'Other'
puts vcard
The above would output:
BEGIN:VCARD
VERSION:4.0
N:Strummer;Joe;;;
FN:Joe Strummer
PHOTO;TYPE=uri:http://strummer.com/joe.jpg
EMAIL;TYPE=work,internet;PREF=1:joe@strummer.com
item1.URL:http://strummer.com
item1.LABEL:Other
END:VCARD
Using the above output as data
, we could parse it as such:
vcard = VCardigan.parse(data)
vcard.n.first.values # ['strummer', 'joe', '', '', '']
vcard.photo.first.params # { 'type' => 'uri' }
vcard.email.first.params # { 'type' => ['work', 'internet'], 'preferred' => '1' }
vcard[:item1].url.first.value # http://strummer.com