Skip to content

Commit

Permalink
Update RuboCop
Browse files Browse the repository at this point in the history
Require its specific version in `Gemfile`.

Fix it's installation in CI.

Enable new cops.

Resolve new offenses.

Drop support of Ruby 2.3
(it's not supported by RuboCop and by MRI:https://www.ruby-lang.org/en/news/2019/03/31/support-of-ruby-2-3-has-ended/)

Deprecate `Faraday::Request#method`, replace it with `#http_method`.
  • Loading branch information
AlexWayfer authored and olleolleolle committed Apr 19, 2020
1 parent 3b3de79 commit c26df87
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 29 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ jobs:

- name: Rubocop
run: |
gem install rubocop rubocop-performance --no-document
rubocop --require rubocop-performance --format progress
gem install bundler
bundle config set without 'development test'
bundle config set with 'lint'
bundle install
bundle exec rubocop --format progress
- name: Yard-Junk
run: |
Expand Down
19 changes: 18 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require:
AllCops:
DisplayCopNames: true
DisplayStyleGuide: true
TargetRubyVersion: 2.3
TargetRubyVersion: 2.4

Metrics/BlockLength:
Exclude:
Expand All @@ -18,10 +18,27 @@ Layout/LineLength:
- spec/**/*.rb
- examples/**/*.rb

Layout/SpaceAroundMethodCallOperator:
Enabled: true

Style/DoubleNegation:
Enabled: false

Style/Documentation:
Exclude:
- 'spec/**/*'
- 'examples/**/*'

Style/ExponentialNotation:
Enabled: true
Style/HashEachMethods:
Enabled: true
Style/HashTransformKeys:
Enabled: true
Style/HashTransformValues:
Enabled: true

Lint/RaiseException:
Enabled: true
Lint/StructNewOverride:
Enabled: true
10 changes: 7 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ source 'https://rubygems.org'
ruby RUBY_VERSION

gem 'jruby-openssl', '~> 0.8.8', platforms: :jruby
gem 'rake'

group :development, :test do
gem 'pry'
gem 'rake'
end

group :test do
group :lint, :development do
gem 'rubocop', '~> 0.82.0'
gem 'rubocop-performance', '~> 1.0'
end

group :test, :development do
gem 'coveralls', require: false
gem 'em-http-request', '>= 1.1', require: 'em-http'
gem 'em-synchrony', '>= 1.0.3', require: %w[em-synchrony em-synchrony/em-http]
Expand All @@ -24,7 +29,6 @@ group :test do
gem 'rack-test', '>= 0.6', require: 'rack/test'
gem 'rspec', '~> 3.7'
gem 'rspec_junit_formatter', '~> 0.4'
gem 'rubocop-performance', '~> 1.0'
gem 'simplecov'
gem 'typhoeus', '~> 1.3',
git: 'https://github.com/typhoeus/typhoeus.git',
Expand Down
5 changes: 5 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Faraday 2.0

### Others
* Rename `Faraday::Request#method` to `#http_method`.

## Faraday 1.0

### Errors
Expand Down
2 changes: 1 addition & 1 deletion faraday.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://lostisland.github.io/faraday'
spec.licenses = ['MIT']

spec.required_ruby_version = '>= 2.3'
spec.required_ruby_version = '>= 2.4'

spec.add_dependency 'multipart-post', '>= 1.2', '< 3'

Expand Down
4 changes: 2 additions & 2 deletions lib/faraday/adapter/excon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def call(env)

@app.call(env)
rescue ::Excon::Errors::SocketError => e
raise Faraday::TimeoutError, e if e.message =~ /\btimeout\b/
raise Faraday::TimeoutError, e if e.message.match?(/\btimeout\b/)

raise Faraday::SSLError, e if e.message =~ /\bcertificate\b/
raise Faraday::SSLError, e if e.message.match?(/\bcertificate\b/)

raise Faraday::ConnectionFailed, e
rescue ::Excon::Errors::Timeout => e
Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/autoload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module AutoloadHelper
#
# @return [void]
def autoload_all(prefix, options)
if prefix =~ %r{^faraday(/|$)}i
if prefix.match? %r{^faraday(/|$)}i
prefix = File.join(Faraday.root_path, prefix)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def find_default_proxy
uri = ENV['http_proxy']
return unless uri && !uri.empty?

uri = 'http://' + uri if uri !~ /^http/i
uri = 'http://' + uri unless uri.match?(/^http/i)
uri
end

Expand Down
4 changes: 2 additions & 2 deletions lib/faraday/rack_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def dup
end

# ENV Keys
# :method - a symbolized request method (:get, :post)
# :http_method - a symbolized request HTTP method (:get, :post)
# :body - the request body that will eventually be converted to a string.
# :url - URI instance for the current request.
# :status - HTTP response status code
Expand All @@ -207,7 +207,7 @@ def build_env(connection, request)
request.options.params_encoder
)

