firstruby

Master ruby with us

Archive for December, 2007

vCard Exporting to *.vcf file through RubyOnRails

Posted by firstruby on December 10, 2007

To Export vCard of Contacts for offline access, install vpim gem,

gem install vpim

In Controller, code to export contact to vCard (export to *.vcf file) should be as follows

require 'vpim/vcard'
class VcardController < ApplicationController
def get_contact
contact = Address.find(params[:id])
company = OfficeAddress.find(contact.office_address_id)

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

maker.add_name do |name|
name.given = contact.firstname + ‘ ‘ + contact.lastname
end

maker.add_addr do |addr|
addr.location = ‘home’
addr.street = contact.street
addr.locality = contact.area
addr.region = contact.state
addr.postalcode = contact.pin
addr.country = contact.country
end

maker.add_addr do |addr|
addr.location = ‘work’
addr.street = company.street
addr.locality = company.area
addr.region = company.state
addr.postalcode = company.pin
addr.country = company.country
end

if !contact.ph_resi.empty?
maker.add_tel(contact.ph_resi) do |tel|
tel.location = ‘home’
tel.preferred = true
end
end

if !contact.ph_office.empty?
phone = contact.ph_office
maker.add_tel(phone) do |tel|
tel.location = ‘work’
tel.preferred = true
end
end

if !contact.fax.empty?
maker.add_tel(contact.fax) do |tel|
tel.location = ‘work’
tel.capability = ‘fax’
end
end

if !contact.mobile.empty?
maker.add_tel(contact.mobile) do |tel|
tel.location = ‘home’
tel.capability = ‘mobile’
end
end

maker.add_email(contact.email) do |e|
e.location = ‘work’
end

end

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

Advertisements

Posted in Useful Links | Tagged: | 2 Comments »

iCal ( iCalendar ) Publishing to *.ics file through RubyOnRails

Posted by firstruby on December 10, 2007

To publish iCal through Rails, install iCalendar gem

gem install icalendar

then in Controller, code to export event ( export to *.ics file ) should look like,

require 'icalendar'
class EventController < ApplicationController
def export_events
@event = Event.find(params[:id])
@calendar = Icalendar::Calendar.new
event = Icalendar::Event.new
event.start = @event.dt_time.strftime("%Y%m%dT%H%M%S")
event.end = @event.dt_time.strftime("%Y%m%dT%H%M%S")
event.summary = @event.summary
event.description = @event.description
event.location = @event.location
@calendar.add event
@calendar.publish
headers['Content-Type'] = "text/calendar; charset=UTF-8"
render_without_layout :text => @calendar.to_ical
end
end

Posted in Useful Links | Tagged: , | 3 Comments »