lib/zold/remotes.rb in zold-0.14.8 vs lib/zold/remotes.rb in zold-0.14.9

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + # Copyright (c) 2018 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights @@ -75,11 +77,11 @@ @network = network @log = log end def http(path = '/') - Http.new("http://#{@host}:#{@port}#{path}", @score, network: @network) + Http.new(uri: "http://#{@host}:#{@port}#{path}", score: @score, network: @network) end def to_s "#{@host}:#{@port}/#{@idx}" end @@ -153,24 +155,22 @@ raise 'Host can\'t be nil' if host.nil? raise 'Host can\'t be empty' if host.empty? raise 'Port can\'t be nil' if port.nil? raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise 'Port can\'t be zero' if port.zero? - raise 'Port can\'t be negative' if port < 0 + raise 'Port can\'t be negative' if port.negative? raise 'Port can\'t be over 65536' if port > 0xffff 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 def remove(host, port = Remotes::PORT) raise 'Port has to be of type Integer' unless port.is_a?(Integer) raise 'Host can\'t be nil' if host.nil? raise 'Port can\'t be nil' if port.nil? - raise "#{host}:#{port} is absent" unless exists?(host, port) list = load list.reject! { |r| r[:host] == host.downcase && r[:port] == port } save(list) end @@ -209,29 +209,31 @@ def error(host, port = Remotes::PORT) raise 'Host can\'t be nil' if host.nil? raise 'Port can\'t be nil' if port.nil? raise 'Port has to be of type Integer' unless port.is_a?(Integer) - list = load - 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) + if_present(host, port) { |r| r[:errors] += 1 } end def rescore(host, port, score) raise 'Host can\'t be nil' if host.nil? raise 'Port can\'t be nil' if port.nil? raise 'Score can\'t be nil' if score.nil? 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 { |r| r[:host] == host.downcase && r[:port] == port }[:score] = score - save(list) + if_present(host, port) { |r| r[:score] = score } end private + def if_present(host, port) + list = load + remote = list.find { |r| r[:host] == host.downcase && r[:port] == port } + return unless remote + yield remote + save(list) + end + def load @mutex.synchronize do raw = CSV.read(file).map do |r| { host: r[0], @@ -248,10 +250,10 @@ end def save(list) @mutex.synchronize do AtomicFile.new(file).write( - list.map do |r| + list.uniq { |r| "#{r[:host]}:#{r[:port]}" }.map do |r| [ r[:host], r[:port], r[:score], r[:errors]