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:



7
8
9
# File 'lib/sfrest/group.rb', line 7

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 }



56
57
58
59
60
# File 'lib/sfrest/group.rb', line 56

def add_members(group_id, uids)
  current_path = "/api/v1/groups/#{group_id}/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, …]



96
97
98
99
100
# File 'lib/sfrest/group.rb', line 96

def add_sites(group_id, site_ids)
  current_path = "/api/v1/groups/#{group_id}/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



14
15
16
17
18
# File 'lib/sfrest/group.rb', line 14

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



22
23
24
25
# File 'lib/sfrest/group.rb', line 22

def delete_group(group_id)
  current_path = "/api/v1/groups/#{group_id}"
  @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 }



86
87
88
89
90
# File 'lib/sfrest/group.rb', line 86

def demote_from_admins(group_id, uids)
  current_path = "/api/v1/groups/#{group_id}/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



39
40
41
42
# File 'lib/sfrest/group.rb', line 39

def get_group(group_id = 0)
  current_path = "/api/v1/groups/#{group_id}"
  @conn.get(current_path)
end

#get_group_id(groupname) ⇒ Integer

gets the group ID for the group named groupname will page through all the groups available searching for the group

Parameters:

  • group (String)

    the name of the group

Returns:

  • (Integer)

    the id of groupname



144
145
146
147
# File 'lib/sfrest/group.rb', line 144

def get_group_id(groupname)
  res = group_list
  group_data_from_results(res, groupname, 'group_id')
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 }



47
48
49
50
# File 'lib/sfrest/group.rb', line 47

def get_members(group_id = 0)
  current_path = "/api/v1/groups/#{group_id}/members"
  @conn.get(current_path)
end

#group_data_from_results(res, groupname, key) ⇒ Object

Extract the group data for ‘key’ based on the site result object

Parameters:

  • res (Hash)

    result from a request to /sites

  • groupname (String)
  • key (String)

    one of the user data returned (id, site, domain…)

Returns:

  • (Object)

    Integer, String, Array, Hash depending on the site data



154
155
156
157
158
159
160
# File 'lib/sfrest/group.rb', line 154

def group_data_from_results(res, groupname, key)
  groups = res['groups']
  groups.each do |group|
    return group[key] if group['group_name'] == groupname
  end
  nil
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 }



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

def group_list
  page = 1
  not_done = true
  count = 0
  while not_done
    current_path = '/api/v1/groups?page='.dup << 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, …] }



76
77
78
79
80
# File 'lib/sfrest/group.rb', line 76

def promote_to_admins(group_id, uids)
  current_path = "/api/v1/groups/#{group_id}/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, …]



66
67
68
69
70
# File 'lib/sfrest/group.rb', line 66

def remove_members(group_id, uids)
  current_path = "/api/v1/groups/#{group_id}/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, …]



106
107
108
109
110
# File 'lib/sfrest/group.rb', line 106

def remove_sites(group_id, site_ids)
  current_path = "/api/v1/groups/#{group_id}/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.



30
31
32
33
34
# File 'lib/sfrest/group.rb', line 30

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

#sites(group_id = 0) ⇒ Hash

Returns sites belonging to a specified group id.

Parameters:

  • group_id (Integer) (defaults to: 0)

    Id of the group to fetch

Returns:

  • (Hash)

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



165
166
167
168
# File 'lib/sfrest/group.rb', line 165

def sites(group_id = 0)
  current_path = "/api/v1/groups/#{group_id}/sites"
  @conn.get(current_path)
end