#!/usr/bin/ruby module GoogleAppsApi #:nodoc: module Contacts class Api < GoogleAppsApi::BaseApi attr_reader :token def initialize(*args) super(:contacts, *args) end def retrieve_all_contacts(*args) options = args.extract_options! request(:retrieve_all_contacts, options) end def remove_contact(contact, *args) options = args.extract_options!.merge(:contact => contact.id_escaped, :merge_headers => {"If-Match" => "*"}) request(:remove_contact, options) end def create_contact(contact, *args) req = <<-DESCXML #{contact.name} DESCXML contact.emails.each_pair do |loc, email| req += <<-DESCXML DESCXML end req += "" options = args.extract_options!.merge(:body => req.strip) request(:create_contact, options) end end end class ContactEntity < Entity attr_reader :name, :emails, :primary_email def initialize(*args) @emails = {} options = args.extract_options! if (_xml = options[:xml]) xml = _xml.at_css("entry") || _xml @kind = "contact" @id = xml.at_css("id").content.gsub(/^.+\/base\//,"") @domain = xml.at_css("id").content.gsub(/^.+\/contacts\/([^\/]+)\/.+$/,"\\1") @name = xml.at_css("title").content xml.css("gd|email").each do |email_node| loc = email_node.attribute("rel").content.gsub(/^.+\#/,"").to_sym email = email_node.attribute("address").content primary = email_node.attribute("primary") && email_node.attribute("primary").content == "true" @primary_email = loc if primary @emails[loc] = email end else if args.first.kind_of?(String) super(:contact => args.first) else @name = options.delete(:name) @emails = options.delete(:emails) || {} super(options.merge(:kind => "contact")) end end end def ==(other) super(other) end end end