lib/google_apps/transport.rb in google_apps-0.4.8.1 vs lib/google_apps/transport.rb in google_apps-0.4.9
- old
+ new
@@ -2,56 +2,56 @@
require 'cgi'
require 'openssl'
require 'rexml/document'
module GoogleApps
- class Transport
- attr_reader :request, :response, :domain, :feeds
- attr_accessor :auth, :user, :group, :nickname, :export
+ class Transport
+ 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"
+ 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}"
@migration = targets[:migration] || "https://apps-apis.google.com/a/feeds/migration/2.0/#{domain}"
- @group = targets[:group] || "https://apps-apis.google.com/a/feeds/group/2.0/#{domain}"
- @nickname = targets[:nickname] || "https://apps-apis.google.com/a/feeds/#{domain}/nickname/2.0"
+ @group = targets[:group] || "https://apps-apis.google.com/a/feeds/group/2.0/#{domain}"
+ @nickname = targets[:nickname] || "https://apps-apis.google.com/a/feeds/#{domain}/nickname/2.0"
@export = targets[:export] || "https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/#{domain}"
@domain = domain
- @token = nil
- @response = nil
- @request = nil
+ @token = nil
+ @response = nil
+ @request = nil
@feeds = []
- end
+ end
# authenticate will take the provided account and
# password and attempt to authenticate them with
# Google
#
# authenticate 'username@domain', 'password'
#
# authenticate returns the HTTP response received
# from Google
- def authenticate(account, pass)
- uri = URI(@auth)
- @request = Net::HTTP::Post.new(uri.path)
- @request.body = auth_body(account, pass)
- set_headers :auth
+ def authenticate(account, pass)
+ uri = URI(@auth)
+ @request = Net::HTTP::Post.new(uri.path)
+ @request.body = auth_body(account, pass)
+ set_headers :auth
- @response = request uri
+ @response = request uri
- set_auth_token
+ set_auth_token
@response
- end
+ end
# request_export performs the GoogleApps API call to
# generate a mailbox export. It takes the username
# and an GoogleApps::Atom::Export instance as
# arguments
@@ -129,21 +129,45 @@
#
# get_users start: 'lholcomb2'
#
# get_users returns the final response from google.
def get_users(options = {})
- @feeds, pages = [], 0
+ get_all :users, options
+ end
+
+ # get_groups retrieves all the groups from the domain
+ #
+ # get_groups
+ #
+ # get_groups returns the final response from Google.
+ def get_groups(options = {})
+ get_all :groups, options
+ end
+
+
+ # get_all retrieves a batch of records of the specified type
+ # from google. You must specify the type of object you want
+ # to retreive. You can also specify a start point and a limit.
+ #
+ # get_all 'users', start: 'lholcomb2', limit: 300
+ #
+ # get_all returns the HTTP response received from Google.
+ def get_all(type, options = {})
+ @feeds, current_page = [], 0
+ type = type.to_s
+ type.gsub!(/\w*s$/) { |match| match[0..-2] }
+
options[:limit] ? limit = options[:limit] : limit = 1000000
- options[:start] ? get(@user + "?startUsername=#{options[:start]}") : get(@user)
+ options[:start] ? get(instance_variable_get("@#{type}") + "?#{start_query(type)}=#{options[:start]}") : get(instance_variable_get("@#{type}"))
add_feed
- pages += 1
+ current_page += 1
- while (@feeds.last.next_page) and (pages * PAGE_SIZE[:user] < limit)
+ while (@feeds.last.next_page) and (current_page * PAGE_SIZE[:user] < limit)
get_next_page
- pages += 1
+ current_page += 1
end
@response
end
@@ -169,26 +193,37 @@
def delete_member_from(group_id, member_id)
delete(@group + "/#{group_id}/member", member_id)
end
+ # get_nicknames_for retrieves all the nicknames associated
+ # with the requested user. It takes the username as a string.
+ #
+ # get_nickname_for 'lholcomb2'
+ #
+ # get_nickname_for returns the HTTP response from Google
+ def get_nicknames_for(login)
+ get_nickname "?username=#{login}"
+ end
+
+
# add is a generic target for method_missing. It is
# intended to handle the general case of adding
# to the GoogleApps Domain. It takes an API endpoint
# and a GoogleApps::Atom document as arguments.
#
# add 'endpoint', document
#
# add returns the HTTP response received from Google.
- def add(endpoint, document)
- uri = URI(endpoint)
- @request = Net::HTTP::Post.new(uri.path)
- @request.body = document.to_s
- set_headers :user
+ def add(endpoint, document)
+ uri = URI(endpoint)
+ @request = Net::HTTP::Post.new(uri.path)
+ @request.body = document.to_s
+ set_headers :user
- @response = request uri
- end
+ @response = request uri
+ end
# update is a generic target for method_missing. It is
# intended to handle the general case of updating an
# item that already exists in your GoogleApps Domain.
# It takes an API endpoint and a GoogleApps::Atom document
@@ -241,31 +276,30 @@
end
# TODO: This should perform the instance_variable_get and pass the value to the appropriate method.
def method_missing(name, *args)
- super unless name.match /([a-z]*)_([a-z]*)/
+ super unless name.match /([a-z]*)_([a-z]*)/
case $1
- when "new"
- self.send(:add, instance_variable_get("@#{$2}"), *args)
+ when "new", "add"
+ self.send(:add, instance_variable_get("@#{$2}"), *args)
when "delete"
self.send(:delete, instance_variable_get("@#{$2}"), *args)
when "update"
self.send(:update, instance_variable_get("@#{$2}"), *args)
when "get"
self.send(:get, instance_variable_get("@#{$2}"), *args)
- when "add"
- self.send(:add, instance_variable_get("@#{$2}"), *args)
else
- super
+ super
end
end
private
+
# auth_body generates the body for the authentication
# request made by authenticate.
#
# auth_body 'username@domain', 'password'
#
@@ -296,35 +330,48 @@
get @feeds.last.next_page
add_feed
end
+ # start_query builds the value for the starting point
+ # query string used for retrieving batches of objects
+ # from Google.
+ def start_query(type)
+ case type
+ when 'user'
+ "startUsername"
+ when 'group'
+ "startGroup"
+ end
+ end
+
+
# add_feed adds a feed to the @feeds array.
def add_feed
@feeds << GoogleApps::Atom.feed(@response.body)
end
- def request(uri)
+ def request(uri)
# TODO: Clashes with @request reader
- Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
- http.request(@request)
- end
- end
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
+ http.request(@request)
+ end
+ end
- def set_headers(request_type)
+ def set_headers(request_type)
case request_type
when :auth
@request['content-type'] = "application/x-www-form-urlencoded"
when :migrate
@request['content-type'] = "multipart/related; boundary=\"#{BOUNDARY}\""
@request['authorization'] = "GoogleLogin auth=#{@token}"
else
@request['content-type'] = "application/atom+xml"
@request['authorization'] = "GoogleLogin auth=#{@token}"
end
- end
+ end
def multi_part(properties, message)
post_body = []
post_body << "--#{BOUNDARY}\n"
post_body << "Content-Type: application/atom+xml\n\n"
@@ -334,7 +381,7 @@
post_body << message.to_s
post_body << "\n--#{BOUNDARY}--}"
post_body.join
end
- end
+ end
end
\ No newline at end of file