Class: UsersApi

Inherits:
PermitBaseApi show all
Defined in:
lib/api/users.rb

Instance Attribute Summary

Attributes inherited from PermitBaseApi

#client, #config, #logger

Instance Method Summary collapse

Methods inherited from PermitBaseApi

#lazy_load_context

Constructor Details

#initialize(client, config, logger) ⇒ UsersApi

Returns a new instance of UsersApi.



4
5
6
# File 'lib/api/users.rb', line 4

def initialize(client, config, logger)
  super
end

Instance Method Details

#get(user_key) ⇒ Object



8
9
10
11
12
# File 'lib/api/users.rb', line 8

def get(user_key)
  lazy_load_context
  data = @client.get_user(config.context.project_id, config.context.environment_id, user_key)
  data
end

#sync_user(user) ⇒ Object

Raises:

  • (KeyError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/api/users.rb', line 15

def sync_user(user)
  lazy_load_context
  raise KeyError.new("Required UserCreate or hash object") if user.is_a?(String)
  user_key = user.is_a?(Hash) ? user.delete(:key) : user.key
  raise KeyError.new("required 'key' in input dictionary") if user_key.nil?

  # check if the user key already exists
  logger.info("checking if user '#{user_key}' already exists")
  begin
    user_to_update = @client.get_user(config.context.project_id, config.context.environment_id, user_key)
  rescue OpenapiClient::ApiError => e
    if e.code == 404
      # if the user does not exist, create it
      logger.info("user does not exist, creating it...")
      user_create = user.is_a?(Hash) ? OpenapiClient::UserCreate.new(user.merge("key" => user_key)) : user
      created_user = @client.create_user(config.context.project_id, config.context.environment_id, user_create)
      return created_user
    else
      raise e
    end
  end
  if user_to_update
    # if the user exists update it
    logger.info("user exists, updating it...")
    unless user.is_a?(Hash)
      user = user.instance_variables.map { |var| [var.to_s.delete("@"), user.instance_variable_get(var)] }.to_h.except("key")
    end
    if user.empty?
      raise "empty user object, nothing to update"
    end
    user_update =  OpenapiClient::UserUpdate.new(user)
    updated_user = @client.update_user(config.context.project_id, config.context.environment_id, user_key, user_update)
    return updated_user
  end
end