Class: SFRest::Role
- Inherits:
-
Object
- Object
- SFRest::Role
- Defined in:
- lib/sfrest/role.rb
Overview
create, delete, update, roles
Instance Method Summary collapse
-
#create_role(name) ⇒ Object
Creates a role.
-
#delete_role(id) ⇒ Object
Delete a role.
-
#get_role_id(rolename) ⇒ Integer
gets the role ID for the role named rolename will page through all the roles available searching for the site this will iterate through the roles pages.
-
#initialize(conn) ⇒ Role
constructor
A new instance of Role.
-
#role_data(id) ⇒ Hash
Gets role data for a specific role id.
-
#role_data_from_results(res, rolename) ⇒ Object
Extract the role data for rolename based on the role result object.
-
#role_list ⇒ Hash
roles Gets the complete list of roles this will iterate through the roles pages.
-
#update_role(id, name) ⇒ Object
Updates a role (changes the name).
Constructor Details
#initialize(conn) ⇒ Role
Returns a new instance of Role.
5 6 7 |
# File 'lib/sfrest/role.rb', line 5 def initialize(conn) @conn = conn end |
Instance Method Details
#create_role(name) ⇒ Object
Creates a role.
80 81 82 83 84 |
# File 'lib/sfrest/role.rb', line 80 def create_role(name) current_path = '/api/v1/roles' payload = { 'name' => name }.to_json @conn.post(current_path, payload) end |
#delete_role(id) ⇒ Object
Delete a role.
97 98 99 100 |
# File 'lib/sfrest/role.rb', line 97 def delete_role(id) current_path = "/api/v1/roles/#{id}" @conn.delete(current_path) end |
#get_role_id(rolename) ⇒ Integer
gets the role ID for the role named rolename will page through all the roles available searching for the site this will iterate through the roles pages
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/sfrest/role.rb', line 43 def get_role_id(rolename) pglimit = 100 res = @conn.get('/api/v1/roles&limit=' + pglimit.to_s) rolecount = res['count'].to_i id = role_data_from_results(res, rolename) return id if id pages = (rolecount / pglimit) + 1 2.upto(pages) do |i| res = @conn.get('/api/v1/roles&limit=' + pglimit.to_s + '?page=' + i.to_s) id = role_data_from_results(res, rolename) return id if id end nil end |
#role_data(id) ⇒ Hash
Gets role data for a specific role id
74 75 76 |
# File 'lib/sfrest/role.rb', line 74 def role_data(id) @conn.get('/api/v1/roles/' + id.to_s) end |
#role_data_from_results(res, rolename) ⇒ Object
Extract the role data for rolename based on the role result object
63 64 65 66 67 68 69 |
# File 'lib/sfrest/role.rb', line 63 def role_data_from_results(res, rolename) roles = res['roles'] roles.each do |role| return role[0].to_i if role[1] == rolename end nil end |
#role_list ⇒ Hash
roles Gets the complete list of roles this will iterate through the roles pages
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sfrest/role.rb', line 14 def role_list page = 1 not_done = true count = 0 while not_done current_path = '/api/v1/roles?page=' << page.to_s res = @conn.get(current_path) if res['roles'] == [] not_done = false elsif !res['message'].nil? return { 'message' => res['message'] } elsif page == 1 count = res['count'] roles = res['roles'] else res['roles'].each do |roleid, rolename| roles[roleid] = rolename end end page += 1 end { 'count' => count, 'roles' => roles } end |
#update_role(id, name) ⇒ Object
Updates a role (changes the name).
89 90 91 92 93 |
# File 'lib/sfrest/role.rb', line 89 def update_role(id, name) current_path = "/api/v1/roles/#{id}/update" payload = { 'new_name' => name }.to_json @conn.put(current_path, payload) end |