lib/zold/commands/push.rb in zold-0.4.1 vs lib/zold/commands/push.rb in zold-0.4.2

- old
+ new

@@ -17,10 +17,11 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. require 'slop' +require 'json' require 'net/http' require_relative '../log' require_relative '../id' require_relative '../http' @@ -53,15 +54,28 @@ end end def push(wallet, _) raise 'The wallet is absent' unless wallet.exists? - remote = @remotes.all[0] - uri = URI("#{remote[:home]}wallet/#{wallet.id}") - response = Http.new(uri).put(File.read(wallet.path)) - unless response.code == '200' - raise "Failed to push to #{uri}: #{response.code}/#{response.message}" + total = 0 + @remotes.all.each do |r| + uri = URI("#{r[:home]}wallet/#{wallet.id}") + response = Http.new(uri).put(File.read(wallet.path)) + if response.code != '304' && response.code != '200' + @log.error("#{uri} failed as #{response.code}/#{response.message}") + next + end + json = JSON.parse(response.body)['score'] + score = Score.new( + Time.parse(json['time']), json['host'], + json['port'], json['suffixes'] + ) + unless score.valid? + @log.error("#{uri} invalid score") + next + end + total += score.value end - @log.info("The #{wallet.id} pushed to #{uri}") + @log.info("Total score is #{total}") end end end