Module: CardanoUp::Session

Defined in:
lib/cardano-up/session.rb

Overview

Basic sessions management

Class Method Summary collapse

Class Method Details

.create_or_update(session_name, service_details) ⇒ Object

create or update session with details of launched service

Parameters:

  • session_name (String)

    session name

  • service_details (Hash)

    service details

Raises:

  • ArgumentError

  • CardanoUp::SessionHasNodeError

  • CardanoUp::SessionHasWalletError



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cardano-up/session.rb', line 42

def self.create_or_update(session_name, service_details)
  raise ArgumentError, 'service_details should be Hash' unless service_details.is_a?(Hash)
  raise ArgumentError, 'service_details shoud have :network' if service_details[:network].nil?

  service_network = service_details[:network].to_sym || :unknown

  if !exists?(session_name) || empty?(session_name)
    # if session is not set then just create new with just service_details
    content = { service_network => service_details }
    put_content(session_name, content)
  elsif !network?(session_name, service_network)
    # if session exists but has no network, add new network
    existing_services = get(session_name)
    new_service = { service_network => service_details }
    put_content(session_name, existing_services.merge(new_service))
  else
    # if session has network and node already, you can add only wallet
    if service_details.key?(:node) && node?(session_name, service_network)
      raise CardanoUp::SessionHasNodeError.new(session_name, service_network)
    end

    # if session has network and wallet already, you can add only node
    if service_details.key?(:wallet) && wallet?(session_name, service_network)
      raise CardanoUp::SessionHasWalletError.new(session_name,
                                                 service_network)
    end

    existing_services = get(session_name)
    updated_network = existing_services[service_network].merge(service_details)
    existing_services[service_network] = updated_network
    put_content(session_name, existing_services)
  end
end

.destroy!(session_name) ⇒ Object



21
22
23
24
# File 'lib/cardano-up/session.rb', line 21

def self.destroy!(session_name)
  path = session_file_path(session_name)
  FileUtils.rm_f(path)
end

.destroy_all!Object



26
27
28
29
30
# File 'lib/cardano-up/session.rb', line 26

def self.destroy_all!
  list_all.each do |path|
    FileUtils.rm_f(path)
  end
end

.exists?(session_name) ⇒ Boolean

Check session file exists

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/cardano-up/session.rb', line 7

def self.exists?(session_name)
  CardanoUp.configure_default unless CardanoUp.configured?
  File.exist?(session_file_path(session_name))
end

.get(session_name) ⇒ Object

get session contents



13
14
15
16
17
18
19
# File 'lib/cardano-up/session.rb', line 13

def self.get(session_name)
  raise CardanoUp::SessionNotExistsError, session_name unless exists?(session_name)

  CardanoUp.configure_default unless CardanoUp.configured?
  session = session_file_path(session_name)
  CardanoUp::Utils.from_json(session)
end

.list_allObject



32
33
34
# File 'lib/cardano-up/session.rb', line 32

def self.list_all
  Dir[File.join(CardanoUp.base_dir, '.session*')]
end

.network_or_raise?(session_name, env) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



88
89
90
91
92
# File 'lib/cardano-up/session.rb', line 88

def self.network_or_raise?(session_name, env)
  raise CardanoUp::SessionEnvNotUpError.new(session_name, env) unless network?(session_name, env)

  true
end

.node_or_raise?(session_name, env) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



76
77
78
79
80
# File 'lib/cardano-up/session.rb', line 76

def self.node_or_raise?(session_name, env)
  raise CardanoUp::SessionServiceNotUpError.new(session_name, env, :node) unless node?(session_name, env)

  true
end

.remove(session_name, service_details) ⇒ Object

remove entry from session

Parameters:

  • session_name (String)

    session name

  • service_details (Hash)

    service details ({ network: ‘preprod’, service: ‘wallet’ })



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cardano-up/session.rb', line 97

def self.remove(session_name, service_details)
  existing_services = get(session_name)
  service = service_details[:service].to_sym
  network = service_details[:network].to_sym
  if existing_services && existing_services[network]
    existing_services[network].delete(service)

    unless existing_services[network].key?(:node) || existing_services[network].key?(:wallet)
      existing_services.delete(network)
    end
    put_content(session_name, existing_services)
  end

  destroy!(session_name) if empty?(session_name)
end

.wallet_or_raise?(session_name, env) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



82
83
84
85
86
# File 'lib/cardano-up/session.rb', line 82

def self.wallet_or_raise?(session_name, env)
  raise CardanoUp::SessionServiceNotUpError.new(session_name, env, :wallet) unless wallet?(session_name, env)

  true
end