Exporting vcards from rails

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?

i was able to modify the examples to do what i needed. i would be
interested in hearing from others to see if this is an ok way of
generating the vcard when the user wants to download one:

require ‘vpim/vcard’

class VcardController < ApplicationController

def get_card
contact = Contact.find(params[:id])
card = Vpim::Vcard::Maker.make2 do |maker|

  maker.add_name do |name|
    name.given = contact.name
  end

  maker.add_addr do |addr|
    addr.location = 'home'
    addr.street = contact.address
    addr.locality = contact.city
    addr.region = contact.state
    addr.postalcode = contact.zip
  end

  if !contact.phone.empty?
    maker.add_tel(contact.phone) do |tel|
      tel.location = 'home'
      tel.preferred = true
    end
  end

  if !contact.fax.empty?
    maker.add_tel(contact.fax) do |tel|
      tel.location = 'home'
      tel.capability = 'fax'
    end
  end

  maker.add_email(contact.email) do |e|
    e.location = 'home'
  end
end

send_data card.to_s, :filename => "contact_#{contact.id}.vcf"

end
end

Josh K. wrote:

i was able to modify the examples to do what i needed. i would be
interested in hearing from others to see if this is an ok way of
generating the vcard when the user wants to download one:

  maker.add_name do |name|
    name.given = contact.name
  end

I don’t think it’s possible to do this “right”, but you might want to
try something like this, depending on the format of contact.name:

tmp = contact.name.split
case tmp.size
when 1
n.given = tmp[0]
when 2
name.given = tmp[0]
name.family = tmp[1]
when 3
name.given = tmp[0]
name.family = tmp[2]
when 4
name.given = tmp[1]
name.family = tmp[3]
end

Cut to fit, paint to match:-)

–Al Evans

Derek Neighbors wrote:

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

Using this technique gives me a server response with:

“uninitialized constant Mime::VCF”

I found several mime-type related files in the following directory:
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
mime_responds.rb
mime_type.rb
mime_types.rb

Is there a way to add the .vcf mime-type?

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]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs