Skip to content

Commit

Permalink
chore: RuboCop lint Style/IfUnlessModifier (#926)
Browse files Browse the repository at this point in the history
* Fixes Rubocop Style/IfUnlessModifier

* Update lib/faraday/adapter/httpclient.rb

Co-Authored-By: iMacTia <iMacTia@users.noreply.github.com>

* Update lib/faraday/request/retry.rb

Co-Authored-By: iMacTia <iMacTia@users.noreply.github.com>
  • Loading branch information
iMacTia authored and olleolleolle committed Mar 4, 2019
1 parent 8ba22d5 commit ef40442
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 99 deletions.
35 changes: 5 additions & 30 deletions .rubocop_todo.yml
Expand Up @@ -14,27 +14,22 @@ Metrics/AbcSize:
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 31
Max: 27

# Offense count: 5
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 216
Max: 212

# Offense count: 18
Metrics/CyclomaticComplexity:
Max: 16

# Offense count: 46
# Offense count: 44
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 65

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 101

# Offense count: 16
Metrics/PerceivedComplexity:
Max: 18
Expand Down Expand Up @@ -71,7 +66,7 @@ Style/GlobalVars:
Exclude:
- 'script/generate_certs'

# Offense count: 26
# Offense count: 21
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
Expand All @@ -87,26 +82,6 @@ Style/GuardClause:
- 'lib/faraday/request/url_encoded.rb'
- 'lib/faraday/utils/headers.rb'

# Offense count: 23
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'lib/faraday.rb'
- 'lib/faraday/adapter/em_http.rb'
- 'lib/faraday/adapter/httpclient.rb'
- 'lib/faraday/adapter/net_http_persistent.rb'
- 'lib/faraday/autoload.rb'
- 'lib/faraday/connection.rb'
- 'lib/faraday/encoders/flat_params_encoder.rb'
- 'lib/faraday/encoders/nested_params_encoder.rb'
- 'lib/faraday/error.rb'
- 'lib/faraday/options/env.rb'
- 'lib/faraday/rack_builder.rb'
- 'lib/faraday/request/authorization.rb'
- 'lib/faraday/request/retry.rb'
- 'lib/faraday/utils/params_hash.rb'
- 'test/live_server.rb'

# Offense count: 1
Style/MethodMissingSuper:
Exclude:
Expand All @@ -122,7 +97,7 @@ Style/MultipleComparison:
Exclude:
- 'lib/faraday/encoders/flat_params_encoder.rb'

# Offense count: 276
# Offense count: 287
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand Down
4 changes: 1 addition & 3 deletions lib/faraday.rb
Expand Up @@ -154,7 +154,5 @@ def self.default_connection_options=(options)
require_libs 'utils', 'options', 'connection', 'rack_builder', 'parameters',
'middleware', 'adapter', 'request', 'response', 'upload_io', 'error'

unless ENV['FARADAY_NO_AUTOLOAD']
require_lib 'autoload'
end
require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
end
12 changes: 3 additions & 9 deletions lib/faraday/adapter/em_http.rb
Expand Up @@ -73,9 +73,7 @@ def configure_timeout(options, env)

# Reads out compression header settings from env into options
def configure_compression(options, env)
if (env[:method] == :get) && !options[:head].key?('accept-encoding')
options[:head]['accept-encoding'] = 'gzip, compressed'
end
options[:head]['accept-encoding'] = 'gzip, compressed' if (env[:method] == :get) && !options[:head].key?('accept-encoding')
end

def request_options(env)
Expand Down Expand Up @@ -227,9 +225,7 @@ def run
perform_request(&proc)
end
end
unless @errors.empty?
raise Faraday::ClientError, @errors.first || 'connection failed'
end
raise Faraday::ClientError, @errors.first || 'connection failed' unless @errors.empty?
end
ensure
reset
Expand All @@ -248,9 +244,7 @@ def perform_request
end

def check_finished
if @num_succeeded + @errors.size == @num_registered
EventMachine.stop
end
EventMachine.stop if @num_succeeded + @errors.size == @num_registered
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/faraday/adapter/httpclient.rb
Expand Up @@ -79,9 +79,7 @@ def configure_socket(bind)
# @param proxy [Hash]
def configure_proxy(proxy)
client.proxy = proxy[:uri]
if proxy[:user] && proxy[:password]
client.set_proxy_auth proxy[:user], proxy[:password]
end
client.set_proxy_auth(proxy[:user], proxy[:password]) if proxy[:user] && proxy[:password]
end

# @param ssl [Hash]
Expand Down
4 changes: 1 addition & 3 deletions lib/faraday/adapter/net_http_persistent.rb
Expand Up @@ -64,9 +64,7 @@ def configure_ssl(http, ssl)
end

def http_set(http, attr, value)
if http.send(attr) != value
http.send("#{attr}=", value)
end
http.send("#{attr}=", value) if http.send(attr) != value
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/faraday/autoload.rb
Expand Up @@ -24,9 +24,7 @@ module AutoloadHelper
#
# @return [void]
def autoload_all(prefix, options)
if prefix =~ %r{^faraday(/|$)}i
prefix = File.join(Faraday.root_path, prefix)
end
prefix = File.join(Faraday.root_path, prefix) if prefix =~ %r{^faraday(/|$)}i
options.each do |const_name, path|
autoload const_name, File.join(prefix, path)
end
Expand Down
8 changes: 2 additions & 6 deletions lib/faraday/connection.rb
Expand Up @@ -463,9 +463,7 @@ def build_url(url = nil, extra_params = nil)
#
# @return [Faraday::Response]
def run_request(method, url, body, headers)
unless METHODS.include?(method)
raise ArgumentError, "unknown http method: #{method}"
end
raise ArgumentError, "unknown http method: #{method}" unless METHODS.include?(method)

# Resets temp_proxy
@temp_proxy = proxy_for_request(url)
Expand Down Expand Up @@ -539,9 +537,7 @@ def dup
# @return [void]
# @api private
def with_uri_credentials(uri)
if uri.user && uri.password
yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
end
yield(Utils.unescape(uri.user), Utils.unescape(uri.password)) if uri.user && uri.password
end

def set_authorization_header(header_type, *args)
Expand Down
4 changes: 1 addition & 3 deletions lib/faraday/encoders/flat_params_encoder.rb
Expand Up @@ -56,9 +56,7 @@ def self.decode(query)
split_query.each_with_object(empty_accumulator.dup) do |pair, accu|
pair[0] = unescape(pair[0])
pair[1] = true if pair[1].nil?
if pair[1].respond_to?(:to_str)
pair[1] = unescape(pair[1].to_str.tr('+', ' '))
end
pair[1] = unescape(pair[1].to_str.tr('+', ' ')) if pair[1].respond_to?(:to_str)
if accu[pair[0]].is_a?(Array)
accu[pair[0]] << pair[1]
elsif accu[pair[0]]
Expand Down
8 changes: 2 additions & 6 deletions lib/faraday/encoders/nested_params_encoder.rb
Expand Up @@ -98,17 +98,13 @@ def self.decode(query)

if !last_subkey || is_array
value_type = is_array ? Array : Hash
if context[subkey] && !context[subkey].is_a?(value_type)
raise TypeError, format("expected #{value_type.name} (got #{context[subkey].class.name}) for param `#{subkey}'")
end
raise TypeError, format("expected #{value_type.name} (got #{context[subkey].class.name}) for param `#{subkey}'") if context[subkey] && !context[subkey].is_a?(value_type)

context = (context[subkey] ||= value_type.new)
end

if context.is_a?(Array) && !is_array
if !context.last.is_a?(Hash) || context.last.key?(subkey)
context << {}
end
context << {} if !context.last.is_a?(Hash) || context.last.key?(subkey)
context = context.last
end

Expand Down
12 changes: 3 additions & 9 deletions lib/faraday/error.rb
Expand Up @@ -30,15 +30,9 @@ def backtrace

def inspect
inner = +''
if @wrapped_exception
inner << " wrapped=#{@wrapped_exception.inspect}"
end
if @response
inner << " response=#{@response.inspect}"
end
if inner.empty?
inner << " #{super}"
end
inner << " wrapped=#{@wrapped_exception.inspect}" if @wrapped_exception
inner << " response=#{@response.inspect}" if @response
inner << " #{super}" if inner.empty?
%(#<#{self.class}#{inner}>)
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/faraday/options/env.rb
Expand Up @@ -72,9 +72,7 @@ class Env < Options.new(:method, :request_body, :url, :request, :request_headers
# @return [Env] from given value
def self.from(value)
env = super(value)
if value.respond_to?(:custom_members)
env.custom_members.update(value.custom_members)
end
env.custom_members.update(value.custom_members) if value.respond_to?(:custom_members)
env
end

Expand Down Expand Up @@ -149,9 +147,7 @@ def inspect
attrs << "@#{mem}=#{value.inspect}"
end
end
unless custom_members.empty?
attrs << "@custom=#{custom_members.inspect}"
end
attrs << "@custom=#{custom_members.inspect}" unless custom_members.empty?
%(#<#{self.class}#{attrs.join(' ')}>)
end

Expand Down
8 changes: 2 additions & 6 deletions lib/faraday/rack_builder.rb
Expand Up @@ -30,9 +30,7 @@ class Handler

def initialize(klass, *args, &block)
@name = klass.to_s
if klass.respond_to?(:name)
@@constants_mutex.synchronize { @@constants[@name] = klass }
end
@@constants_mutex.synchronize { @@constants[@name] = klass } if klass.respond_to?(:name)
@args = args
@block = block
end
Expand Down Expand Up @@ -113,9 +111,7 @@ def response(key, *args, &block)
def adapter(klass = NO_ARGUMENT, *args, &block)
return @adapter if klass == NO_ARGUMENT

if klass.is_a?(Symbol)
klass = Faraday::Adapter.lookup_middleware(klass)
end
klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
@adapter = self.class::Handler.new(klass, *args, &block)
end

Expand Down
4 changes: 1 addition & 3 deletions lib/faraday/request/authorization.rb
Expand Up @@ -43,9 +43,7 @@ def initialize(app, type, token)

# @param env [Faraday::Env]
def call(env)
unless env.request_headers[KEY]
env.request_headers[KEY] = @header_value
end
env.request_headers[KEY] = @header_value unless env.request_headers[KEY]
@app.call(env)
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/faraday/request/retry.rb
Expand Up @@ -179,9 +179,7 @@ def rewind_files(body)
return unless body.is_a?(Hash)

body.each do |_, value|
if value.is_a? UploadIO
value.rewind
end
value.rewind if value.is_a?(UploadIO)
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/faraday/utils/params_hash.rb
Expand Up @@ -42,9 +42,7 @@ def replace(other)
end

def merge_query(query, encoder = nil)
if query && !query.empty?
update((encoder || Utils.default_params_encoder).decode(query))
end
update((encoder || Utils.default_params_encoder).decode(query)) if query && !query.empty?
self
end

Expand Down
4 changes: 1 addition & 3 deletions test/live_server.rb
Expand Up @@ -81,6 +81,4 @@ class LiveServer < Sinatra::Base
end
end

if $PROGRAM_NAME == __FILE__
Faraday::LiveServer.run!
end
Faraday::LiveServer.run! if $PROGRAM_NAME == __FILE__

0 comments on commit ef40442

Please sign in to comment.