Class: GoogleApps::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/google_apps/transport.rb

Constant Summary collapse

BOUNDARY =
"=AaB03xDFHT8xgg"
PAGE_SIZE =
{
  user: 100,
  group: 200
}
FEEDS_ROOT =
'https://apps-apis.google.com/a/feeds'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Transport

Returns a new instance of Transport.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/google_apps/transport.rb', line 17

def initialize(options)
  @domain = options[:domain]
  @token = options[:token]
  @refresh_token = options[:refresh_token]
  @token_changed_callback = options[:token_changed_callback]

  @user       = "#{FEEDS_ROOT}/#{@domain}/user/2.0"
  @pubkey     = "#{FEEDS_ROOT}/compliance/audit/publickey/#{@domain}"
  @migration  = "#{FEEDS_ROOT}/migration/2.0/#{@domain}"
  @group      = "#{FEEDS_ROOT}/group/2.0/#{@domain}"
  @nickname   = "#{FEEDS_ROOT}/#{@domain}/nickname/2.0"

  audit_root  = "#{FEEDS_ROOT}/compliance/audit/mail"
  @export     = "#{audit_root}/export/#{@domain}"
  @monitor    = "#{audit_root}/monitor/#{@domain}"

  @requester = AppsRequest
  @doc_handler = DocumentHandler.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/google_apps/transport.rb', line 305

def method_missing(name, *args)
  super unless name.match /([a-z]*)_([a-z]*)/

  case $1
  when "new", "add"
    response = self.send(:add, send($2), *args)
    process_response(response)
    create_doc(response.body, $2)
  when "delete"
    response = self.send(:delete, send($2), *args)
    process_response(response)
    create_doc(response.body, $2)
  when "update"
    response = self.send(:update, send($2), *args)
    process_response(response)
    create_doc(response.body, $2)
  when "get"
    response = self.send(:get, send($2), *args)
    process_response(response)
    create_doc(response.body, $2)
  else
    super
  end
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



7
8
9
# File 'lib/google_apps/transport.rb', line 7

def domain
  @domain
end

#exportObject (readonly)

Returns the value of attribute export.



8
9
10
# File 'lib/google_apps/transport.rb', line 8

def export
  @export
end

#groupObject (readonly)

Returns the value of attribute group.



8
9
10
# File 'lib/google_apps/transport.rb', line 8

def group
  @group
end

#migrationObject (readonly)

Returns the value of attribute migration.



8
9
10
# File 'lib/google_apps/transport.rb', line 8

def migration
  @migration
end

#nicknameObject (readonly)

Returns the value of attribute nickname.



8
9
10
# File 'lib/google_apps/transport.rb', line 8

def nickname
  @nickname
end

#pubkeyObject (readonly)

Returns the value of attribute pubkey.



8
9
10
# File 'lib/google_apps/transport.rb', line 8

def pubkey
  @pubkey
end

#requesterObject (readonly)

Returns the value of attribute requester.



8
9
10
# File 'lib/google_apps/transport.rb', line 8

def requester
  @requester
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/google_apps/transport.rb', line 7

def token
  @token
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/google_apps/transport.rb', line 8

def user
  @user
end

Instance Method Details

#add(endpoint, document, header_type = nil) ⇒ Object

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.



249
250
251
252
253
254
255
256
# File 'lib/google_apps/transport.rb', line 249

def add(endpoint, document, header_type = nil)
  header_type = :others unless header_type
  uri = URI(endpoint)
  request = requester.new :post, uri, headers(header_type)
  request.add_body document.to_s

  request.send_request
end

#add_member_to(group_id, document) ⇒ Object

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

add_member_to returns the response received from Google.



191
192
193
194
195
# File 'lib/google_apps/transport.rb', line 191

def add_member_to(group_id, document)
  response = add(group + "/#{group_id}/member", document)
  process_response(response)
  create_doc(response.body)
end

#add_owner_to(group_id, document) ⇒ Object

Parameters:

Returns:



204
205
206
# File 'lib/google_apps/transport.rb', line 204

def add_owner_to(group_id, document)
  add(group + "/#{group_id}/owner", nil, document)
end

#create_doc(response_body, type = nil) ⇒ Object



71
72
73
# File 'lib/google_apps/transport.rb', line 71

def create_doc(response_body, type = nil)
  @doc_handler.create_doc(response_body, type)
end

#delete(endpoint, id) ⇒ Object

delete is a generic target for method_missing. It is intended to handle the general case of deleting an item from your GoogleApps Domain. delete takes an API endpoint and an item identifier as argumets.

delete ‘endpoint’, ‘id’

delete returns the HTTP response received from Google.



283
284
285
286
287
288
# File 'lib/google_apps/transport.rb', line 283

def delete(endpoint, id)
  uri = URI(endpoint + "/#{id}")
  request = requester.new :delete, uri, headers(:other)

  request.send_request
end

#delete_member_from(group_id, member_id) ⇒ Object

delete_member_from removes a member from a group in the domain. It takes a group_id and member_id as arguments.

delete_member_from ‘test_group’, ‘[email protected]

delete_member_from returns the respnse received from Google.



216
217
218
# File 'lib/google_apps/transport.rb', line 216

def delete_member_from(group_id, member_id)
  delete(group + "/#{group_id}/member", member_id)
end

#delete_owner_from(group_id, owner_id) ⇒ Object

Parameters:

  • group_id (String)

    Email address of group

  • owner_id (String)

    Email address of owner to remove

Returns:



225
226
227
# File 'lib/google_apps/transport.rb', line 225

def delete_owner_from(group_id, owner_id)
  delete(group + "/#{group_id}/owner", owner_id)
end

#download(url, filename) ⇒ Object

