Skip to content

Instantly share code, notes, and snippets.

@vinhnglx
Forked from maletor/contact_importer.rb
Last active August 29, 2015 14:07
Show Gist options
  • Save vinhnglx/26d72f8c49f3955c3d1c to your computer and use it in GitHub Desktop.
Save vinhnglx/26d72f8c49f3955c3d1c to your computer and use it in GitHub Desktop.
require 'oauth'
require 'oauth2'
class ContactImporter
def initialize(importer)
@importer = importer
end
def authorize_url
case @importer
when 'gmail' then get_gmail_authorize_url
when 'hotmail' then get_hotmail_authorize_url
when 'yahoo' then get_yahoo_authorize_url
end
end
def contacts(code, oauth_token = nil, oauth_secret_token = nil)
case @importer
when 'gmail' then get_gmail_contacts(code)
when 'hotmail' then get_hotmail_contacts(code)
when 'yahoo' then get_yahoo_contacts(code, oauth_token, oauth_secret_token)
end
end
def oauth_token_secret
@request_token = yahoo_consumer.get_request_token(oauth_callback: "https://#{AppConstants.web_host}/contacts/yahoo/callback")
@request_token.secret
end
private
def get_gmail_authorize_url
gmail_client.auth_code.authorize_url(redirect_uri: "https://#{AppConstants.web_host}/contacts/gmail/callback", scope: 'https://www.googleapis.com/auth/contacts.readonly')
end
def get_hotmail_authorize_url
hotmail_client.auth_code.authorize_url(redirect_uri: "https://#{AppConstants.web_host}/contacts/hotmail/callback", scope: 'wl.basic, wl.contacts_emails')
end
def get_yahoo_authorize_url
@request_token ||= yahoo_consumer.get_request_token(oauth_callback: "https://#{AppConstants.web_host}/contacts/yahoo/callback")
@request_token.authorize_url
end
def get_gmail_contacts(code)
token = gmail_client.auth_code.get_token(code, redirect_uri: "https://#{AppConstants.web_host}/contacts/gmail/callback")
response = token.get("https://www.google.com/m8/feeds/contacts/default/thin", headers: { 'GData-Version' => '3.0' }, params: { alt: 'json', "max-results" => 1_000 })
parse_gmail_contacts(response)
end
def get_hotmail_contacts(code)
token = hotmail_client.auth_code.get_token(code, redirect_uri: "https://#{AppConstants.web_host}/contacts/hotmail/callback")
response = token.get("https://apis.live.net/v5.0/me/contacts")
parse_hotmail_contacts(response)
end
def get_yahoo_contacts(oauth_verifier, oauth_token, oauth_token_secret)
request_token = OAuth::RequestToken.new(yahoo_consumer, oauth_token, oauth_token_secret)
access_token = request_token.get_access_token(oauth_verifier: oauth_verifier)
response = access_token.get('https://social.yahooapis.com/v1/user/me/contacts?format=json')
parse_yahoo_contacts(response)
end
def parse_gmail_contacts(response)
response = JSON.parse(response.body)
return [] unless response['feed'] && response['feed']['entry']
contacts = []
response['feed']['entry'].each do |entry|
contact = {}
if gd_name = entry['gd$name']
contact[:first_name] = gd_name['gd$givenName']['$t'] if gd_name['gd$givenName']
contact[:last_name] = gd_name['gd$familyName']['$t'] if gd_name['gd$familyName']
end
contact[:email] = entry['gd$email'][0]['address'] if entry['gd$email']
contacts << contact
end
contacts
end
def parse_hotmail_contacts(response)
response = JSON.parse(response.body)
contacts = []
response['data'].each do |entry|
contact = {}
contact[:first_name] = entry['first_name']
contact[:last_name] = entry['last_name']
contact[:email] = entry['emails']['preferred'] if entry['emails']
contacts << contact
end
contacts
end
def parse_yahoo_contacts(response)
response = JSON.parse(response.body)
return [] unless response['contacts']['contact']
contacts = []
response['data'].each do |entry|
contact = {}
entry['fields'].each do |field|
case field['type']
when 'name'
contact[:first_name] = field['value']['givenName']
contact[:last_name] = field['value']['familyName']
when 'email'
contact[:email] = field['value']
end
end
contacts << contact
end
contacts
end
def gmail_client
@gmail_client ||= OAuth2::Client.new(AppConstants.google_client_id, AppConstants.google_client_secret, site: 'https://accounts.google.com', authorize_url: '/o/oauth2/auth', token_url: '/o/oauth2/token')
end
def hotmail_client
@hotmail_client ||= OAuth2::Client.new(AppConstants.hotmail_client_id, AppConstants.hotmail_client_secret, site: 'https://login.live.com', authorize_url: '/oauth20_authorize.srf', token_url: '/oauth20_token.srf')
end
def yahoo_consumer
@yahoo_consumer ||= OAuth::Consumer.new(AppConstants.yahoo_consumer_key, AppConstants.yahoo_consumer_secret, site: 'https://api.login.yahoo.com', request_token_path: '/oauth/v2/get_request_token', access_token_path: '/oauth/v2/get_token', authorize_path: '/oauth/v2/request_auth')
end
end
class ImportedContactsController < ApplicationController
before_filter :authenticate
before_filter :assign_contacts, only: :callback
respond_to :html
def redirect
importer = ContactImporter.new(params[:importer])
session[:oauth_token_secret] = importer.oauth_token_secret if params[:importer] == 'yahoo'
redirect_to importer.authorize_url
end
def callback
@address_book = AddressBook.create(source: params[:importer], user: current_account.user, imported_contacts_attributes: @contacts)
respond_with @address_book
end
private
def assign_contacts
importer = ContactImporter.new(params[:importer])
@contacts = importer.contacts(params[:code] || params[:oauth_verifier], params[:oauth_token], session[:oauth_token_secret])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment