Fetching Cotacts from Gmail, Yahoo and Hotmail

Hey guys, it’s pretty simple to fetch the contacts from gmail, yahoo or hotmail. Just 5-10 minutes is quite enough to be done. Follow the steps.

Step 1 :-

Install the gem contacts (sudo gem install contacts)

Step 2 :-

Add this line in the environment.rb

require ‘contacts’

Step 3 :-

Some coding part in controller on view

invite_controller.rb

def invite_friends

@user = User.find(params[:id])

//Pass the params of user id, can be the current user id also.for eg, invite/invite_friends/1

//It does not have impact of fetching the contacts from gmail, yahoo or hotmail.
end

def import
@users = User.find(params[:id])
begin
@sites = {“gmail”  => Contacts::Gmail, “yahoo” => Contacts::Yahoo, “hotmail” => Contacts::Hotmail}
@contacts = @sites[params[:from]].new(params[:login], params[:password]).contacts
@users , @no_users = [], []
@contacts.each do |contact|
if u = User.find(:first , :conditions => “email = ‘#{contact[1]}'” )
@users << u
else
@no_users << {:name => contact[0] , :email => contact[1]}
end
end
respond_to do |format|
format.html {render :template => ‘invite/_contact_list’, :layout => false}
format.xml {render : xml => @contacts.to_xml}
end
end
end

Create a folder named ‘invite’ on the views and create the following erb files.

invite_friends.html.erb

<% form_tag :action => ‘import’, :id => @user do %>

<select name=”from” id=”from”>
<option value=””>Select Id</option>
<option value=”gmail”>Gmail</option>
<option value=”yahoo”>Yahoo</option>
<option value=”hotmail”>Hotmail</option>
</select>

<BR />
<p>Please Enter Your Email Address Below : <BR />
<input type=”text” name=”login”></p>
<p>Enter Your Password :<BR />
<input type=”password” name=”password”></p>

<p><h4>Note : we are not going to save your Password anywhere </h4></p>
<p><%= submit_tag ‘Find My Friends’ %>

<% end %>

_contact_list.html.erb

<% for i in @contacts %>
<input type=”checkbox” name=”email[]” id=”email_<%= i %>” value=”<%= i %>” /><%= i %><br>
<% end %>

That’s it. Done. Any queries let me know.

Advertisements