ruby_provisioning_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,28 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ coverage
12
+ InstalledFiles
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ # YARD artifacts
21
+ .yardoc
22
+ _yardoc
23
+ doc/
24
+ .idea
25
+ # Ignore wizzy configuration
26
+ .wizzy
27
+ wizzy
28
+ .wizzy-config
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@ruby_provisioning_api --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_provisioning_api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Davide Targa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # RubyProvisioningApi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ruby_provisioning_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ruby_provisioning_api
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+ http://rdoc.info/github/digitalprog/ruby_provisioning_api/master/frames
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ :username: brgdmn
2
+ :password: 23428dami
3
+ :domain: damianobraga.com
@@ -0,0 +1,51 @@
1
+ module RubyProvisioningApi
2
+
3
+ class Configuration
4
+
5
+ attr_accessor :config_file, :user_path, :group_path, :user_actions, :group_actions, :base_apps_url, :base_path
6
+ attr_reader :config
7
+
8
+ def initialize
9
+ #@config_file = "/home/davide/sites/ruby_provisioning_api/lib/ruby_provisioning_api/config/google_apps.yml"
10
+ config_file = "#{Rails.root}/config/google_apps.yml"
11
+
12
+ if File.exist?(config_file)
13
+ @config = YAML.load_file(config_file)
14
+ else
15
+ raise "RubyProvisioningApi: File #{config_file} not found, maybe you forgot to define it ?"
16
+ end
17
+
18
+ @base_apps_url = 'https://apps-apis.google.com'
19
+ @base_path = '/a/feeds'
20
+ @user_path = "/#{@config[:domain]}/user/2.0"
21
+ @group_path = "/group/2.0/#{@config[:domain]}"
22
+ @user_actions = {
23
+ :create => {method: "POST", url: "#{user_path}"},
24
+ :retrieve_all => {method: "GET", url: "#{user_path}"},
25
+ :retrieve => {:method => "GET", :url => "#{user_path}/userName"},
26
+ :delete => {:method => "DELETE", :url => "#{user_path}/userName"},
27
+ :update => {:method => "PUT", :url => "#{user_path}/userName"},
28
+ :member_of => {method: "GET", url: "#{group_path}/groupId/member/memberId"}
29
+ }
30
+ @group_actions = {
31
+ :create => { method: "POST" , url: "#{group_path}"},
32
+ :update => { method: "PUT" , url: "#{group_path}/groupId"},
33
+ :delete => { method: "DELETE" , url: "#{group_path}/groupId"},
34
+ :retrieve_all => { method: "GET" , url: "#{group_path}" },
35
+ :retrieve_groups => { method: "GET" , url: "#{group_path}/?member=memberId" },
36
+ :retrieve => { method: "GET" , url: "#{group_path}/groupId" },
37
+ :add_member => { method: "POST" , url: "#{group_path}/groupId/member" },
38
+ :add_owner => { method: "POST" , url: "#{group_path}/groupId/owner" },
39
+ :delete_member => { method: "DELETE" , url: "#{group_path}/groupId/member/memberId" },
40
+ :has_member => {method: "GET", url: "#{group_path}/groupId/member/memberId"},
41
+ :has_owner => {method: "GET", url: "#{group_path}/groupId/owner/ownerId"},
42
+ :delete_owner => {method: "DELETE" , url: "#{group_path}/groupId/owner/ownerId"},
43
+ :member => {method: "GET", url: "#{group_path}/groupId/member/memberId"},
44
+ :members => {method: "GET", url: "#{group_path}/groupId/member"},
45
+ :owner => {method: "GET", url: "#{group_path}/groupId/owner/ownerId"},
46
+ :owners => {method: "GET", url: "#{group_path}/groupId/owner"}
47
+ }
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,24 @@
1
+ module RubyProvisioningApi
2
+ class Connection
3
+
4
+ attr_reader :token
5
+
6
+ def initialize
7
+ client = client('https://www.google.com')
8
+ response = client.post '/accounts/ClientLogin', {:Email => "#{RubyProvisioningApi.configuration.config[:username]}@#{RubyProvisioningApi.configuration.config[:domain]}", :Passwd => RubyProvisioningApi.configuration.config[:password], :accountType => "HOSTED", :service => "apps"}
9
+ # Set the token
10
+ @token = response.body.split("\n").last.split('Auth=').last
11
+ end
12
+
13
+ def client(url)
14
+ # TODO: move ca_file option into initializer
15
+ client = Faraday.new(:url => url, :ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}) do |faraday|
16
+ faraday.request :url_encoded # form-encode POST params
17
+ # TODO :move log level into initializer
18
+ # faraday.response :logger # log requests to STDOUT
19
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,56 @@
1
+ module RubyProvisioningApi
2
+ module Entity
3
+
4
+ def perform(action, params = nil)
5
+ connection = RubyProvisioningApi.connection
6
+ client = connection.client(RubyProvisioningApi.configuration.base_apps_url)
7
+ method = action[:method]
8
+ url = action[:url]
9
+
10
+ response = client.send(action[:method].downcase) do |req|
11
+ req.url "#{RubyProvisioningApi.configuration.base_apps_url}#{RubyProvisioningApi.configuration.base_path}#{action[:url]}"
12
+ req.headers['Content-Type'] = 'application/atom+xml'
13
+ req.headers['Authorization'] = "GoogleLogin auth=#{connection.token}"
14
+ req.body = params if params
15
+ end
16
+ end
17
+
18
+ def response_error?(response)
19
+ (400..600).include?(response.status)
20
+ end
21
+
22
+ def check_response(response)
23
+ if (400..600).include?(response.status)
24
+ xml = Nokogiri::XML(response.body)
25
+ error_code = xml.xpath('//error').first.attributes["errorCode"].value
26
+ error_description = xml.xpath('//error').first.attributes["reason"].value
27
+ RubyProvisioningApi.const_set(error_description, Class.new(RubyProvisioningApi::Error)) unless RubyProvisioningApi.const_defined? error_description
28
+ raise "RubyProvisioningApi::#{error_description}".constantize
29
+ end
30
+ true
31
+ end
32
+
33
+ def present?(id)
34
+ begin
35
+ self.find(id)
36
+ true
37
+ rescue
38
+ false
39
+ end
40
+ end
41
+
42
+ def prepare_params_for(action, options = {})
43
+ options.stringify_keys!
44
+ params = deep_copy(RubyProvisioningApi.configuration.send("#{self.name.demodulize.underscore}_actions")[action])
45
+ options.each_pair do |k,v|
46
+ params[:url].gsub!(k, v)
47
+ end
48
+ params
49
+ end
50
+
51
+ def deep_copy(element)
52
+ Marshal.load(Marshal.dump(element))
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,24 @@
1
+ module RubyProvisioningApi
2
+
3
+ # A general RubyProvisioningApi exception
4
+ # All RubyProvisioningApi exceptions are descendants of the error class.
5
+ #
6
+ # *NOTE:* Exceptions raised by google provisioning api protocol in Entity::check_response are generated on the fly and
7
+ # are also descendants of the Error class.
8
+ #
9
+ #
10
+ #
11
+ class Error < StandardError
12
+
13
+ # @param [String] message the message of the exception
14
+ def initialize(message = nil)
15
+ @message = message || self.class.to_s.split("::").last.underscore.humanize
16
+ end
17
+
18
+ def to_s
19
+ @message
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,373 @@
1
+ module RubyProvisioningApi
2
+
3
+ # @attr [String] group_id Group identification
4
+ # @attr [String] group_name Group name
5
+ # @attr [String] description Group description
6
+ # @attr [String] email_permission Group permission: Owner or Member
7
+ #
8
+ class Group
9
+ extend Entity
10
+ include Member
11
+ include Owner
12
+
13
+ include ActiveModel::Validations
14
+ include ActiveModel::Dirty
15
+
16
+ attr_accessor :group_id, :group_name, :description, :email_permission
17
+
18
+ GROUP_ATTRIBUTES = ['groupId','groupName','description','emailPermission']
19
+
20
+ # @param [Hash] params the options to create a Group with.
21
+ # @option params [String] :group_id Group identification
22
+ # @option params [String] :group_name Group name
23
+ # @option params [String] :description Group description
24
+ # @option params [String] :email_permission Group permission: Owner or Member
25
+ #
26
+ def initialize(params = {})
27
+ params.each do |name, value|
28
+ send("#{name}=", value)
29
+ end
30
+ end
31
+
32
+ # Retrieve all groups in the domain
33
+ # @note This method executes a <b>GET</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain[?[start=]]</i>
34
+ #
35
+ # @example Retrieve all group in the current domain
36
+ # RubyProvisioningApi::Group.all # => [Array<Group>]
37
+ #
38
+ # @see https://developers.google.com/google-apps/provisioning/#retrieving_all_groups_in_a_domain
39
+ # @return [Array<Group>] all groups in the domain
40
+ #
41
+ def self.all
42
+ response = perform(RubyProvisioningApi.configuration.group_actions[:retrieve_all])
43
+ # Perform the request & Check if the response contains an error
44
+ check_response(response)
45
+ # Parse the response
46
+ xml = Nokogiri::XML(response.body)
47
+ # Prepare a Groups array
48
+ groups = []
49
+ xml.children.css("entry").each do |entry|
50
+ group = Group.new
51
+ GROUP_ATTRIBUTES.each do |attribute_name|
52
+ group.send("#{attribute_name.underscore}=", entry.css("apps|property[name='#{attribute_name}']").attribute("value").value)
53
+ end
54
+ # Fill groups array
55
+ groups << group
56
+ end
57
+ # Return the array of Groups
58
+ groups
59
+ end
60
+
61
+ # Retrieve a group
62
+ # @param [String] group_id Group identification
63
+ # @note This method executes a <b>GET</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId </i>
64
+ #
65
+ # @example Find the group "foo"
66
+ # group = RubyProvisioningApi::Group.find("foo") # => [Group]
67
+ #
68
+ # @see https://developers.google.com/google-apps/provisioning/#retrieving_a_group
69
+ # @return [Group] the group found
70
+ # @raise [Error] if group does not exist
71
+ #
72
+ def self.find(group_id)
73
+ params = prepare_params_for(:retrieve, "groupId" => group_id)
74
+ response = perform(params)
75
+ # Check if the response contains an error
76
+ check_response(response)
77
+ # Parse the response
78
+ xml = Nokogiri::XML(response.body)
79
+ group = Group.new
80
+ for attribute_name in ['groupId','groupName','description','emailPermission']
81
+ group.send("#{attribute_name.underscore}=",xml.children.css("entry apps|property[name='#{attribute_name}']").attribute("value").value)
82
+ end
83
+ group
84
+ end
85
+
86
+ # Initialize and save a group
87
+ # @param [Hash] params the options to create a Group with.
88
+ # @option params [String] :group_id Group identification
89
+ # @option params [String] :group_name Group name
90
+ # @option params [String] :description Group description
91
+ # @option params [String] :email_permission Group permission: Owner or Member
92
+ # @note This method executes a <b>POST</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain</i>
93
+ #
94
+ # @example Create the group "foo"
95
+ # group = RubyProvisioningApi::Group.create(:group_id => "foo",
96
+ # :group_name => "foo name" ,
97
+ # :description => "bar",
98
+ # :email_permission => "Owner") # => true
99
+ #
100
+ # @see https://developers.google.com/google-apps/provisioning/#creating_a_group
101
+ # @return [Boolean] true if created, false if not valid or not created
102
+ # @raise [Error] if group already exists (group_id must be unique)
103
+ #
104
+ def self.create(params = {})
105
+ group = Group.new(params).save
106
+ end
107
+
108
+ # Save a group
109
+ # @note This method executes a <b>POST</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain</i>
110
+ #
111
+ # @example Save the group "foo"
112
+ # group = RubyProvisioningApi::Group.new( :group_id => "foo", :group_name => "foo name" , :description => "bar", :email_permission => "Owner" ).save # => true
113
+ #
114
+ # @see https://developers.google.com/google-apps/provisioning/#creating_a_group
115
+ # @return [Boolean] true if saved, false if not valid or not saved
116
+ # @raise [Error] if group already exists (group_id must be unique)
117
+ #
118
+ def save
119
+ return false unless valid?
120
+ # If group is present, this is an update
121
+ update = Group.present?(group_id)
122
+ # Creating the XML request
123
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
124
+ xml.send(:'atom:entry', 'xmlns:atom' => 'http://www.w3.org/2005/Atom', 'xmlns:apps' => 'http://schemas.google.com/apps/2006') {
125
+ xml.send(:'atom:category', 'scheme' => 'http://schemas.google.com/g/2005#kind', 'term' => 'http://schemas.google.com/apps/2006#emailList')
126
+ xml.send(:'apps:property', 'name' => 'groupId', 'value' => group_id) if update
127
+ xml.send(:'apps:property', 'name' => 'groupName', 'value' => group_name)
128
+ xml.send(:'apps:property', 'name' => 'description', 'value' => description)
129
+ xml.send(:'apps:property', 'name' => 'emailPermission', 'value' => email_permission)
130
+ }
131
+ end
132
+ if !update
133
+ #Acting on a new object
134
+ # Check if the response contains an error
135
+ self.class.check_response(self.class.perform(RubyProvisioningApi.configuration.group_actions[:create],builder.to_xml))
136
+ else
137
+ #Acting on an existing object
138
+ params = self.class.prepare_params_for(:update, "groupId" => group_id)
139
+ # Perform the request & Check if the response contains an error
140
+ self.class.check_response(self.class.perform(params,builder.to_xml))
141
+ end
142
+ end
143
+
144
+ # Update a group
145
+ # @note This method executes a <b>PUT</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId</i>
146
+ #
147
+ # @example Update the group "test" description from "foo" to "bar"
148
+ # group = RubyProvisioningApi::Group.find("test") # => true
149
+ # group.description # => "foo"
150
+ # group.description = "bar" # => "bar"
151
+ # group.update # => true
152
+ #
153
+ # @see https://developers.google.com/google-apps/provisioning/#updating_a_group
154
+ # @return [Boolean] true if updated, false otherwise
155
+ # @raise [Error] if group does not exist
156
+ #
157
+ def update
158
+ save
159
+ end
160
+
161
+ # Update attributes of a group
162
+ # @param [Hash] params the options to update a Group
163
+ # @option params [String] :group_name Group name
164
+ # @option params [String] :description Group description
165
+ # @option params [String] :email_permission Group permission: Owner or Member
166
+ # @note This method executes a <b>PUT</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId</i>
167
+ #
168
+ # @example Update the group "test"
169
+ # group = RubyProvisioningApi::Group.find("test")
170
+ # group.update_attributes(:group_id => "foo", :group_name => "foo name" , :description => "bar", :email_permission => "Owner") # => true
171
+ #
172
+ # @see https://developers.google.com/google-apps/provisioning/#updating_a_group
173
+ # @return [Boolean] true if updated, false if not valid or not updated
174
+ # @raise [Error] if group does not exist
175
+ #
176
+ def update_attributes(params = {})
177
+ self.group_name = params[:group_name] if params[:group_name]
178
+ self.description = params[:description] if params[:description]
179
+ self.email_permission = params[:email_permission] if params[:email_permission]
180
+ update
181
+ end
182
+
183
+ # Delete group
184
+ # @note This method executes a <b>DELETE</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId </i>
185
+ #
186
+ # @example Delete the group "test"
187
+ # group = RubyProvisioningApi::Group.find("test")
188
+ # group.delete
189
+ #
190
+ # @see https://developers.google.com/google-apps/provisioning/#deleting_a_group
191
+ # @return [Boolean] true if deleted, false otherwise
192
+ # @raise [Error] if group does not exist
193
+ #
194
+ def delete
195
+ params = self.class.prepare_params_for(:delete, "groupId" => group_id)
196
+ # Perform the request & Check if the response contains an error
197
+ self.class.check_response(self.class.perform(params))
198
+ end
199
+
200
+ # Retrieve all groups for a given member
201
+ # @param [String] member_id Member identification
202
+ # @note This method executes a <b>GET</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/?member=memberId[&directOnly=true|false]</i>
203
+ #
204
+ # @example Retrieve all groups for a member "foo"
205
+ # groups = RubyProvisioningApi::Group.groups("foo") # => [Array<Group>]
206
+ #
207
+ # @see https://developers.google.com/google-apps/provisioning/#retrieving_all_groups_for_a_member
208
+ # @return [Array<Group>] all groups for a given member
209
+ # @raise [Error] if member(user) does not exist
210
+ #
211
+ def self.groups(member_id)
212
+ params = prepare_params_for(:retrieve_groups, "memberId" => member_id)
213
+ response = perform(params)
214
+ # Perform the request & Check if the response contains an error
215
+ check_response(response)
216
+ # Parse the response
217
+ xml = Nokogiri::XML(response.body)
218
+ # Prepare a Groups array
219
+ groups = []
220
+ xml.children.css("entry").each do |entry|
221
+ # Prepare a Group object
222
+ group = Group.new
223
+ GROUP_ATTRIBUTES.each do |attribute_name|
224
+ # Set group attributes
225
+ group.send("#{attribute_name.underscore}=", entry.css("apps|property[name='#{attribute_name}']").attribute("value").value)
226
+ end
227
+ # Fill groups array
228
+ groups << group
229
+ end
230
+ # Return the array of Groups
231
+ groups
232
+ end
233
+
234
+ # Add member to group
235
+ # @param [String] member_id Member identification
236
+ # @note This method executes a <b>POST</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId/member </i>
237
+ #
238
+ # @example Add member "foo" to group "bar"
239
+ # group = RubyProvisioningApi::Group.find("bar") # => [Group]
240
+ # group.add_member("foo") # => [true]
241
+ #
242
+ # @see https://developers.google.com/google-apps/provisioning/#adding_a_member_to_a_group
243
+ # @return [Boolean] true if added as a member, false otherwise
244
+ # @raise [Error] if member(user) does not exist
245
+ #
246
+ def add_member(member_id)
247
+ user = User.find(member_id)
248
+ # Creating the XML request
249
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
250
+ xml.send(:'atom:entry', 'xmlns:atom' => 'http://www.w3.org/2005/Atom', 'xmlns:apps' => 'http://schemas.google.com/apps/2006') {
251
+ xml.send(:'atom:category', 'scheme' => 'http://schemas.google.com/g/2005#kind', 'term' => 'http://schemas.google.com/apps/2006#emailList')
252
+ xml.send(:'apps:property', 'name' => 'memberId', 'value' => member_id)
253
+ }
254
+ end
255
+ params = self.class.prepare_params_for(:add_member, "groupId" => group_id)
256
+ # Perform the request & Check if the response contains an error
257
+ self.class.check_response(self.class.perform(params,builder.to_xml))
258
+ end
259
+
260
+ # Group membership of a given member
261
+ # @param [String] member_id Member identification
262
+ # @note This method executes a <b>GET</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId/member/memberId</i>
263
+ #
264
+ # @example Check if user "foo" is member to the group "bar"
265
+ # group = RubyProvisioningApi::Group.find("bar") # => [Group]
266
+ # group.has_member?("foo") # => [true]
267
+ #
268
+ # @see https://developers.google.com/google-apps/provisioning/#retrieving_all_members_of_a_group
269
+ # @return [Boolean] true if user is a member of the group
270
+ # @raise [Error] if member(user) does not exist
271
+ #
272
+ def has_member?(member_id)
273
+ params = self.class.prepare_params_for(:has_member, { "groupId" => group_id, "memberId" => member_id })
274
+ begin
275
+ # Perform the request & Check if the response contains an error
276
+ self.class.check_response(self.class.perform(params))
277
+ rescue
278
+ User.find(owner_id)
279
+ false
280
+ end
281
+ end
282
+
283
+
284
+ # Delete group membership of a given member
285
+ # @param [String] member_id Member identification
286
+ # @note This method executes a <b>DELETE</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId/member/memberId</i>
287
+ #
288
+ # @example Delete user "foo" membership from group "bar"
289
+ # group = RubyProvisioningApi::Group.find("bar") # => [Group]
290
+ # group.delete_member("foo") # => [true]
291
+ #
292
+ # @see https://developers.google.com/google-apps/provisioning/#deleting_member_from_an_group
293
+ # @return [Boolean] true if deleted, false otherwise
294
+ # @raise [Error] if member(user) does not exist
295
+ #
296
+ def delete_member(member_id)
297
+ params = self.class.prepare_params_for(:delete_member, { "groupId" => group_id, "memberId" => member_id })
298
+ # Perform the request & Check if the response contains an error
299
+ self.class.check_response(self.class.perform(params))
300
+ end
301
+
302
+ # Add owner to group
303
+ # @param [String] owner_id Owner identification
304
+ # @note This method executes a <b>POST</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId/owner</i>
305
+ #
306
+ # @example Add owner "foo" to group "bar"
307
+ # group = RubyProvisioningApi::Group.find("bar") # => [Group]
308
+ # group.add_owner("foo") # => [true]
309
+ #
310
+ # @see https://developers.google.com/google-apps/provisioning/#assigning_an_owner_to_a_group
311
+ # @return [Boolean] true if added as a owner, false otherwise
312
+ # @raise [Error] if owner(user) does not exist
313
+ #
314
+ def add_owner(owner_id)
315
+ user = User.find(owner_id)
316
+ # Creating the XML request
317
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
318
+ xml.send(:'atom:entry', 'xmlns:atom' => 'http://www.w3.org/2005/Atom', 'xmlns:apps' => 'http://schemas.google.com/apps/2006') {
319
+ xml.send(:'atom:category', 'scheme' => 'http://schemas.google.com/g/2005#kind', 'term' => 'http://schemas.google.com/apps/2006#emailList')
320
+ xml.send(:'apps:property', 'name' => 'email', 'value' => owner_id)
321
+ }
322
+ end
323
+ params = self.class.prepare_params_for(:add_owner, "groupId" => group_id )
324
+ # Perform the request & Check if the response contains an error
325
+ self.class.check_response(self.class.perform(params,builder.to_xml))
326
+ end
327
+
328
+ # Group ownership of a given owner
329
+ # @param [String] owner_id Owner identification
330
+ # @note This method executes a <b>GET</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId/owner/ownerEmail
331
+ # @example Check if user "foo" is owner to the group "bar"
332
+ # group = RubyProvisioningApi::Group.find("bar") # => [Group]
333
+ # group.has_owner?("foo") # => [true]
334
+ #
335
+ # @see https://developers.google.com/google-apps/provisioning/#querying_if_a_user_or_group_is_owner
336
+ # @return [Boolean] true if user is a owner of the group, false otherwise
337
+ # @raise [Error] if owner(user) does not exist
338
+ #
339
+ def has_owner?(owner_id)
340
+ params = self.class.prepare_params_for(:has_owner, {"groupId" => group_id, "ownerId" => owner_id} )
341
+ begin
342
+ # Perform the request & Check if the response contains an error
343
+ self.class.check_response(self.class.perform(params))
344
+ rescue
345
+ User.find(owner_id)
346
+ false
347
+ end
348
+ end
349
+
350
+
351
+ # Delete group ownership of a given owner
352
+ # @param [String] owner_id Owner identification
353
+ # @note This method executes a <b>DELETE</b> request to <i>apps-apis.google.com/a/feeds/group/2.0/domain/groupId/owner/ownerEmail</i>
354
+ #
355
+ # @example Delete user "foo" ownership from group "bar"
356
+ # group = RubyProvisioningApi::Group.find("bar") # => [Group]
357
+ # group.delete_owner("foo") # => [true]
358
+ #
359
+ # @see https://developers.google.com/google-apps/provisioning/#deleting_an_owner_from_a_group
360
+ # @return [Boolean] true if deleted, false otherwise
361
+ # @raise [Error] if owner(user) does not exist
362
+ #
363
+ def delete_owner(owner_id)
364
+ params = self.class.prepare_params_for(:delete_owner, {"groupId" => group_id, "ownerId" => owner_id} )
365
+ # Perform the request & Check if the response contains an error
366
+ self.class.check_response(self.class.perform(params))
367
+ end
368
+
369
+ end
370
+ end
371
+
372
+
373
+