Importing contacts from your email using Rails ‘contacts’ gem

Using ‘contacts’ gem you can import all your contact emails.

Step 1:

In Your Gemfile add,

## For Importing Email Contacts
gem "contacts", :git => "git://github.com/abhilashak/contacts.git"
gem "gdata", :git => "git://github.com/abhilashak/gdata-1.git"

For importing from gmail, we need the gem ‘gdata’.

Do bundle install

you are done.

Step 2:

In your views/import_profile_contact/new.html.erb file,

<%=  text_field_tag "profile_contact[email]", "" %>

<%=  password_field_tag "profile_contact[password]", "" %>

Put one more hidden field tag to know that from which email the user need to import

<%= hidden_field_tag :contact_from, @contact_from %>

the values of @contact_from may be gmail, yahoo mail, hot mail. According to which option user clicks pass the corresponding string.

Step 3:

In your controller get the email and password params

@contact_from = params[:contact_from]

email = params[:profile_contact][:email]
password = params[:profile_contact][:password]

if @contact_from && email && password
  if @contact_from == "gmail"
    @imported_contacts = Contacts.new(:gmail, email, password).contacts
  elsif @contact_from == "yahoo_mail"
    @imported_contacts = Contacts.new(:yahoo, email, password).contacts
  elsif @contact_from == "hotmail"
    @imported_contacts = Contacts.new(:hotmail, email, password).contacts
  end
end

You will get all contacts in an array….

Author: Abhilash

Hey! My name is Abhilash - A Ruby developer for years, specialised on web programming, working as a freelancer. Mainly working on Ruby On Rails platform since 2010. This blog is about Ruby, Ruby On Rails and other subjects that I work. You can contact me here: abhilash.amballur@gmail.com Abhilash AK

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s