lib/google_apps/transport.rb in google_apps-0.4.6 vs lib/google_apps/transport.rb in google_apps-0.4.8

- old
+ new

@@ -3,14 +3,18 @@ require 'openssl' require 'rexml/document' module GoogleApps class Transport - attr_reader :request, :response, :domain + attr_reader :request, :response, :domain, :feeds attr_accessor :auth, :user, :group, :nickname, :export BOUNDARY = "=AaB03xDFHT8xgg" + PAGE_SIZE = { + user: 100, + group: 200 + } def initialize(domain, targets = {}) @auth = targets[:auth] || "https://www.google.com/accounts/ClientLogin" @user = targets[:user] || "https://apps-apis.google.com/a/feeds/#{domain}/user/2.0" @pubkey = targets[:pubkey] || "https://apps-apis.google.com/a/feeds/compliance/audit/publickey/#{domain}" @@ -20,10 +24,11 @@ @export = targets[:export] || "https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/#{domain}" @domain = domain @token = nil @response = nil @request = nil + @feeds = [] end # authenticate will take the provided account and # password and attempt to authenticate them with @@ -107,19 +112,45 @@ # get 'endpoint', 'username' # # get returns the HTTP response received from Google. def get(endpoint, id = nil) # TODO: Need to handle <link rel='next' for pagination if wanting all users - id ? uri = URI(endpoint + "/#{id}") : uri = URI(endpoint) - #uri = URI(instance_variable_get("@#{endpoint.to_s}") + "/#{id}") - @request = Net::HTTP::Get.new(uri.path) + id ? uri = URI(endpoint + build_id(id)) : uri = URI(endpoint) + @request = Net::HTTP::Get.new(uri.request_uri) set_headers :user @response = request uri end + # get_users retrieves as many users as specified from the + # domain. If no starting point is given it will grab all the + # users in the domain. If a starting point is specified all + # users from that point on (alphabetically) will be returned. + # + # get_users start: 'lholcomb2' + # + # get_users returns the final response from google. + def get_users(options = {}) + # TODO: Limit isn't working right. It stops the retrieval but not as soon as it should. + @feeds, page = [], 0 + + options[:limit] ? limit = options[:limit] : limit = 1000000 + options[:start] ? get(@user + "?startUsername=#{options[:start]}") : get(@user) + + add_feed + + while (next_page = get_next(@feeds.last.xml)) and (page * PAGE_SIZE[:user] < limit) + get @feeds.last.next_page + add_feed + page += 1 + end + + @response + end + + # add_member_to adds a member to a group in the domain. # It takes a group_id and a GoogleApps::Atom::GroupMember # document as arguments. # # add_member_to 'test', document @@ -244,14 +275,28 @@ def auth_body(account, pass) "&Email=#{CGI::escape(account)}&Passwd=#{CGI::escape(pass)}&accountType=HOSTED&service=apps" end + # build_id checks the id string. If it is formatted + # as a query string it is returned as is. If not + # a / is prepended to the id string. + def build_id(id) + id =~ /^\?/ ? id : "/#{id}" + end + + # Grab the auth token from the response body def set_auth_token @response.body.split("\n").grep(/auth=(.*)/i) @token = $1 + end + + + # add_feed adds a feed to the @feeds array. + def add_feed + @feeds << GoogleApps::Atom.feed(@response.body) end def request(uri) # TODO: Clashes with @request reader \ No newline at end of file