Class: Zold::Push

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/push.rb

Overview

Wallet pushing command

Instance Method Summary collapse

Constructor Details

#initialize(wallets:, remotes:, log: Log::Quiet.new) ⇒ Push

Returns a new instance of Push.



37
38
39
40
41
# File 'lib/zold/commands/push.rb', line 37

def initialize(wallets:, remotes:, log: Log::Quiet.new)
  @wallets = wallets
  @remotes = remotes
  @log = log
end

Instance Method Details

#push(wallet, opts) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/zold/commands/push.rb', line 61

def push(wallet, opts)
  total = 0
  @remotes.iterate(@log) do |r|
    start = Time.now
    response = r.http(
      "/wallet/#{wallet.id}#{opts['sync'] ? '?sync=true' : ''}"
    ).put(File.read(wallet.path))
    if response.code == '304'
      @log.info("#{r}: same version of #{wallet.id} there")
      next
    end
    r.assert_code(200, response)
    json = JSON.parse(response.body)['score']
    score = Score.parse_json(json)
    r.assert_valid_score(score)
    raise "Score is too weak #{score}" if score.strength < Score::STRENGTH
    @log.info("#{r} accepted #{wallet.id} in #{(Time.now - start).round(2)}s: #{Rainbow(score.value).green}")
    total += score.value
  end
  @log.info("Total score for #{wallet.id} is #{total}")
end

#run(args = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/zold/commands/push.rb', line 43

def run(args = [])
  opts = Slop.parse(args, help: true, suppress_errors: true) do |o|
    o.banner = "Usage: zold push [ID...] [options]
Available options:"
    o.bool '--sync',
      'Wait until the server confirms merge and pushes all wallets further (default: false)',
      default: false
    o.bool '--help', 'Print instructions'
  end
  mine = Args.new(opts, @log).take || return
  mine = @wallets.all if mine.empty?
  mine.each do |id|
    wallet = @wallets.find(Id.new(id))
    raise "The wallet #{id} is absent" unless wallet.exists?
    push(wallet, opts)
  end
end