Class: Zold::Fetch
- Inherits:
-
Object
- Object
- Zold::Fetch
- Defined in:
- lib/zold/commands/fetch.rb
Overview
FETCH pulling command
Instance Method Summary collapse
- #fetch(id, cps, opts) ⇒ Object
-
#initialize(remotes:, copies:, log: Log::Quiet.new) ⇒ Fetch
constructor
A new instance of Fetch.
- #run(args = []) ⇒ Object
Constructor Details
#initialize(remotes:, copies:, log: Log::Quiet.new) ⇒ Fetch
37 38 39 40 41 |
# File 'lib/zold/commands/fetch.rb', line 37 def initialize(remotes:, copies:, log: Log::Quiet.new) @remotes = remotes @copies = copies @log = log end |
Instance Method Details
#fetch(id, cps, opts) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/zold/commands/fetch.rb', line 62 def fetch(id, cps, opts) total = 0 @remotes.all.each do |r| uri = URI("#{r[:home]}wallet/#{id}") res = Http.new(uri).get unless res.code == '200' @log.error("#{r[:host]}:#{r[:port]} \ #{Rainbow(res.code).red}/#{res.message} at #{uri}") next end json = JSON.parse(res.body) score = Score.new( Time.parse(json['score']['time']), r[:host], r[:port], json['score']['suffixes'] ) unless score.valid? @log.error("#{r[:host]}:#{r[:port]} invalid score") next end if score.strength < Score::STRENGTH && !opts['ignore-score-weakness'] @log.error( "#{r[:host]}:#{r[:port]} score is too weak: #{score.strength} \ (<#{Score::STRENGTH})" ) next end total += 1 cps.add(json['body'], r[:host], r[:port], score.value) @log.info( "#{r[:host]}:#{r[:port]} #{json['body'].length}b/\ #{Rainbow(score.value).green} (v.#{json['version']})" ) end @log.debug("#{total} copies fetched, \ there are #{cps.all.count} available locally") end |
#run(args = []) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/zold/commands/fetch.rb', line 43 def run(args = []) opts = Slop.parse(args, help: true) do |o| o. = "Usage: zold fetch [ID...] [options] Available options:" o.bool '--ignore-score-weakness', 'Don\'t complain when their score is too weak', default: false o.bool '--help', 'Print instructions' end if opts.help? @log.info(opts.to_s) return end raise 'At least one wallet ID is required' if opts.arguments.empty? opts.arguments.each do |id| fetch(id, Copies.new(File.join(@copies, id)), opts) end end |