Class: GoogleAppsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ GoogleAppsClient

Returns a new instance of GoogleAppsClient.



2
3
4
# File 'lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb', line 2

def initialize( client )
  @client = client
end

Class Method Details

.client(identity) ⇒ Object



6
7
8
9
10
# File 'lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb', line 6

def self.client( identity )
  client = Google::APIClient.new(:application_name => 'HappySeed App', :application_version => "1.0.0" )
  client.authorization.update_token!({:access_token => identity.accesstoken, :refresh_token => identity.refreshtoken})
  GoogleAppsClient.new( client )
end

Instance Method Details

#admin_apiObject



12
13
14
# File 'lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb', line 12

def admin_api
  @admin_api ||= @client.discovered_api("admin", "directory_v1")
end

#ensure_user(email, first_name, last_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb', line 32

def ensure_user( email, first_name, last_name )
  if !is_valid_user?( email )
    new_user = admin_api.users.insert.request_schema.new({
      'password' => 'happiness4u',
      'primaryEmail' => email,
      'name' => {
        'familyName' => last_name,
        'givenName' => first_name
      },
      changePasswordAtNextLogin: true
    })

    result = @client.execute(
      :api_method => admin_api.users.insert,
      :body_object => new_user
    )
  end
end

#is_valid_user?(email, domain = "happyfuncorp.com") ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
# File 'lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb', line 22

def is_valid_user?( email, domain = "happyfuncorp.com" )
  users = list_users( domain )
  users.data['users'].each do |user|
    if user['primaryEmail'].downcase == email.downcase
      return true
    end
  end
  return false
end

#list_groups(domain = "happyfuncorp.com") ⇒ Object



51
52
53
54
55
# File 'lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb', line 51

def list_groups( domain = "happyfuncorp.com" )
  request = { api_method: admin_api.groups.list }
  request[:parameters] = { domain: domain }
  @client.execute request
end

#list_members(group_key) ⇒ Object



57
58
59
60
61
# File 'lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb', line 57

def list_members( group_key )
  request = { api_method: admin_api.members.list }
  request[:parameters] = { groupKey: group_key }
  @client.execute request
end

#list_users(domain = "happyfuncorp.com") ⇒ Object



16
17
18
19
20
# File 'lib/generators/happy_seed/googleoauth/templates/app/models/google_apps_client.rb', line 16

def list_users( domain = "happyfuncorp.com" )
  request = { api_method: admin_api.users.list }
  request[:parameters] = { domain: domain }
  @client.execute request
end