Created
September 22, 2008 22:12
-
-
Save tow8ie/12167 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/https' | |
class GoogleContactsConnector | |
GOOGLE_AUTH_HOST = 'www.google.com' | |
GOOGLE_AUTH_PORT = 443 | |
GOOGLE_AUTH_PATH = '/accounts/ClientLogin' | |
CONTACTS_BASE_FEED = 'http://www.google.com/m8/feeds/contacts/default/full' | |
CONTACT_GROUPS_BASE_FEED = 'http://www.google.com/m8/feeds/groups/default/full' | |
def initialize(email, passwd) | |
@email = email | |
@passwd = passwd | |
@token = nil | |
@headers = {'Content-Type' => 'application/x-www-form-urlencoded'} | |
authenticate | |
@authority_group_id = get_authority_group_id if authenticated? | |
end | |
def authenticated? | |
!@token.nil? | |
end | |
def find_all | |
doc = REXML::Document.new(get_authority_contacts.body) | |
parse_contacts_to_array(doc, "/feed/entry") | |
end | |
def find_by_entry_id(entry_id) | |
doc = REXML::Document.new(get_contact(entry_id).body) | |
parse_contacts_to_array(doc, "/entry") | |
end | |
private | |
def authenticate | |
http = Net::HTTP.new(GOOGLE_AUTH_HOST, GOOGLE_AUTH_PORT) | |
http.use_ssl = true | |
data = "accountType=HOSTED_OR_GOOGLE&Email=#{@email}&Passwd=#{@passwd}&service=cp" | |
resp = http.post(GOOGLE_AUTH_PATH, data, @headers) | |
if resp.code == '200' | |
@token = resp.body[/Auth=(.*)/, 1] | |
@headers["Authorization"] = "GoogleLogin auth=#{@token}" | |
true | |
else | |
@token = nil | |
@headers.delete("Authorization") | |
false | |
end | |
end | |
def get_feed(uri) | |
if authenticated? | |
uri = URI.parse uri | |
if uri.query | |
full_path = uri.path + '?' + uri.query | |
else | |
full_path = uri.path | |
end | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
return http.get(full_path, @headers) | |
end | |
end | |
false | |
end | |
def get_all_groups | |
uri = CONTACT_GROUPS_BASE_FEED | |
get_feed uri | |
end | |
def get_authority_group_id | |
doc = REXML::Document.new get_all_groups.body | |
REXML::XPath.first(doc, "/feed/entry[title='author!ty']/id[1]/text()") | |
end | |
def get_authority_contacts | |
uri = CONTACTS_BASE_FEED + "?group=#{@authority_group_id}&max-results=1000" | |
get_feed uri | |
end | |
def get_contact(id) | |
uri = CONTACTS_BASE_FEED + "/#{id}" | |
get_feed uri | |
end | |
def parse_contacts_to_array(doc, xpath) | |
results = {} | |
doc.elements.each(xpath) do |entry| | |
entry.elements.each("id[1]") do |id| | |
contact_data = {} | |
contact_data[:self_link] = entry.elements.each("link[@rel='self'][1]/@href").first.to_s | |
contact_data[:name] = entry.elements.each("title[1]/text()").first.to_s | |
mails = entry.elements.collect("gd:email") do |email| | |
email.attributes["address"] | |
end | |
contact_data[:mails] = mails | |
entry_id_uri = id.get_text.value | |
contact_data[:entry_id_uri] = entry_id_uri | |
id = /\/base\/(.+)/.match(entry_id_uri)[1] | |
results[id] = contact_data | |
end | |
end | |
results | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment