Class: GoogleAccount

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

Defined Under Namespace

Classes: GoogleAppsAPIError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email) ⇒ GoogleAccount


6
7
8
# File 'lib/google_account.rb', line 6

def initialize(email)
  @email = email
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email


2
3
4
# File 'lib/google_account.rb', line 2

def email
  @email
end

Class Method Details

.full_email(email) ⇒ Object


100
101
102
103
104
105
106
# File 'lib/google_account.rb', line 100

def self.full_email(email)
  if email.include? '@'
    email
  else
    "#{email}@#{Settings.google.domain}"
  end
end

.group_to_email(group_name) ⇒ Object


108
109
110
# File 'lib/google_account.rb', line 108

def self.group_to_email(group_name)
  full_email(group_name.to_s.downcase.gsub /[^a-z0-9]+/, '.')
end

.random_passwordObject


112
113
114
# File 'lib/google_account.rb', line 112

def self.random_password
  rand(36**rand(16..42)).to_s(36)
end

Instance Method Details

#available?Boolean


22
23
24
# File 'lib/google_account.rb', line 22

def available?
  !exists?
end

#create!(first_name, last_name, department, title, privacy) ⇒ Object

Raises:


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/google_account.rb', line 36

def create!(first_name, last_name, department, title, privacy)
  params = {
    primaryEmail: full_email,
    password: GoogleAccount.random_password,
    name: {
      familyName: last_name,
      givenName: first_name
    },
    organizations: [
      department: department,
      title: title
    ],
    includeInGlobalAddressList: !privacy
  }

  new_user = directory.users.insert.request_schema.new(params)

  result = api.execute api_method: directory.users.insert, body_object: new_user
  raise GoogleAppsAPIError, result.data['error']['message'] unless result.success?

  true
end

#create_or_update!(first_name, last_name, department, title, privacy) ⇒ Object


26
27
28
29
30
31
32
33
34
# File 'lib/google_account.rb', line 26

def create_or_update!(first_name, last_name, department, title, privacy)
  if exists?
    update! first_name, last_name, department, title, privacy
    :update
  else
    create! first_name, last_name, department, title, privacy
    :create
  end
end

#exists?Boolean


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/google_account.rb', line 10

def exists?
  result = api.execute(
    api_method: directory.users.get,
    # This will find by primary email or aliases according to Google's documentation
    parameters: {userKey: full_email}
  )

  return false unless result.success?

  result.data.emails.map{|e| e['address']}.include? full_email
end

#full_emailObject


96
97
98
# File 'lib/google_account.rb', line 96

def full_email
  GoogleAccount.full_email(email)
end

#join!(group, role = 'MEMBER') ⇒ Object

Raises:


80
81
82
83
84
85
86
87
88
# File 'lib/google_account.rb', line 80

def join!(group, role = 'MEMBER')
  group = GoogleAccount.group_to_email(group)
  params = {email: full_email, role: role}

  new_member = directory.members.insert.request_schema.new(params)

  result = api.execute api_method: directory.members.insert, parameters: {groupKey: group}, body_object: new_member
  raise GoogleAppsAPIError, result.data['error']['message'] unless result.success?
end

#leave!(group) ⇒ Object

Raises:


90
91
92
93
94
# File 'lib/google_account.rb', line 90

def leave!(group)
  group = GoogleAccount.group_to_email(group)
  result = api.execute api_method: directory.members.delete, parameters: {groupKey: group, memberKey: full_email}
  raise GoogleAppsAPIError, result.data['error']['message'] unless result.success?
end

#update!(first_name, last_name, department, title, privacy) ⇒ Object

Raises:


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/google_account.rb', line 59

def update!(first_name, last_name, department, title, privacy)
  params = {
    name: {
      givenName: first_name,
      familyName: last_name
    },
    organizations: [
      department: department,
      title: title
    ],
    includeInGlobalAddressList: !privacy
  }

  user_updates = directory.users.update.request_schema.new(params)

  result = api.execute api_method: directory.users.update, parameters: {userKey: full_email}, body_object: user_updates
  raise GoogleAppsAPIError, result.data['error']['message'] unless result.success?

  true
end