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:



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.

Parameters:

  • name (String)

    name of the role to create



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.

Parameters:

  • id (Integer)

    the role id of the role to delete



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

Parameters:

  • rolename (String)

    the name of the role to find

Returns:

  • (Integer)

    the id of rolename



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

Parameters:

  • id (Integer)

    the role id

Returns:

  • (Hash)

    the role



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

Parameters:

  • res (Hash)

    result from a request to /roles

  • rolename (String)

Returns:

  • (Object)

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



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_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 }



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

Parameters:

  • id (Integer)

    the id of the role to rename

  • name (String)

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