Class: SFRest::Group
- Inherits:
-
Object
- Object
- SFRest::Group
- Defined in:
- lib/sfrest/group.rb
Overview
SF Group management
Instance Method Summary collapse
-
#add_members(group_id, uids) ⇒ Hash
Add users to this group.
-
#add_sites(group_id, site_ids) ⇒ Hash
Add sites to this group.
-
#create_group(groupname) ⇒ Object
Creates a site group with specified group name.
-
#delete_group(group_id) ⇒ Object
Deletes the group with the specified id.
-
#demote_from_admins(group_id, uids) ⇒ Hash
Demote users from group admins.
-
#get_group(group_id = 0) ⇒ Hash
Gets a site group with a specified group id.
-
#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.
-
#get_members(group_id = 0) ⇒ Hash
Gets all users that are members of this group.
-
#group_data_from_results(res, groupname, key) ⇒ Object
Extract the group data for ‘key’ based on the site result object.
-
#group_list ⇒ Hash
Gets a list of all site groups.
-
#initialize(conn) ⇒ Group
constructor
A new instance of Group.
-
#promote_to_admins(group_id, uids) ⇒ Hash
Promote users to group admins.
-
#remove_members(group_id, uids) ⇒ Hash
Remove members from this group.
-
#remove_sites(group_id, site_ids) ⇒ Hash
Remove sites from this group.
-
#rename_group(group_id, groupname) ⇒ Object
Renames existing group.
-
#sites(group_id = 0) ⇒ Hash
Returns sites belonging to a specified group id.
Constructor Details
#initialize(conn) ⇒ Group
Returns a new instance of Group.
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
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
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
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
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
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.
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
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
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
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_list ⇒ Hash
Gets a list of all site groups. this will iterate through the group pages
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
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
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
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.
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.
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 |