Class: GoogleWrapper

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

Constant Summary collapse

SCOPE =
[
  'https://www.googleapis.com/auth/admin.directory.user',
  'https://www.googleapis.com/auth/admin.directory.orgunit',
  'https://www.googleapis.com/auth/admin.directory.group'
]

Class Method Summary collapse

Class Method Details

.create_user(email, first_name, last_name, password, org_unit_path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/google_wrapper.rb', line 43

def self.create_user(email, first_name, last_name, password, org_unit_path)
  authorization, api_client = setup_client()
  directory_api = api_client.discovered_api('admin', 'directory_v1')

  new_user = directory_api.users.insert.request_schema.new( {
    :primaryEmail => email,
    :name => {
      :givenName => first_name,
      :familyName => last_name
    },
    :suspended => false,
    :password => password,
    :changePasswordAtNextLogin => true,
    :orgUnitPath => org_unit_path
  })

  created_user = api_client.execute(
    :api_method => directory_api.users.insert,
    :authorization => authorization,
    :body_object => new_user
  )
  created_user
end

.delete_group(email) ⇒ Object



37
38
39
# File 'lib/google_wrapper.rb', line 37

def self.delete_group(email)

end

.read_keyObject

private



68
69
70
71
72
73
74
75
# File 'lib/google_wrapper.rb', line 68

def self.read_key
  if (ENV['WHITEBOARD_GOOGLE_PRIVATE_KEY'])
    key = OpenSSL::PKey::RSA.new(ENV['WHITEBOARD_GOOGLE_PRIVATE_KEY'], 'notasecret')
  else
    puts 'WHITEBOARD_GOOGLE_PRIVATE_KEY is not set'
  end
  key
end

.retrieve_all_groupsObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/google_wrapper.rb', line 11

def self.retrieve_all_groups
  authorization, api_client = setup_client()
  directory_api = api_client.discovered_api('admin', 'directory_v1')
  groups = api_client.execute(
    :api_method => directory_api.groups.list,
    :authorization => authorization,
    :parameters => {
      :domain => 'west.cmu.edu'
    }
  )
  groups.data.groups
end

.retrieve_all_members(mailing_list) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/google_wrapper.rb', line 24

def self.retrieve_all_members(mailing_list)
  authorization, api_client = setup_client()
  directory_api = api_client.discovered_api('admin', 'directory_v1')
  group = api_client.execute(
    :api_method => directory_api.members.list,
    :authorization => authorization,
    :parameters => {
      :groupKey => mailing_list
    }
  )
  group.data.members
end

.setup_clientObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/google_wrapper.rb', line 77

def self.setup_client
  key = read_key()
  authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    :audience => 'https://accounts.google.com/o/oauth2/token',
    :scope => SCOPE,
    :issuer => ENV['WHITEBOARD_GOOGLE_ISSUER'], #'[email protected]'
    :sub => '[email protected]',
    :signing_key => key)

  authorization.fetch_access_token!

  api_client = Google::APIClient.new(
    :application_name => 'CMU Whiteboard',
    :application_version => '1.0.0'
  )
  return authorization, api_client
end