Class: Zold::NoDupEntrance

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

Overview

The safe entrance

Instance Method Summary collapse

Constructor Details

#initialize(entrance, wallets, log: Log::Quiet.new) ⇒ NoDupEntrance

Returns a new instance of NoDupEntrance.



35
36
37
38
39
40
41
42
# File 'lib/zold/node/nodup_entrance.rb', line 35

def initialize(entrance, wallets, log: Log::Quiet.new)
  raise 'Entrance can\'t be nil' if entrance.nil?
  @entrance = entrance
  raise 'Wallets can\'t be nil' if wallets.nil?
  @wallets = wallets
  raise 'Log can\'t be nil' if log.nil?
  @log = log
end

Instance Method Details

#push(id, body) ⇒ Object

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



53
54
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/nodup_entrance.rb', line 53

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(['', Wallet::EXTENSION]) do |f|
    File.write(f, body)
    wallet = Wallet.new(f.path)
    wallet.refurbish
    after = File.read(wallet.path)
    before = @wallets.find(id) do |w|
      w.exists? ? File.read(w.path).to_s : ''
    end
    if before == after
      @log.info(
        "Duplicate of #{id}/#{wallet.digest[0, 6]}/#{Size.new(after.length)}/#{wallet.txns.count}t ignored"
      )
      return []
    end
    @log.info(
      "New content for #{id} arrived, #{Size.new(before.length)} before and #{Size.new(after.length)} after"
    )
    @entrance.push(id, body)
  end
end

#startObject



44
45
46
# File 'lib/zold/node/nodup_entrance.rb', line 44

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

#to_jsonObject



48
49
50
# File 'lib/zold/node/nodup_entrance.rb', line 48

def to_json
  @entrance.to_json
end