download makes a get request of the provided url and writes the body to the provided filename.

download ‘url’, ‘save_file’



112
113
114
115
116
117
118
# File 'lib/google_apps/transport.rb', line 112

def download(url, filename)
  request = requester.new :get, URI(url), headers(:other)

  File.open(filename, "w") do |file|
    file.puts request.send_request.body
  end
end

#export_ready?(export_status_doc) ⇒ Boolean

export_ready? checks the export_status response for the presence of an apps:property element with a fileUrl name attribute.

export_ready?(export_status(‘username’, 847576))

export_ready? returns true if there is a fileUrl present in the response and false if there is no fileUrl present in the response.

Returns:

  • (Boolean)


84
85
86
# File 'lib/google_apps/transport.rb', line 84

def export_ready?(export_status_doc)
  export_file_urls(export_status_doc).any?
end

#export_status(username, req_id) ⇒ Object

export_status checks the status of a mailbox export request. It takes the username and the request_id as arguments

export_status ‘username’, 847576

export_status will return the body of the HTTP response from Google



65
66
67
68
69
# File 'lib/google_apps/transport.rb', line 65

def export_status(username, req_id)
  response = get(export + "/#{username}", req_id)
  process_response(response)
  create_doc(response.body, :export_status)
end

#fetch_export(username, req_id, filename) ⇒ Object

fetch_export downloads the mailbox export from Google. It takes a username, request id and a filename as arguments. If the export consists of more than one file the file name will have numbers appended to indicate the piece of the export.

fetch_export ‘lholcomb2’, 838382, ‘lholcomb2’

fetch_export reutrns nil in the event that the export is not yet ready.



98
99
100
101
102
103
104
105
# File 'lib/google_apps/transport.rb', line 98

def fetch_export(username, req_id, filename)
  export_status_doc = export_status(username, req_id)
  if export_ready?(export_status_doc)
    download_export(export_status_doc, filename).each_with_index { |url, index| url.gsub!(/.*/, "#{filename}#{index}")}
  else
    nil
  end
end

#get(endpoint, id = nil) ⇒ Object

get is a generic target for method_missing. It is intended to handle the general case of retrieving a record from the Google Apps Domain. It takes an API endpoint and an id as arguments.

get ‘endpoint’, ‘username’

get returns the HTTP response received from Google.



129
130
131
132
133
134
# File 'lib/google_apps/transport.rb', line 129

def get(endpoint, id = nil)
  id ? uri = URI(endpoint + build_id(id)) : uri = URI(endpoint)
  request = requester.new :get, uri, headers(:other)

  request.send_request
end

#get_groups(options = {}) ⇒ Object

get_groups retrieves all the groups from the domain

get_groups

get_groups returns the final response from Google.



160
161
162
163
164
165
166
167
# File 'lib/google_apps/transport.rb', line 160

def get_groups(options = {})
  limit = options[:limit] || 1000000
  response = get(group + "#{options[:extra]}" + "?startGroup=#{options[:start]}")
  process_response(response, :feed)
  pages = fetch_pages(response, limit, :feed)

  return_all(pages)
end

#get_members_of(group_id, options = {}) ⇒ Object

Retrieves the members of the requested group.

Parameters:

  • group_id (String)

    the Group ID in the Google Apps Environment

Returns:



175
176
177
178
# File 'lib/google_apps/transport.rb', line 175

def get_members_of(group_id, options = {})
  options[:extra] = "/#{group_id}/member"
  get_groups options
end

#get_nicknames_for(login) ⇒ Object

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



236
237
238
# File 'lib/google_apps/transport.rb', line 236

def get_nicknames_for()
  get_nickname "?username=#{}"
end

#get_users(options = {}) ⇒ Object

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.



145
146
147
148
149
150
151
152
153
# File 'lib/google_apps/transport.rb', line 145

def get_users(options = {})
  limit = options[:limit] || 1000000
  response = get(user + "?startUsername=#{options[:start]}")
  process_response(response)

  pages = fetch_pages(response, limit, :feed)

  return_all(pages)
end

#migrate(username, properties, message) ⇒ Object

migration performs mail migration from a local mail environment to GoogleApps. migrate takes a username a GoogleApps::Atom::Properties dcoument and the message as plain text (String) as arguments.

migrate ‘user’, properties, message

migrate returns the HTTP response received from Google.



298
299
300
301
302
303
# File 'lib/google_apps/transport.rb', line 298

def migrate(username, properties, message)
  request = requester.new(:post, URI(migration + "/#{username}/mail"), headers(:migration))
  request.add_body multi_part(properties.to_s, message)

  request.send_request
end

#request_export(username, document) ⇒ Object

request_export performs the GoogleApps API call to generate a mailbox export. It takes the username and an GoogleApps::Atom::Export instance as arguments

request_export ‘username’, document

request_export returns the request ID on success or the HTTP response object on failure.



46
47
48
49
50
51
52
53
54
# File 'lib/google_apps/transport.rb', line 46

def request_export(username, document)
  response = add(export + "/#{username}", document)
  process_response(response)
  export = create_doc(response.body, :export_response)

  export.find('//apps:property').inject(nil) do |request_id, node|
    node.attributes['name'] == 'requestId' ? node.attributes['value'].to_i : request_id
  end
end

#update(endpoint, target, document) ⇒ Object

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 as arguments.

update ‘endpoint’, target, document

update returns the HTTP response received from Google



267
268
269
270
271
272
273
# File 'lib/google_apps/transport.rb', line 267

def update(endpoint, target, document)
  uri = URI(endpoint + "/#{target}")
  request = requester.new :put, uri, headers(:other)
  request.add_body document.to_s

  request.send_request
end