Create Company Card w/ Vpim Gem
I'm using Vpim to generate .vcf files that users can then import into their address books. The problem that I'm having is that the information that their downloading is for a Company and not a person so I need to mark the card as being such. Is there a way to do this using Vpim or is there another gem that I could use to accomplish this?
def to_vcf
card = Vpim::Vcard::Maker.make2 do |maker|
...
end
end
Source of a Business Card from Address Book
BEGIN:VCARD
VERSION:3.0
N:;;;;
FN:The Flow Skatepark
ORG:The Flow Skatepark;
item1.TEL;type=pref:(614) 864-1610
item1.X-ABLabel:Work #
item2.ADR;type=WORK;type=pref:;;4252 Groves Rd;Columbus;OH;43232;USA
item2.X-ABADR:us
BDAY;value=date:2001-07-06
X-ABShowAs:COMPANY
X-ABUID:5F7349CB-369F-4EAC-AB65-49ED902BEF9F\:ABPerson
END:VCARD
Source of a Non-Business Card from Address Book
BEGIN:VCARD
VERSION:3.0
N:;The Flow Skatepark;;;
FN:The Flow Skatepark
item1.TEL;type=pref:(614) 864-1610
item1.X-ABLabel:Work #
item2.ADR;type=WORK;type=pref:;;4252 Groves Rd;Columbus;OH;43232;USA
item2.X-ABADR:us
BDAY;value=date:2001-07-06
X-ABUID:5F7349CB-369F开发者_开发知识库-4EAC-AB65-49ED902BEF9F\:ABPerson
END:VCARD
Obviously there are two main differences in these code samples:
- ORG:The Flow Skatepark;
- X-ABShowAs:COMPANY
I don't know how this translates into Vpim however.
Quick and dirty implementation, I hope that I understood you correctly:
require 'vpim/vcard'
vcards = <<VCARD
BEGIN:VCARD
VERSION:3.0
N:;;;;
FN:The Flow Skatepark
ORG:The Flow Skatepark;
item1.TEL;type=pref:(614) 864-1610
item1.X-ABLabel:Work #
item2.ADR;type=WORK;type=pref:;;4252 Groves Rd;Columbus;OH;43232;USA
item2.X-ABADR:us
BDAY;value=date:2001-07-06
X-ABShowAs:COMPANY
X-ABUID:5F7349CB-369F-4EAC-AB65-49ED902BEF9F\:ABPerson
END:VCARD
BEGIN:VCARD
VERSION:3.0
N:;The Flow Skatepark;;;
FN:The Flow Skatepark
item1.TEL;type=pref:(614) 864-1610
item1.X-ABLabel:Work #
item2.ADR;type=WORK;type=pref:;;4252 Groves Rd;Columbus;OH;43232;USA
item2.X-ABADR:us
BDAY;value=date:2001-07-06
X-ABUID:5F7349CB-369F-4EAC-AB65-49ED902BEF9F\:ABPerson
END:VCARD
VCARD
contacts = []
Vpim::Vcard.decode(vcards).each do |vcard|
contacts << {
first_name: vcard.name ? vcard.name.given : '',
last_name: vcard.name ? vcard.name.family : '',
organisation_name: vcard.org ? vcard.org.first : '',
}
end
def to_vcard(card)
Vpim::Vcard::Maker.make2 do |maker|
maker.add_name do |name|
name.given = card[:first_name] unless card[:first_name].empty? || card[:first_name].nil?
name.family = card[:last_name] unless card[:last_name].empty? || card[:last_name].nil?
end
maker.org = card[:organisation_name] unless card[:organisation_name].empty? || card[:organisation_name].nil?
end
end
contacts.each_with_index do |contact, idx|
File.open("contact#{idx}.vcf", 'w') {|f| f.write(to_vcard(contact)) }
end
Looks like the maker object has a org= method, which you can use to set ORG. As for X-ABShowAs, maker has an add_field method. You can probably create your own field (http://vpim.rubyforge.org/classes/Vpim/DirectoryInfo/Field.html) for that.
精彩评论