contacts – Ruby gem – Import contacts from Gmail, Yahoo, hotmail

Import contacts from Gmail, Yahoo, Hotmail using contacts gem

Installation:

1. gem install contacts

2. OPTIONAL — Not required –> Install acts_as_authenticated plugin
–> https://raveendran.wordpress.com/2007/11/02/acts_as_authenticated-plungin-in-ror/

3. config/environment.rb file –> Add first line –> require ‘contacts’

4. controller:

controller.rb
def import_contacts

end

def import
@users = User.find(params[:id]) #or use — @users = User.find(1)
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 => ‘CONTROLLER/_list, :layout => false}
format.xml {render 😡 ml => @contacts.to_xml}
end
end

5. view file :

import_contacts.html.erb

<h2>Welcome to Raveendran’s Blog(<a href=”https://raveendran.wordpress.com”>https://raveendran.wordpress.com</a&gt;) Contact import Page</h2><br />
<b> Please select the domain here: </b><br />
<% 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><b>Please Enter Your Email Address Below : (Ex. test1@gmail.com)</b><BR />
<input type=”text” name=”login”></p>
<p><b>Enter Your Password :</b><br />
<input type=”password” name=”password”></p>
<p><%= submit_tag ‘Fetch Friends’ %>
<% end %>

6. Result page:

_list.html.erb

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

7. http://localhost:3000/CONTROLLER/import_contacts

8. Enjoy with your old contacts

Note: currently import hotmail contact gives some protocol error. Once the issue fixed in contacts gem I will update here. comments are always welcome