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.
7 8 9 |
# File 'lib/sfrest/role.rb', line 7 def initialize(conn) @conn = conn end |
Instance Method Details
#create_role(name) ⇒ Object
Creates a role.
82 83 84 85 86 |
# File 'lib/sfrest/role.rb', line 82 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.
99 100 101 102 |
# File 'lib/sfrest/role.rb', line 99 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sfrest/role.rb', line 45 def get_role_id(rolename) pglimit = 100 res = @conn.get("/api/v1/roles&limit=#{pglimit}") 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}?page=#{i}") 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
76 77 78 |
# File 'lib/sfrest/role.rb', line 76 def role_data(id) @conn.get("/api/v1/roles/#{id}") end |
#role_data_from_results(res, rolename) ⇒ Object
Extract the role data for rolename based on the role result object
65 66 67 68 69 70 71 |
# File 'lib/sfrest/role.rb', line 65 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
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sfrest/role.rb', line 16 def role_list page = 1 not_done = true count = 0 while not_done current_path = '/api/v1/roles?page='.dup << 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).
91 92 93 94 95 |
# File 'lib/sfrest/role.rb', line 91 def update_role(id, name) current_path = "/api/v1/roles/#{id}/update" payload = { 'new_name' => name }.to_json @conn.put(current_path, payload) end |