Class: Ree::Licensing::ClientStore

Inherits:
Object
  • Object
show all
Defined in:
lib/ree/licensing/client_store.rb

Constant Summary collapse

CLIENTS_FILE =
'clients.json'

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ ClientStore

Returns a new instance of ClientStore.



12
13
14
15
# File 'lib/ree/licensing/client_store.rb', line 12

def initialize(dir)
  @dir = dir
  @file_path = File.join(dir, CLIENTS_FILE)
end

Instance Method Details

#add_license(client_id, license) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
# File 'lib/ree/licensing/client_store.rb', line 44

def add_license(client_id, license)
  data = load_data
  client = data['clients'].detect { |c| c['client_id'] == client_id }
  raise Ree::Error.new("Client #{client_id} not found") unless client

  client['licenses'] << license
  save_data(data)
end

#find_client(client_id) ⇒ Object



39
40
41
42
# File 'lib/ree/licensing/client_store.rb', line 39

def find_client(client_id)
  data = load_data
  data['clients'].detect { |c| c['client_id'] == client_id }
end

#last_license(client_id) ⇒ Object

Raises:



53
54
55
56
57
58
# File 'lib/ree/licensing/client_store.rb', line 53

def last_license(client_id)
  client = find_client(client_id)
  raise Ree::Error.new("Client #{client_id} not found") unless client

  client['licenses'].last
end

#load_dataObject



60
61
62
63
64
65
66
# File 'lib/ree/licensing/client_store.rb', line 60

def load_data
  if File.exist?(@file_path)
    JSON.parse(File.read(@file_path))
  else
    { 'clients' => [] }
  end
end

#register_client(name:, contact:, metadata: {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ree/licensing/client_store.rb', line 17

def register_client(name:, contact:, metadata: {})
  data = load_data
  client_id = "client_#{SecureRandom.hex(8)}"
  rsa_key = OpenSSL::PKey::RSA.new(4096)

  client = {
    'client_id' => client_id,
    'name' => name,
    'contact' => contact,
    'metadata' => ,
    'created_at' => Date.today.to_s,
    'private_key_pem' => rsa_key.to_pem,
    'public_key_pem' => rsa_key.public_key.to_pem,
    'licenses' => []
  }

  data['clients'] << client
  save_data(data)

  client
end