Creating vcards with ruby on rails

vCards provide a great way to pas your contact details to your clients, its a widely supported format which most programs for managing contacts understand be it outlook, outlook express, or macs address book. For a recent project we had a database of employees within our company and we wanted to provide these employees contact details through our website. Using ruby on rails and the vpim gem this was easy to setup.

First install the vPIm gem using:

sudo gem install vpim

Once installed create a controller called vcard. Within this controller we need to require the gem so our controller will look like this:

require 'vpim/vcard'

class VcardController < ApplicationController
      def view

      end
end

The view action will be the page we use to automatically generate and serve the vcard. First pull the user record from your database, obviously the way you store your users details will probably be different.

user = Employee.find_by_id(params[:id])

Next we need to create the vcard object using the vpim gem

card = Vpim::Vcard::Maker.make2 do |maker|

end

then using maker theres a few functions we can use to populate the vcard. first we can add a name using the add_name block.

maker.add_name do |name|
  	name.prefix = ''
  	name.given = @user.fname
  	name.family = @user.lname
end  

We can add an address using add_addr:

maker.add_addr do |addr|
 	addr.preferred = true
 	addr.location = 'work'
 	addr.street = '123 Some Street'
 	addr.locality = 'Anytown'
        addr.country = 'United Kingdom'
end

we can add a telephone number using add_tel:

maker.add_tel(@user.telephone)

and an email address using:

maker.add_email(@user.email) { |e| e.location = 'work' }

Once we have entered all the details into our vcard object we can then send the output to the browser using:

send_data card.to_s, :filename => "contact.vcf"

So our full action would be:

def vcard
	@user = Employee.find_by_id(params[:id])

	card = Vpim::Vcard::Maker.make2 do |maker|

		maker.add_name do |name|
    			name.prefix = ''
    			name.given = @user.fname
    			name.family = @user.lname
		end

		maker.add_addr do |addr|
    			addr.preferred = true
    			addr.location = 'work'
    			addr.street = '243 Felixstowe Road'
    			addr.locality = 'Ipswich'
    			addr.country = 'United Kingdom'
		end

		maker.add_tel(@user.telephone)

		maker.add_email(@user.email) { |e| e.location = 'work' }

	end

	send_data card.to_s, :filename => "contact.vcf"
end

so by calling the url /vcard/view/:id you can pull back a user record and create a vcard for that user.

For a list of all the functions vPim provides you can goto http://vpim.rubyforge.org/



Thursday, June 10th, 2010
Graham
Tags: , ,

One Response to Creating vcards with ruby on rails

  1. sunny says:

    Hi,
    It’s good post but i need to import vcard in database how i can do that? there is decode method but i need to parse and just extract email address how it is possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>