Class: SFRest::Group

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

Overview

SF Group management

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Group

Returns a new instance of Group.

Parameters:



5
6
7
# File 'lib/sfrest/group.rb', line 5

def initialize(conn)
  @conn = conn
end

Instance Method Details

#add_members(group_id, uids) ⇒ Hash

Add users to this group

Parameters:

  • group_id (Integer)

    Id of the group

  • uids (Array)

    of the users that need to be added

Returns:

  • (Hash)

    {‘count’ => count, ‘uids_added’ => Hash }



54
55
56
57
58
# File 'lib/sfrest/group.rb', line 54

def add_members(group_id, uids)
  current_path = '/api/v1/groups/' + group_id.to_s + '/members'
  payload = { 'uids' => uids.map(&:to_i) }.to_json
  @conn.post(current_path, payload)
end

#add_sites(group_id, site_ids) ⇒ Hash

Add sites to this group

Parameters:

  • group_id (Integer)

    Id of the group

  • site_ids (Array)

    Ids of the sites that need to be added

Returns:

  • (Hash)

    => 123, ‘site_ids_added’ => [1, 2, …]



94
95
96
97
98
# File 'lib/sfrest/group.rb', line 94

def add_sites(group_id, site_ids)
  current_path = '/api/v1/groups/' + group_id.to_s + '/sites'
  payload = { 'site_ids' => site_ids }.to_json
  @conn.post(current_path, payload)
end

#create_group(groupname) ⇒ Object

Creates a site group with specified group name. This currently will only create a group in the root

Parameters:

  • groupname (String)

    Name of the group to be created



12
13
14
15
16
# File 'lib/sfrest/group.rb', line 12

def create_group(groupname)
  current_path = '/api/v1/groups'
  payload = { 'group_name' => groupname }.to_json
  @conn.post(current_path, payload)
end

#delete_group(group_id) ⇒ Object

Deletes the group with the specified id

Parameters:

  • group_id (Integer)

    Id of the group to fetch



20
21
22
23
# File 'lib/sfrest/group.rb', line 20

def delete_group(group_id)
  current_path = '/api/v1/groups/' << group_id.to_s
  @conn.delete(current_path)
end

#demote_from_admins(group_id, uids) ⇒ Hash

Demote users from group admins

Parameters:

  • group_id (Integer)

    Id of the group

  • uids (Array)

    of the users that need to be demoted

Returns:

  • (Hash)

    {‘count’ => count, ‘uids_demoted’ => Hash }



84
85
86
87
88
# File 'lib/sfrest/group.rb', line 84

def demote_from_admins(group_id, uids)
  current_path = '/api/v1/groups/' + group_id.to_s + '/admins'
  payload = { 'uids' => uids.map(&:to_i) }.to_json
  @conn.delete(current_path, payload)
end

#get_group(group_id = 0) ⇒ Hash

Gets a site group with a specified group id.

Parameters:

  • group_id (Integer) (defaults to: 0)

    Id of the group to fetch

Returns:

  • (Hash)

    group object from the SF Api



37
38
39
40
# File 'lib/sfrest/group.rb', line 37

def get_group(group_id = 0)
  current_path = '/api/v1/groups/' << group_id.to_s
  @conn.get(current_path)
end

#get_members(group_id = 0) ⇒ Hash

Gets all users that are members of this group

Parameters:

  • group_id (Integer) (defaults to: 0)

    Id of the group to fetch

Returns:

  • (Hash)

    {‘count’ => count, ‘members’ => Hash }



45
46
47
48
# File 'lib/sfrest/group.rb', line 45

def get_members(group_id = 0)
  current_path = '/api/v1/groups/' + group_id.to_s + '/members'
  @conn.get(current_path)
end

#group_listHash

Gets a list of all site groups. this will iterate through the group pages

Returns:

  • (Hash)

    all the groups on the factory plus a count {‘count’ => count, ‘groups’ => Hash }



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sfrest/group.rb', line 114

def group_list
  page = 1
  not_done = true
  count = 0
  while not_done
    current_path = '/api/v1/groups?page=' << page.to_s
    res = @conn.get(current_path)
    if res['groups'] == []
      not_done = false
    elsif !res['message'].nil?
      return { 'message' => res['message'] }
    elsif page == 1
      count = res['count']
      groups = res['groups']
    else
      res['groups'].each do |group|
        groups << group
      end
    end
    page += 1
  end
  { 'count' => count, 'groups' => groups }
end

#promote_to_admins(group_id, uids) ⇒ Hash

Promote users to group admins

Parameters:

  • group_id (Integer)

    Id of the group

  • uids (Array)

    of the users that need to be promoted

Returns:

  • (Hash)

    {‘count’ => count, ‘uids_promoted’ => [1, 2, …] }



74
75
76
77
78
# File 'lib/sfrest/group.rb', line 74

def promote_to_admins(group_id, uids)
  current_path = '/api/v1/groups/' + group_id.to_s + '/admins'
  payload = { 'uids' => uids.map(&:to_i) }.to_json
  @conn.post(current_path, payload)
end

#remove_members(group_id, uids) ⇒ Hash

Remove members from this group

Parameters:

  • group_id (Integer)

    Id of the group

  • uids (Array)

    of the users that need to be removed

Returns:

  • (Hash)

    => 123, ‘uids_removed’ => [1, 2, …]



64
65
66
67
68
# File 'lib/sfrest/group.rb', line 64

def remove_members(group_id, uids)
  current_path = '/api/v1/groups/' + group_id.to_s + '/members'
  payload = { 'uids' => uids.map(&:to_i) }.to_json
  @conn.delete(current_path, payload)
end

#remove_sites(group_id, site_ids) ⇒ Hash

Remove sites from this group

Parameters:

  • group_id (Integer)

    Id of the group

  • site_ids (Array)

    Ids of the sites that need to be removed.

Returns:

  • (Hash)

    => 123, ‘site_ids_removed’ => [1, 2, …], ‘site_ids_failed’ => [3, 4, …]



104
105
106
107
108
# File 'lib/sfrest/group.rb', line 104

def remove_sites(group_id, site_ids)
  current_path = '/api/v1/groups/' + group_id.to_s + '/sites'
  payload = { 'site_ids' => site_ids }.to_json
  @conn.delete(current_path, payload)
end

#rename_group(group_id, groupname) ⇒ Object

Renames existing group.

Parameters:

  • group_id (Integer)

    Id of the group to rename.

  • groupname (String)

    New name for the group.



28
29
30
31
32
# File 'lib/sfrest/group.rb', line 28

def rename_group(group_id, groupname)
  current_path = '/api/v1/groups/' + group_id.to_s + '/update'
  payload = { 'group_name' => groupname }.to_json
  @conn.put(current_path, payload)
end