lib/zold/remotes.rb in zold-0.11.6 vs lib/zold/remotes.rb in zold-0.11.7

- old
+ new

@@ -20,10 +20,11 @@ require 'csv' require 'uri' require 'fileutils' require_relative 'node/farm' +require_relative 'atomic_file' # The list of remotes. # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2018 Yegor Bugayenko # License:: MIT @@ -62,11 +63,11 @@ def to_s "#{@host}:#{@port}" end def assert_code(code, response) - msg = response.message + msg = response.message.strip return if response.code.to_i == code @log.debug("#{response.code} \"#{response.message}\" at \"#{response.body}\"") raise "Unexpected HTTP code #{response.code}, instead of #{code}" if msg.empty? raise "#{msg} (HTTP code #{response.code}, instead of #{code})" end @@ -111,11 +112,11 @@ def add(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise 'Port can\'t be negative' if port < 0 raise 'Port can\'t be over 65536' if port > 0xffff - raise "#{host}:#{port} alread exists" if exists?(host, port) + raise "#{host}:#{port} already exists" if exists?(host, port) list = load list << { host: host.downcase, port: port, score: 0 } list.uniq! { |r| "#{r[:host]}:#{r[:port]}" } save(list) end @@ -143,25 +144,21 @@ end end def error(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) - raise "#{host}:#{port} is absent" unless exists?(host, port) list = load - list.find do |r| - r[:host] == host.downcase && r[:port] == port - end[:errors] += 1 + raise "#{host}:#{port} is absent among #{list.count} remotes" unless exists?(host, port) + list.find { |r| r[:host] == host.downcase && r[:port] == port }[:errors] += 1 save(list) end def rescore(host, port, score) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise "#{host}:#{port} is absent" unless exists?(host, port) list = load - list.find do |r| - r[:host] == host.downcase && r[:port] == port - end[:score] = score + list.find { |r| r[:host] == host.downcase && r[:port] == port }[:score] = score save(list) end private @@ -176,11 +173,10 @@ } end end def save(list) - File.write( - file, + AtomicFile.new(file).write( list.map do |r| [ r[:host], r[:port], r[:score],