Class: Zold::SafeEntrance

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/node/safe_entrance.rb

Overview

The safe entrance

Instance Method Summary collapse

Constructor Details

#initialize(entrance, network: 'test') ⇒ SafeEntrance

Returns a new instance of SafeEntrance.


39
40
41
42
43
44
# File 'lib/zold/node/safe_entrance.rb', line 39

def initialize(entrance, network: 'test')
  raise 'Entrance can\'t be nil' if entrance.nil?
  @entrance = entrance
  raise 'Network can\'t be nil' if network.nil?
  @network = network
end

Instance Method Details

#push(id, body) ⇒ Object

Returns a list of modifed wallets (as Zold::Id)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zold/node/safe_entrance.rb', line 55

def push(id, body)
  raise 'Id can\'t be nil' if id.nil?
  raise 'Id must be of type Id' unless id.is_a?(Id)
  raise 'Body can\'t be nil' if body.nil?
  Tempfile.open do |f|
    File.write(f.path, body)
    wallet = Wallet.new(f)
    unless wallet.network == @network
      raise "The network name mismatch, the wallet is in '#{wallet.network}', we are in '#{@network}'"
    end
    balance = wallet.balance
    if balance.negative? && !wallet.root?
      raise "The balance #{balance} of #{wallet.id} is negative and it's not a root wallet"
    end
    Emission.new(wallet).check
    tax = Tax.new(wallet)
    if tax.in_debt?
      raise "Taxes are not paid, can't accept the wallet; the debt is #{tax.debt} (#{tax.debt.to_i} zents)"
    end
    @entrance.push(id, body)
  end
end

#startObject


46
47
48
# File 'lib/zold/node/safe_entrance.rb', line 46

def start
  @entrance.start { yield(self) }
end

#to_jsonObject


50
51
52
# File 'lib/zold/node/safe_entrance.rb', line 50

def to_json
  @entrance.to_json
end