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, _) ⇒ Object



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

def push(wallet, _)
  total = 0
  @remotes.iterate(@log) do |r|
    response = r.http("/wallet/#{wallet.id}").put(File.read(wallet.path))
    if response.code == '304'
      @log.info("#{r}: same version there")
      next
    end
    raise "#{response.code} \"#{response.message}\" at #{response.body}" unless response.code == '200'
    json = JSON.parse(response.body)['score']
    score = Score.parse_json(json)
    raise "Invalid score #{score}" unless score.valid?
    raise "Expired score #{score}" if score.expired?
    raise "Score is too weak #{score}" if score.strength < Score::STRENGTH
    @log.info("#{r} accepted: #{Rainbow(score.value).green}")
    total += score.value
  end
  @log.info("Total score is #{total}")
end

#run(args = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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 '--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