Env.new(request.method, request.body, exclusive_url,
Env.new(request.http_method, request.body, exclusive_url,
request.options, request.headers, connection.ssl,
connection.parallel_manager)
end
Expand Down
30 changes: 20 additions & 10 deletions lib/faraday/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Faraday
# req.body = 'abc'
# end
#
# @!attribute method
# @!attribute http_method
# @return [Symbol] the HTTP method of the Request
# @!attribute path
# @return [URI, String] the path
Expand All @@ -26,7 +26,9 @@ module Faraday
# @return [RequestOptions] options
#
# rubocop:disable Style/StructInheritance
class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
class Request < Struct.new(
:http_method, :path, :params, :headers, :body, :options
)
# rubocop:enable Style/StructInheritance

extend MiddlewareRegistry
Expand Down Expand Up @@ -56,6 +58,14 @@ def self.create(request_method)
end
end

def method
warn <<~TEXT
WARNING: `Faraday::Request##{__method__}` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.
`Faraday::Request##{__method__}` called from #{caller_locations(1..1).first}
TEXT
http_method
end

# Replace params, preserving the existing hash type.
#
# @param hash [Hash] new params
Expand Down Expand Up @@ -116,7 +126,7 @@ def []=(key, value)
# @return [Hash] the hash ready to be serialized in Marshal.
def marshal_dump
{
method: method,
http_method: http_method,
body: body,
headers: headers,
path: path,
Expand All @@ -129,17 +139,17 @@ def marshal_dump
# Restores the instance variables according to the +serialised+.
# @param serialised [Hash] the serialised object.
def marshal_load(serialised)
self.method = serialised[:method]
self.body = serialised[:body]
self.headers = serialised[:headers]
self.path = serialised[:path]
self.params = serialised[:params]
self.options = serialised[:options]
self.http_method = serialised[:http_method]
self.body = serialised[:body]
self.headers = serialised[:headers]
self.path = serialised[:path]
self.params = serialised[:params]
self.options = serialised[:options]
end

# @return [Env] the Env for this Request
def to_env(connection)
Env.new(method, body, connection.build_exclusive_url(path, params),
Env.new(http_method, body, connection.build_exclusive_url(path, params),
options, headers, connection.ssl, connection.parallel_manager)
end
end
Expand Down
2 changes: 1 addition & 1 deletion script/proxy-server
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (found = ARGV.index('-u'))
end

match_credentials = lambda { |credentials|
got_username, got_password = credentials.to_s.unpack('m*')[0].split(':', 2)
got_username, got_password = credentials.to_s.unpack1('m*').split(':', 2)
got_username == username && got_password == password
}

Expand Down
21 changes: 16 additions & 5 deletions spec/faraday/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@
headers: { 'Mime-Version' => '1.0' },
request: { oauth: { consumer_key: 'anonymous' } })
end
let(:method) { :get }
let(:http_method) { :get }
let(:block) { nil }

subject { conn.build_request(method, &block) }
subject { conn.build_request(http_method, &block) }

context 'when nothing particular is configured' do
it { expect(subject.method).to eq(:get) }
it { expect(subject.http_method).to eq(:get) }
it { expect(subject.to_env(conn).ssl.verify).to be_falsey }
end

context 'when method is post' do
let(:method) { :post }
context 'when HTTP method is post' do
let(:http_method) { :post }

it { expect(subject.http_method).to eq(:post) }
end

describe 'deprecate method for HTTP method' do
let(:http_method) { :post }
let(:expected_warning) do
%r{WARNING: `Faraday::Request#method` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.\n`Faraday::Request#method` called from .+/spec/faraday/request_spec.rb:\d+.}
end

it { expect(subject.method).to eq(:post) }

it { expect { subject.method }.to output(expected_warning).to_stderr }
end

context 'when setting the url on setup with a URI' do
Expand Down

0 comments on commit c26df87

Please sign in to comment.