lib/zold/http.rb in zold-0.16.6 vs lib/zold/http.rb in zold-0.16.7

- old
+ new

@@ -24,19 +24,19 @@ require 'uri' require 'timeout' require 'net/http' require 'backtrace' require_relative 'version' -require_relative 'type' +require_relative 'score' # HTTP page. # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2018 Yegor Bugayenko # License:: MIT module Zold # Http page - class Http < Dry::Struct + class Http # HTTP header we add to each HTTP request, in order to inform # the other node about the score. If the score is big enough, # the remote node will add us to its list of remote nodes. SCORE_HEADER = 'X-Zold-Score' @@ -58,41 +58,37 @@ READ_TIMEOUT = 4 # Connect timeout in seconds CONNECT_TIMEOUT = 4 - # @todo #98:30m/DEV The following two statements are seen as issues by rubocop - # raising a Lint/AmbiguousBlockAssociation offense. It is somthing - # that could be solved by changing the TargetRubyVersion in .rubocop.yml - # that is already taken care of in another issue. I am leaving a todo - # to check that rubocop doesn't complain anymore, otherwise find another - # solution - attribute :uri, (Types::Class.constructor { |v| v.is_a?(URI) ? v : URI(v) }) - attribute :score, (Types::Class.constructor { |v| v.nil? ? Score::ZERO : v }) - attribute :network, Types::Strict::String.optional.default('test') + def initialize(uri:, score: Score::ZERO, network: 'test') + @uri = uri.is_a?(URI) ? uri : URI(uri) + @score = score + @network = network + end def get - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = uri.scheme == 'https' + http = Net::HTTP.new(@uri.host, @uri.port) + http.use_ssl = @uri.scheme == 'https' http.read_timeout = Http::READ_TIMEOUT http.open_timeout = Http::CONNECT_TIMEOUT - path = uri.path - path += '?' + uri.query if uri.query + path = @uri.path + path += '?' + @uri.query if @uri.query Timeout.timeout(Http::READ_TIMEOUT + Http::CONNECT_TIMEOUT) do http.request_get(path, headers) end rescue StandardError => e Error.new(e) end def put(body) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = uri.scheme == 'https' + http = Net::HTTP.new(@uri.host, @uri.port) + http.use_ssl = @uri.scheme == 'https' http.read_timeout = Http::READ_TIMEOUT http.open_timeout = Http::CONNECT_TIMEOUT - path = uri.path - path += '?' + uri.query if uri.query + path = @uri.path + path += '?' + @uri.query if @uri.query Timeout.timeout(Http::READ_TIMEOUT + Http::CONNECT_TIMEOUT) do http.request_put( path, body, headers.merge( 'Content-Type': 'text/plain', @@ -139,11 +135,11 @@ 'Connection': 'close', 'Accept-Encoding': 'gzip' } headers[Http::VERSION_HEADER] = Zold::VERSION headers[Http::PROTOCOL_HEADER] = Zold::PROTOCOL.to_s - headers[Http::NETWORK_HEADER] = network - headers[Http::SCORE_HEADER] = score.reduced(4).to_text if score.valid? && !score.expired? && score.value > 3 + headers[Http::NETWORK_HEADER] = @network + headers[Http::SCORE_HEADER] = @score.reduced(4).to_text if @score.valid? && !@score.expired? && @score.value > 3 headers end end end