On Aug 10, 2006, at 10:50 AM, Josh K. wrote:
i’m doing a site for someone that has a form set up to collect contact
information from the user. i installed the vpim gem and was hoping to
give my client a way to download all of the contact’s information as a
vcard. using the examples, i was able to get it working in ruby,
but i’m
not quite sure how to generate and download a vcard on the fly.
has anyone done anything like that?
We are doing this using the SimplyRESTful stuff…
In the controller for example we have:
def show
respond_to do |format|
format.html
format.xml { render :xml => @contact.to_xml }
format.vcf do
send_data @contact.to_vcard, :filename => “#{@contact.id}.vcf”,
:type => ‘text/directory’
end
end
end
This allows us to get a contact back as html, xml or vcard.
Then in the contact model you can add something like:
def to_vcard
card = Vpim::Vcard::Maker.make2 do |maker|
maker.add_name do |name|
name.prefix = prefix unless self.prefix.blank?
name.given = first_name
name.family = last_name
name.suffix = suffix unless self.suffix.blank?
end
if preferred_location
maker.add_addr do |addr|
addr.preferred = true
addr.location = 'work'
addr.street = preferred_location.address_line_1 unless
preferred_location.address_line_1.blank?
addr.locality = preferred_location.city unless
preferred_location.city.blank?
addr.postalcode = preferred_location.postal_code unless
preferred_location.postal_code.blank?
addr.country = preferred_location.country unless
preferred_location.country.blank?
end
maker.add_tel(preferred_location.voice_number) do |tel|
tel.location = 'work'
tel.preferred = true
end unless preferred_location.voice_number.blank?
maker.add_tel(preferred_location.fax_number) do |tel|
tel.location = 'work'
tel.capability = 'fax'
end unless preferred_location.fax_number.blank?
maker.add_email(preferred_location.email) do |e|
e.location = 'work'
e.preferred = 'yes'
end unless preferred_location.email.blank?
end
end
card.to_s
end
–
Derek Neighbors
Integrum Technologies
Work: 602.792.1270 x1101
Mobil: 480.330.7892
Email: [email protected]