Class: SFRest::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrest/role.rb

Overview

create, delete, update, roles

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Role

Returns a new instance of Role.

Parameters:



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.

Parameters:

  • name (String)

    name of the role to create



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.

Parameters:

  • id (Integer)

    the role id of the role to delete



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

Parameters:

  • rolename (String)

    the name of the role to find

Returns:

  • (Integer)

    the id of rolename



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

Parameters:

  • id (Integer)

    the role id

Returns:

  • (Hash)

    the role



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

Parameters:

  • res (Hash)

    result from a request to /roles

  • rolename (String)

Returns:

  • (Object)

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



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_listHash

roles Gets the complete list of roles this will iterate through the roles pages

Returns:

  • (Hash)

    all the roles on the factory plus a count {‘count’ => count, ‘roles’ => Hash }



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).

Parameters:

  • id (Integer)

    the id of the role to rename

  • name (String)

    the new role 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