diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d899959e7..31962cf4b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2019-02-27 21:48:38 +0100 using RuboCop version 0.65.0. +# on 2019-02-28 17:29:46 +0000 using RuboCop version 0.65.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -572,61 +572,7 @@ Style/StructInheritance: - 'lib/faraday/request.rb' - 'spec/faraday/rack_builder_spec.rb' -# Offense count: 6 -# Cop supports --auto-correct. -# Configuration parameters: MinSize. -# SupportedStyles: percent, brackets -Style/SymbolArray: - EnforcedStyle: brackets - -# Offense count: 7 -# Cop supports --auto-correct. -# Configuration parameters: IgnoredMethods. -# IgnoredMethods: respond_to, define_method -Style/SymbolProc: - Exclude: - - 'lib/faraday/options.rb' - - 'lib/faraday/upload_io.rb' - - 'lib/faraday/utils/headers.rb' - - 'spec/support/helper_methods.rb' - - 'test/shared.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, AllowSafeAssignment. -# SupportedStyles: require_parentheses, require_no_parentheses, require_parentheses_when_complex -Style/TernaryParentheses: - Exclude: - - 'lib/faraday/adapter/test.rb' - - 'lib/faraday/options.rb' - - 'lib/faraday/upload_io.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: ExactNameMatch, AllowPredicates, AllowDSLWriters, IgnoreClassMethods, Whitelist. -# Whitelist: to_ary, to_a, to_c, to_enum, to_h, to_hash, to_i, to_int, to_io, to_open, to_path, to_proc, to_r, to_regexp, to_str, to_s, to_sym -Style/TrivialAccessors: - Exclude: - - 'lib/faraday/utils/headers.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -Style/UnlessElse: - Exclude: - - 'lib/faraday/adapter/em_http.rb' - - 'lib/faraday/adapter/em_http_ssl_patch.rb' - -# Offense count: 5 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle. -# SupportedStyles: forbid_for_all_comparison_operators, forbid_for_equality_operators_only, require_for_all_comparison_operators, require_for_equality_operators_only -Style/YodaCondition: - Exclude: - - 'lib/faraday/adapter/net_http.rb' - - 'script/proxy-server' - - 'test/helper.rb' - -# Offense count: 272 +# Offense count: 271 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Metrics/LineLength: diff --git a/lib/faraday/adapter/em_http.rb b/lib/faraday/adapter/em_http.rb index 085fa72a5..ad8d8083e 100644 --- a/lib/faraday/adapter/em_http.rb +++ b/lib/faraday/adapter/em_http.rb @@ -107,29 +107,27 @@ def perform_request(env) perform_single_request(env) .callback { env[:response].finish(env) } } + elsif EventMachine.reactor_running? + # EM is running: instruct upstream that this is an async request + env[:parallel_manager] = true + perform_single_request(env) + .callback { env[:response].finish(env) } + .errback { + # TODO: no way to communicate the error in async mode + raise NotImplementedError + } else - unless EventMachine.reactor_running? - error = nil - # start EM, block until request is completed - EventMachine.run do - perform_single_request(env) - .callback { EventMachine.stop } - .errback { |client| - error = error_message(client) - EventMachine.stop - } - end - raise_error(error) if error - else - # EM is running: instruct upstream that this is an async request - env[:parallel_manager] = true + error = nil + # start EM, block until request is completed + EventMachine.run do perform_single_request(env) - .callback { env[:response].finish(env) } - .errback { - # TODO: no way to communicate the error in async mode - raise NotImplementedError + .callback { EventMachine.stop } + .errback { |client| + error = error_message(client) + EventMachine.stop } end + raise_error(error) if error end rescue EventMachine::Connectify::CONNECTError => err if err.message.include?('Proxy Authentication Required') diff --git a/lib/faraday/adapter/em_http_ssl_patch.rb b/lib/faraday/adapter/em_http_ssl_patch.rb index 0339c4642..d1122302b 100644 --- a/lib/faraday/adapter/em_http_ssl_patch.rb +++ b/lib/faraday/adapter/em_http_ssl_patch.rb @@ -30,10 +30,10 @@ def ssl_verify_peer(cert_string) def ssl_handshake_completed return true unless verify_peer? - unless OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host) - raise OpenSSL::SSL::SSLError.new(%(host "#{host}" does not match the server certificate)) - else + if OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host) true + else + raise OpenSSL::SSL::SSLError.new(%(host "#{host}" does not match the server certificate)) end end diff --git a/lib/faraday/adapter/net_http.rb b/lib/faraday/adapter/net_http.rb index 84ee0cdd3..5c213b371 100644 --- a/lib/faraday/adapter/net_http.rb +++ b/lib/faraday/adapter/net_http.rb @@ -70,7 +70,7 @@ def create_request(env) request = Net::HTTPGenericRequest.new \ env[:method].to_s.upcase, # request method !!env[:body], # is there request body - :head != env[:method], # is there response body + env[:method] != :head, # is there response body env[:url].request_uri, # request uri path env[:request_headers] # request headers @@ -103,7 +103,7 @@ def perform_request(http, env) end def perform_request_with_wrapped_block(http, env, &block) - if (:get == env[:method]) && !env[:body] + if (env[:method] == :get) && !env[:body] # prefer `get` to `request` because the former handles gzip (ruby 1.9) request_via_get_method(http, env, &block) else diff --git a/lib/faraday/adapter/net_http_persistent.rb b/lib/faraday/adapter/net_http_persistent.rb index 3035d89be..7111659f8 100644 --- a/lib/faraday/adapter/net_http_persistent.rb +++ b/lib/faraday/adapter/net_http_persistent.rb @@ -10,7 +10,7 @@ class NetHttpPersistent < NetHttp def net_http_connection(env) @cached_connection ||= - if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name] + if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == %i[key name] options = { name: 'Faraday' } options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size) Net::HTTP::Persistent.new(options) diff --git a/lib/faraday/adapter/test.rb b/lib/faraday/adapter/test.rb index ae8f0672d..a59893849 100644 --- a/lib/faraday/adapter/test.rb +++ b/lib/faraday/adapter/test.rb @@ -202,7 +202,7 @@ def call(env) env[:params] = (query = env[:url].query) ? params_encoder.decode(query) : {} block_arity = stub.block.arity - status, headers, body = (block_arity >= 0) ? + status, headers, body = block_arity >= 0 ? stub.block.call(*[env, meta].take(block_arity)) : stub.block.call(env, meta) save_response(env, status, body, headers) diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 6ee5741d8..9f6624020 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -14,7 +14,7 @@ module Faraday # class Connection # A Set of allowed HTTP verbs. - METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options, :trace, :connect] + METHODS = Set.new %i[get post put delete head patch options trace connect] # @return [Hash] URI query unencoded key/value pairs. attr_reader :params diff --git a/lib/faraday/options.rb b/lib/faraday/options.rb index 5b87d7907..6240f78c8 100644 --- a/lib/faraday/options.rb +++ b/lib/faraday/options.rb @@ -52,7 +52,7 @@ def merge!(other) other.each do |key, other_value| self_value = send(key) sub_options = self.class.options_for(key) - new_value = (self_value && sub_options && other_value) ? self_value.merge(other_value) : other_value + new_value = self_value && sub_options && other_value ? self_value.merge(other_value) : other_value send("#{key}=", new_value) unless new_value.nil? end self @@ -188,7 +188,7 @@ def [](key) end def symbolized_key_set - @symbolized_key_set ||= Set.new(keys.map { |k| k.to_sym }) + @symbolized_key_set ||= Set.new(keys.map(&:to_sym)) end def self.inherited(subclass) diff --git a/lib/faraday/request/retry.rb b/lib/faraday/request/retry.rb index 38b7659c1..a9fcab109 100644 --- a/lib/faraday/request/retry.rb +++ b/lib/faraday/request/retry.rb @@ -23,7 +23,7 @@ module Faraday # interval that is random between 0.1 and 0.15. class Request::Retry < Faraday::Middleware DEFAULT_EXCEPTIONS = [Errno::ETIMEDOUT, 'Timeout::Error', Faraday::TimeoutError, Faraday::RetriableResponse].freeze - IDEMPOTENT_METHODS = [:delete, :get, :head, :options, :put] + IDEMPOTENT_METHODS = %i[delete get head options put] class Options < Faraday::Options.new(:max, :interval, :max_interval, :interval_randomness, :backoff_factor, :exceptions, :methods, :retry_if, :retry_block, diff --git a/lib/faraday/upload_io.rb b/lib/faraday/upload_io.rb index 5623432a4..932e3738b 100644 --- a/lib/faraday/upload_io.rb +++ b/lib/faraday/upload_io.rb @@ -14,7 +14,7 @@ module Faraday class CompositeReadIO def initialize(*parts) @parts = parts.flatten - @ios = @parts.map { |part| part.to_io } + @ios = @parts.map(&:to_io) @index = 0 end @@ -27,7 +27,7 @@ def length # # @return [void] def rewind - @ios.each { |io| io.rewind } + @ios.each(&:rewind) @index = 0 end @@ -49,14 +49,14 @@ def read(length = nil, outbuf = nil) end advance_io end - (!got_result && length) ? nil : outbuf + !got_result && length ? nil : outbuf end # Close each of the IOs. # # @return [void] def close - @ios.each { |io| io.close } + @ios.each(&:close) end def ensure_open_and_readable diff --git a/lib/faraday/utils/headers.rb b/lib/faraday/utils/headers.rb index de292c5dd..6ef9b4fa6 100644 --- a/lib/faraday/utils/headers.rb +++ b/lib/faraday/utils/headers.rb @@ -42,7 +42,7 @@ def initialize_copy(other) key else key.to_s.split('_') # user_agent: %w(user agent) - .each { |w| w.capitalize! } # => %w(User Agent) + .each(&:capitalize!) # => %w(User Agent) .join('-') # => "User-Agent" end keymap_mutex.synchronize { map[key] = value } @@ -125,9 +125,7 @@ def parse(header_string) protected - def names - @names - end + attr_reader :names private diff --git a/script/proxy-server b/script/proxy-server index 12165e32c..642737604 100755 --- a/script/proxy-server +++ b/script/proxy-server @@ -28,7 +28,7 @@ webrick_opts = { ProxyAuthProc: lambda { |req, res| if username type, credentials = req.header['proxy-authorization'].first.to_s.split(/\s+/, 2) - unless 'Basic' == type && match_credentials.call(credentials) + unless type == 'Basic' && match_credentials.call(credentials) res['proxy-authenticate'] = %{Basic realm="testing"} raise WEBrick::HTTPStatus::ProxyAuthenticationRequired end diff --git a/spec/support/helper_methods.rb b/spec/support/helper_methods.rb index 79ed993b2..28e196409 100644 --- a/spec/support/helper_methods.rb +++ b/spec/support/helper_methods.rb @@ -93,7 +93,7 @@ def method_with_body?(method) def big_string kb = 1024 - (32..126).map { |i| i.chr }.cycle.take(50 * kb).join + (32..126).map(&:chr).cycle.take(50 * kb).join end end end diff --git a/test/helper.rb b/test/helper.rb index d9c718b2e..417d67888 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -59,11 +59,11 @@ def capture_warnings end def self.jruby? - defined? RUBY_ENGINE && ('jruby' == RUBY_ENGINE) + defined? RUBY_ENGINE && (RUBY_ENGINE == 'jruby') end def self.rbx? - defined? RUBY_ENGINE && ('rbx' == RUBY_ENGINE) + defined? RUBY_ENGINE && (RUBY_ENGINE == 'rbx') end def self.ruby_22_plus? diff --git a/test/live_server.rb b/test/live_server.rb index be74e2436..a9ed560b3 100644 --- a/test/live_server.rb +++ b/test/live_server.rb @@ -9,7 +9,7 @@ class LiveServer < Sinatra::Base disable :logging disable :protection - [:get, :post, :put, :patch, :delete, :options].each do |method| + %i[get post put patch delete options].each do |method| send(method, '/echo') do kind = request.request_method.downcase out = kind.dup @@ -21,7 +21,7 @@ class LiveServer < Sinatra::Base end end - [:get, :post].each do |method| + %i[get post].each do |method| send(method, '/stream') do content_type :txt stream do |out| @@ -32,7 +32,7 @@ class LiveServer < Sinatra::Base end end - [:get, :post].each do |method| + %i[get post].each do |method| send(method, '/empty_stream') do content_type :txt stream do |out| diff --git a/test/shared.rb b/test/shared.rb index 5d0c26fc4..3462a4162 100644 --- a/test/shared.rb +++ b/test/shared.rb @@ -4,7 +4,7 @@ module Faraday module Shared def self.big_string kb = 1024 - (32..126).map { |i| i.chr }.cycle.take(50 * kb).join + (32..126).map(&:chr).cycle.take(50 * kb).join end def big_string