Class: RolesManagementAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/roles-management-api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, username, api_key) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
# File 'lib/roles-management-api/client.rb', line 10

def initialize(url, username, api_key)
  @uri = URI.parse(url)

  @conn = Net::HTTP.new(@uri.host, @uri.port)
  @conn.use_ssl = true if @uri.scheme == "https"

  @username = username
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/roles-management-api/client.rb', line 5

def api_key
  @api_key
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/roles-management-api/client.rb', line 5

def url
  @url
end

#usernameObject

Returns the value of attribute username.



5
6
7
# File 'lib/roles-management-api/client.rb', line 5

def username
  @username
end

Instance Method Details

#connected?Boolean

Returns true if the API endpoint and key are valid and can connect

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/roles-management-api/client.rb', line 21

def connected?
  response = get_request("validate")

  return response.code == "200"
end

#find_entity_by_id(id) ⇒ Object

Returns a Person object for the given loginid or nil on error / not found



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/roles-management-api/client.rb', line 49

def find_entity_by_id(id)
  response = get_request("entities/" + id.to_s + ".json")

  return nil unless response.code == "200"

  json = JSON.parse(response.body, symbolize_names: true)

  if json[:type] == "Person"
    return Person.new(json)
  else
    STDERR.puts "Did not understand entity type returned by RM."
    return nil
  end
end

#find_person_by_loginid(loginid) ⇒ Object

Returns a Person object for the given loginid or nil on error / not found



65
66
67
68
69
70
71
72
73
# File 'lib/roles-management-api/client.rb', line 65

def find_person_by_loginid(loginid)
  response = get_request("people/" + loginid + ".json")

  return nil unless response.code == "200"

  json = JSON.parse(response.body, symbolize_names: true)

  return Person.new(json)
end

#find_role_by_id(role_id) ⇒ Object

Returns a Role object for the given role_id or nil on error / not found



38
39
40
41
42
43
44
45
46
# File 'lib/roles-management-api/client.rb', line 38

def find_role_by_id(role_id)
  response = get_request("roles/" + role_id.to_s + ".json")

  return nil unless response.code == "200"

  json = JSON.parse(response.body, symbolize_names: true)

  return Role.new(role_id, json)
end

#save(object) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/roles-management-api/client.rb', line 27

def save(object)
  if object.is_a? RolesManagementAPI::Role
    response = put_request("roles/" + object.id.to_s + ".json", object.as_json)
    return response
  else
    STDERR.puts "Cannot save object: type '#{object.class}' not supported."
    return false
  end
end