google_apps_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Stuart
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = google_apps_api
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 James Stuart. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "google_apps_api"
8
+ gem.summary = %Q{Various APIs for Google Apps}
9
+ gem.description = %Q{APIs for Google Apps (currently Provisioning, Calendar, Calendar Resources)}
10
+ gem.email = "tastyhat@jamesstuart.org"
11
+ gem.homepage = "http://github.com/tastyhat/google_apps_api"
12
+ gem.authors = ["James Stuart"]
13
+ gem.add_dependency "httpclient"
14
+ gem.add_dependency "nokogiri"
15
+ gem.add_dependency "activesupport"
16
+ gem.add_development_dependency "shoulda", ">= 0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/*_test.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "google_apps_api #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,85 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{google_apps_api}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Stuart"]
12
+ s.date = %q{2010-05-05}
13
+ s.description = %q{APIs for Google Apps (currently Provisioning, Calendar, Calendar Resources)}
14
+ s.email = %q{tastyhat@jamesstuart.org}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "google_apps_api.gemspec",
27
+ "lib/config/calendar.yml",
28
+ "lib/config/provisioning.yml",
29
+ "lib/google_apps_api.rb",
30
+ "lib/google_apps_api/base_api.rb",
31
+ "lib/google_apps_api/calendar.rb",
32
+ "lib/google_apps_api/calendar_resources.rb",
33
+ "lib/google_apps_api/contacts.rb",
34
+ "lib/google_apps_api/exceptions.rb",
35
+ "lib/google_apps_api/provisioning.rb",
36
+ "lib/google_apps_api/user_profiles.rb",
37
+ "lib/load_config.rb",
38
+ "private/gapps-config.yml",
39
+ "private/userscalendars.xml",
40
+ "test/google_apps_api-calendar_resources_test.rb",
41
+ "test/google_apps_api_calendar_test.rb",
42
+ "test/google_apps_api_contacts_test.rb",
43
+ "test/google_apps_api_provisioning_test.rb",
44
+ "test/google_apps_api_user_profiles_test.rb",
45
+ "test/helper.rb",
46
+ "test/test_helper.rb"
47
+ ]
48
+ s.homepage = %q{http://github.com/tastyhat/google_apps_api}
49
+ s.rdoc_options = ["--charset=UTF-8"]
50
+ s.require_paths = ["lib"]
51
+ s.rubygems_version = %q{1.3.5}
52
+ s.summary = %q{Various APIs for Google Apps}
53
+ s.test_files = [
54
+ "test/google_apps_api-calendar_resources_test.rb",
55
+ "test/google_apps_api_calendar_test.rb",
56
+ "test/google_apps_api_contacts_test.rb",
57
+ "test/google_apps_api_provisioning_test.rb",
58
+ "test/google_apps_api_user_profiles_test.rb",
59
+ "test/helper.rb",
60
+ "test/test_helper.rb"
61
+ ]
62
+
63
+ if s.respond_to? :specification_version then
64
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
65
+ s.specification_version = 3
66
+
67
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
68
+ s.add_runtime_dependency(%q<httpclient>, [">= 0"])
69
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
70
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
71
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
72
+ else
73
+ s.add_dependency(%q<httpclient>, [">= 0"])
74
+ s.add_dependency(%q<nokogiri>, [">= 0"])
75
+ s.add_dependency(%q<activesupport>, [">= 0"])
76
+ s.add_dependency(%q<shoulda>, [">= 0"])
77
+ end
78
+ else
79
+ s.add_dependency(%q<httpclient>, [">= 0"])
80
+ s.add_dependency(%q<nokogiri>, [">= 0"])
81
+ s.add_dependency(%q<activesupport>, [">= 0"])
82
+ s.add_dependency(%q<shoulda>, [">= 0"])
83
+ end
84
+ end
85
+
@@ -0,0 +1,34 @@
1
+ :calendar:
2
+ :service: cl
3
+
4
+ :headers:
5
+ GData-Version: "2"
6
+
7
+ :action_subs:
8
+ :feed_basic: ":feed:/calendar/feeds"
9
+ :service: cl
10
+ :auth: https://www.google.com
11
+ :feed: https://www.google.com
12
+
13
+ :action_hash:
14
+ :domain_login:
15
+ :method: :post
16
+ :path: ":auth:/accounts/ClientLogin"
17
+ :format: :text
18
+ :retrieve_users_calendars:
19
+ :method: :get
20
+ :path: ":feed_basic:/:username:/allcalendars/full"
21
+ :feed: true
22
+ :class: GoogleAppsApi::CalendarEntry
23
+ :retrieve_users_own_calendars:
24
+ :method: :get
25
+ :path: ":feed_basic:/:username:/owncalendars/full"
26
+ :feed: true
27
+ :class: GoogleAppsApi::CalendarEntry
28
+ :add_calendar_to_user:
29
+ :method: :post
30
+ :path: ":feed_basic:/:username:/allcalendars/full"
31
+ :delete_calendar_from_user:
32
+ :method: :delete
33
+ :path: ":feed_basic:/:username:/allcalendars/full/:id:"
34
+
@@ -0,0 +1,36 @@
1
+ :provisioning:
2
+ :service: apps
3
+
4
+ :action_subs:
5
+ :service: apps
6
+ :auth: https://www.google.com
7
+ :feed: https://apps-apis.google.com
8
+ :path_user: ":feed:/a/feeds/:domain:/user/2.0"
9
+
10
+ :action_hash:
11
+ :domain_login:
12
+ :method: :post
13
+ :path: ":auth:/accounts/ClientLogin"
14
+ :format: :text
15
+ :rename_user:
16
+ :method: :put
17
+ :path: ":path_user:/:username:"
18
+ :delete_user:
19
+ :method: :delete
20
+ :path: ":path_user:/:username:"
21
+ :create_user:
22
+ :method: :post
23
+ :path: ":path_user:"
24
+ :retrieve_user:
25
+ :method: :get
26
+ :path: ":path_user:/:username:"
27
+ :class: GoogleAppsApi::UserEntry
28
+ :retrieve_all_users:
29
+ :method: :get
30
+ :path: ":path_user:"
31
+ :feed: true
32
+ :class: GoogleAppsApi::UserEntry
33
+ :update_user:
34
+ :method: :put
35
+ :path: ":path_user:/:username:"
36
+ :format: :text
@@ -0,0 +1,157 @@
1
+ include REXML
2
+
3
+ module GoogleAppsApi
4
+ class BaseApi
5
+
6
+
7
+ def initialize(api_name, *args)
8
+ api_config = GoogleAppsApi.config[api_name] || {}
9
+ options = args.extract_options!.merge!(api_config)
10
+ raise("Must supply admin_user") unless options[:admin_user]
11
+ raise("Must supply admin_password") unless options[:admin_password]
12
+ raise("Must supply domain") unless options[:domain]
13
+ @actions_hash = options[:action_hash] || raise("Must supply action hash")
14
+ @actions_subs = options[:action_subs] || raise("Must supply action subs")
15
+ @actions_hash[:next] = [:get, '']
16
+ @actions_subs[:domain] = options[:domain]
17
+
18
+ @token = login(options[:admin_user], options[:domain], options[:admin_password], options[:service])
19
+ @headers = {'Content-Type'=>'application/atom+xml', 'Authorization'=> 'GoogleLogin auth='+@token}.merge(options[:headers] || {})
20
+
21
+ end
22
+
23
+ def method_missing(method, *args, &block)
24
+ if @actions_hash.has_key?(method.to_sym)
25
+ request(method.to_sym, *args)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def setup_action(*args)
32
+ options = args.extract_options!
33
+ actions = options[:action_hash]
34
+
35
+ actions.each_pair do |k,v|
36
+ actions[k] = {:method => v[0], :path => (v[1].to_s.gsub!(/\:([^\:]+)\:/) { |sub| options[sub.gsub(/\:/,"").to_sym] })}
37
+ end
38
+
39
+ return actions
40
+ end
41
+
42
+ def login(username, domain, password, service)
43
+ request_body = '&Email='+CGI.escape(username + "@" + domain)+'&Passwd='+CGI.escape(password)+'&accountType=HOSTED&service='+ service + '&source=columbiaUniversity-google_apps_api-0.1'
44
+ res = request(:domain_login, :headers => {'Content-Type'=>'application/x-www-form-urlencoded'}, :body => request_body)
45
+
46
+
47
+ return /^Auth=(.+)$/.match(res.to_s)[1]
48
+ end
49
+
50
+ def request(action, *args)
51
+ options = args.extract_options!
52
+ options = {:headers => @headers}.merge(options)
53
+ action_hash = @actions_hash[action] || raise("invalid action #{action} called")
54
+
55
+ subs_hash = @actions_subs.merge(options)
56
+ subs_hash.each { |k,v| subs_hash[k] = action_gsub(v, subs_hash) if v.kind_of?(String)}
57
+
58
+ method = action_hash[:method]
59
+ path = action_gsub(action_hash[:path], subs_hash) + options[:query].to_s
60
+ is_feed = action_hash[:feed]
61
+ feed_class = action_hash[:class].constantize if action_hash[:class]
62
+ format = action_hash[:format] || :xml
63
+ response = http_request(method, path, options[:body], options[:headers])
64
+
65
+ if format == :text
66
+ return response.body.content
67
+ else
68
+ begin
69
+ xml = Nokogiri::XML(response.body.content) { |c| c.strict}
70
+ test_errors(xml)
71
+ if is_feed
72
+ entries = entryset(xml.css('feed>entry'),feed_class)
73
+
74
+
75
+ while (next_feed = xml.at_css('feed>link[rel=next]'))
76
+ response = http_request(:get, next_feed.attribute("href").to_s, nil, options[:headers])
77
+ xml = Nokogiri::XML(response.body.content) { |c| c.strict}
78
+ entries += entryset(xml.css('feed>entry'),feed_class)
79
+ end
80
+
81
+ entries
82
+ else
83
+ feed_class ? feed_class.new(xml) : xml
84
+ end
85
+
86
+
87
+ rescue Nokogiri::XML::SyntaxError => e
88
+ error = GDataError.new()
89
+ error.code = "SyntaxError"
90
+ error.input = "path: #{path}"
91
+ error.reason = "XML expected, syntax error"
92
+ raise error, e.to_s
93
+ end
94
+ end
95
+ end
96
+
97
+ def http_request(method, path, body, headers, redirects = 0)
98
+ @hc ||= HTTPClient.new
99
+
100
+ response = case method
101
+ when :delete
102
+ @hc.send(method, path, headers)
103
+ else
104
+ @hc.send(method, path, body, headers)
105
+ end
106
+
107
+ if response.status_code == 302 && (redirects += 1) < 10
108
+ response = http_request(method, response.header["Location"], body, headers, requests)
109
+ end
110
+ return response
111
+ end
112
+
113
+ def action_gsub(str, sub_hash)
114
+ str.gsub(/\:([^\:]+)\:/) { |key| sub_hash[key.gsub(/\:/,"").to_sym] }
115
+ end
116
+
117
+
118
+ # parses xml response for an API error tag. If an error, constructs and raises a GDataError.
119
+ def test_errors(xml)
120
+ error = xml.at_xpath("AppsForYourDomainErrors/error")
121
+ if error
122
+ gdata_error = GDataError.new
123
+ gdata_error.code = error.attribute("errorCode").content
124
+ gdata_error.input = error.attribute("invalidInput").content
125
+ gdata_error.reason = error.attribute("reason").content
126
+ msg = "error code : "+gdata_error.code+", invalid input : "+gdata_error.input+", reason : "+gdata_error.reason
127
+ raise gdata_error, msg
128
+ end
129
+ end
130
+
131
+ def entryset(entries, feed_class)
132
+ feed_class ? entries.collect { |en| feed_class.new(en)} : entries
133
+ end
134
+
135
+ def escapeXML(text)
136
+ CGI.escapeHTML(text.to_s)
137
+ end
138
+
139
+ end
140
+
141
+ class Entry
142
+
143
+ private
144
+
145
+ def escapeXML(text)
146
+ CGI.escapeHTML(text.to_s)
147
+ end
148
+
149
+ end
150
+
151
+ class GDataError < RuntimeError
152
+ attr_accessor :code, :input, :reason
153
+
154
+ def initialize()
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/ruby
2
+ include REXML
3
+
4
+
5
+
6
+ module GoogleAppsApi #:nodoc:
7
+
8
+ module Calendar
9
+ class Api < GoogleAppsApi::BaseApi
10
+ attr_reader :token
11
+
12
+ def initialize(*args)
13
+ super(:calendar, *args)
14
+ end
15
+
16
+ def retrieve_user_settings(username)
17
+ xml_response = request(:retrieve_user_settings, :username => username)
18
+ end
19
+
20
+ def add_calendar_to_user(calendar, user)
21
+ CalendarEntry.new(request(:add_calendar_to_user, :username => user, :body => calendar.add_message))
22
+ end
23
+
24
+ def delete_calendar_from_user(calendar, user)
25
+ request(:delete_calendar_from_user, :username => user, :id => calendar.id)
26
+ end
27
+ end
28
+
29
+
30
+
31
+
32
+
33
+ end
34
+
35
+ class CalendarEntry
36
+ attr_accessor :id, :title, :edit_link
37
+ def initialize(xml = nil)
38
+ if xml
39
+ @id = xml.at_css('id').content.gsub(/^.*calendars\//,"")
40
+ @title = xml.at_css('title').content
41
+ @edit_link = xml.at_css('link[rel=edit]').attribute('href').value
42
+ end
43
+ end
44
+
45
+ def to_s
46
+ title
47
+ end
48
+
49
+ def inspect
50
+ "<CalendarEntry: #{title} : #{id}, #{edit_link}>"
51
+ end
52
+
53
+ def add_message
54
+ Nokogiri::XML::Builder.new { |xml|
55
+ xml.entry(:xmlns => "http://www.w3.org/2005/Atom") {
56
+ xml.id_ {
57
+ xml.text id.to_s
58
+ }
59
+ }
60
+ }.to_xml
61
+ end
62
+ end
63
+
64
+
65
+ end
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/ruby
2
+ include REXML
3
+
4
+
5
+
6
+ module GoogleAppsApi #:nodoc:
7
+ module CalendarResources
8
+ class Api < BaseApi
9
+ attr_reader :token
10
+
11
+ def initialize(*args)
12
+ begin
13
+ action_list = {
14
+ :domain_login => [:post, ":auth:/accounts/ClientLogin"],
15
+ :retrieve_all_resources => [:get, ":feed_basic:/"],
16
+ }
17
+ end
18
+
19
+
20
+ options = args.extract_options!
21
+
22
+ domain = options[:domain]
23
+
24
+ options.merge!(:action_hash => action_list, :auth => "https://www.google.com", :feed => "https://apps-apis.google.com", :service => "apps")
25
+ options[:feed_basic] = options[:feed]+ "/a/feeds/calendar/resource/2.0/#{domain}"
26
+
27
+ super(options)
28
+ end
29
+ end
30
+
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,53 @@
1
+ # #!/usr/bin/ruby
2
+ #
3
+ # # require 'cgi'
4
+ # # require 'rexml/document'
5
+ # #
6
+ #
7
+ # include REXML
8
+ #
9
+ #
10
+ #
11
+ # module GoogleAppsApi #:nodoc:
12
+ # module SharedContacts
13
+ # class Api < GoogleAppsApi::BaseApi
14
+ # attr_reader :token
15
+ #
16
+ # def initialize(*args)
17
+ # action_list = {
18
+ # :domain_login => [:post, ":auth:/accounts/ClientLogin"],
19
+ # :retrieve_all => [:get, ":feed_basic:/full"],
20
+ # :create_contact => [:post, ":feed_basic:/full"]
21
+ # }
22
+ #
23
+ # options = args.extract_options!
24
+ # options.merge!(:action_hash => action_list, :auth => "https://www.google.com", :feed => "https://www.google.com", :service => "cp")
25
+ # options[:feed_basic] = options[:feed]+ "/m8/feeds/contacts/#{options[:domain]}"
26
+ #
27
+ # super(options)
28
+ # end
29
+ #
30
+ # def create_contact(*args)
31
+ # options = args.extract_options!
32
+ # msg = RequestMessage.new
33
+ # msg.add_contact(options)
34
+ # response = request(:create_contact, :body => msg.to_s)
35
+ # return response.to_s
36
+ # end
37
+ #
38
+ # end
39
+ #
40
+ # class RequestMessage < GoogleAppsApi::RequestMessage #:nodoc:
41
+ #
42
+ # def add_contact(*args)
43
+ # options = args.extract_options!
44
+ # base = self.elements["atom:entry"]
45
+ # base.add_element("atom:title", {"type" => "text"}).text = options[:name]
46
+ # base.add_element("atom:content", {"type" => "text"}).text = "Notes"
47
+ # (options[:email] || {}).each_pair do |email_type, address|
48
+ # base.add_element("gd:email", {"rel" => "http://schemas.google.com/g/2005##{email_type.to_s}", "address" => address})
49
+ # end
50
+ # end
51
+ # end
52
+ # end
53
+ # end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4
+ # use this file except in compliance with the License. You may obtain a copy of
5
+ # the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+ # License for the specific language governing permissions and limitations under
13
+ # the License.
14
+ module GoogleAppsApi #:nodoc:
15
+
16
+ class GDataError < RuntimeError
17
+ attr_accessor :code, :input, :reason
18
+ end
19
+ end