class SFRest::Group

SF Group management

Public Class Methods

new(conn) click to toggle source

@param [SFRest::Connection] conn

# File lib/sfrest/group.rb, line 7
def initialize(conn)
  @conn = conn
end

Public Instance Methods

add_members(group_id, uids) click to toggle source

Add users to this group @param [Integer] group_id Id of the group @param [Array] uids of the users that need to be added @return [Hash] {'count' => count, 'uids_added' => Hash }

# 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) click to toggle source

Add sites to this group @param [Integer] group_id Id of the group @param [Array] site_ids Ids of the sites that need to be added @return [Hash] {'group_id' => 123, 'site_ids_added' => [1, 2, …]}

# 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) click to toggle source

Creates a site group with specified group name. This currently will only create a group in the root @param [String] groupname Name of the group to be created

# 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) click to toggle source

Deletes the group with the specified id @param [Integer] group_id Id of the group to fetch

# 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) click to toggle source

Demote users from group admins @param [Integer] group_id Id of the group @param [Array] uids of the users that need to be demoted @return [Hash] {'count' => count, 'uids_demoted' => Hash }

# 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) click to toggle source

Gets a site group with a specified group id. @param [Integer] group_id Id of the group to fetch @return [Hash] group object from the SF Api

# 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) click to toggle source

gets the group ID for the group named groupname will page through all the groups available searching for the group @param [String] group the name of the group @return [Integer] the id of groupname

# File lib/sfrest/group.rb, line 144
def get_group_id(groupname)
  res = group_list
  id = group_data_from_results(res, groupname, 'group_id')
  return id if id
end
get_members(group_id = 0) click to toggle source

Gets all users that are members of this group @param [Integer] group_id Id of the group to fetch @return [Hash] {'count' => count, 'members' => Hash }

# 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) click to toggle source

Extract the group data for 'key' based on the site result object @param [Hash] res result from a request to /sites @param [String] groupname @param [String] key one of the user data returned (id, site, domain…) @return [Object] Integer, String, Array, Hash depending on the site data

# File lib/sfrest/group.rb, line 155
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_list() click to toggle source

Gets a list of all site groups. @return [Hash] all the groups on the factory plus a count

{'count' => count, 'groups' => Hash }

this will iterate through the group pages

# 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) click to toggle source

Promote users to group admins @param [Integer] group_id Id of the group @param [Array] uids of the users that need to be promoted @return [Hash] {'count' => count, 'uids_promoted' => [1, 2, …] }

# 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) click to toggle source

Remove members from this group @param [Integer] group_id Id of the group @param [Array] uids of the users that need to be removed @return [Hash] {'group_id' => 123, 'uids_removed' => [1, 2, …]}

# 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) click to toggle source

Remove sites from this group @param [Integer] group_id Id of the group @param [Array] site_ids Ids of the sites that need to be removed. @return [Hash] {'group_id' => 123, 'site_ids_removed' => [1, 2, …], 'site_ids_failed' => [3, 4, …]}

# 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) click to toggle source

Renames existing group. @param [Integer] group_id Id of the group to rename. @param [String] groupname New name for the group.

# 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) click to toggle source

Returns sites belonging to a specified group id. @param [Integer] group_id Id of the group to fetch @return [Hash] {'count' => count, 'sites' => Hash }

# File lib/sfrest/group.rb, line 166
def sites(group_id = 0)
  current_path = "/api/v1/groups/#{group_id}/sites"
  @conn.get(current_path)
end