Created
June 7, 2010 15:55
-
-
Save pristinenoise/428836 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
class BAction | |
attr_reader :category, :details, :result, :notes, :connector | |
def initialize(category, details, result, connector = nil) | |
@category = category | |
@details = details | |
@result = result | |
@connector = connector | |
@notes = [] | |
end | |
def run!(*args) | |
options = args.extract_options! | |
mode = options.delete(:mode) || "Test" | |
begin | |
raise "Task already run successfully." if @result == :success | |
begin | |
self.send("run_#{@category.to_s}", mode, options) | |
rescue Exception => e | |
notes << "Task failed: #{e.message}" | |
@result = :failure | |
end | |
end | |
end | |
private | |
def user_exists?(user, p_api) | |
begin | |
return p_api.retrieve_user(user) | |
rescue | |
return false | |
end | |
end | |
def run_add_calendars(mode, options) | |
p_api = @connector.provisioning_api | |
uni = @details[:uni] || raise("Uni not provided") | |
first_name = @details[:first] || raise("First not provided") | |
last_name = @details[:last] || raise("Last not provided") | |
title = first_name + " " + last_name | |
if user_exists?(uni, p_api) | |
@result = :not_necessary | |
else | |
if mode == "Run" | |
ue = p_api.create_user(uni, :given_name => first_name, :family_name => last_name, :password => random_letters(10)) | |
@connector.calendar_api.update_calendar_for_user(ue.entity_for_base_calendar, ue, :title => title) | |
@result = :success | |
else | |
@result = :would_have_run | |
end | |
end | |
end | |
def run_delete_contact(mode, options) | |
co_api = @connector.contacts_api | |
ce = GoogleAppsApi::ContactEntity.new(:id => @details[:id], :domain => co_api.domain) | |
if mode == "Run" | |
co_api.remove_contact(ce) | |
@result = :success | |
else | |
@result = :would_have_run | |
end | |
end | |
def run_add_contact(mode, options) | |
co_api = @connector.contacts_api | |
ce_hash = {:id => "_new_", :name => @details[:name], :emails => {:work => @details[:work]}, :primary_email => :work} | |
ce_hash[:emails][:home] = @details[:home] unless @details[:home].to_s.empty? | |
ce = GoogleAppsApi::ContactEntity.new(ce_hash) | |
if mode == "Run" | |
co_api.create_contact(ce) | |
@result = :success | |
else | |
@result = :would_have_run | |
end | |
end | |
def run_set_calendar_permission(mode, options) | |
c_api = @connector.calendar_api | |
user = GoogleAppsApi::UserEntity.new(:id => @details[:user]) | |
cal = GoogleAppsApi::CalendarEntity.new(@details[:calendar]) | |
if mode == "Run" | |
c_api.set_calendar_for_user(cal, user, :accesslevel => @details[:acl], :acl_comparison => @details[:acl_comparison], :color => @details[:color], :do_not_subscribe => @details[:do_not_subscribe]) | |
@result = :success | |
else | |
@result = :would_have_run | |
end | |
end | |
def run_update_title(mode, options) | |
c_api = @connector.calendar_api | |
ue = GoogleAppsApi::UserEntity.new(@details[:user]) | |
base_cal = ue.get_base_calendar(c_api) | |
raise "No calendar title provided." if @details[:title].empty? | |
if base_cal | |
if base_cal.details[:title].include?("cul.columbia.edu") | |
if mode == "Run" | |
c_api.update_calendar_for_user(base_cal, ue, :title => @details[:title]) | |
@result = :success | |
else | |
@result = :would_have_run | |
end | |
else | |
@result = :not_necessary | |
end | |
else | |
@result = :user_not_found | |
end | |
end | |
def random_letters(num, prefix = "", suffix = "") | |
prefix + (0...num).map{65.+(rand(25)).chr}.join + suffix | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment