From 29f4b587c7a068de5f5d3fd3a0afcc153805aa04 Mon Sep 17 00:00:00 2001 From: Thilo Rusche Date: Thu, 26 Apr 2018 10:38:31 +0100 Subject: [PATCH] rubocop full rubocop integration --- .rubocop.yml | 55 ++++++++++++++++++++++++++++++ .rubocop_todo.yml | 36 +++++++++++++++++++ httplog.gemspec | 1 - lib/httplog/adapters/ethon.rb | 7 ++-- lib/httplog/adapters/excon.rb | 4 +-- lib/httplog/adapters/http.rb | 12 +++---- lib/httplog/configuration.rb | 2 -- lib/httplog/http_log.rb | 6 ++-- lib/httplog/version.rb | 2 +- spec/adapters/http_base_adapter.rb | 16 ++++----- spec/adapters/net_http_adapter.rb | 8 ++--- spec/adapters/open_uri_adapter.rb | 2 +- spec/http_log_spec.rb | 4 +-- 13 files changed, 118 insertions(+), 37 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..3c7ac94 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,55 @@ +inherit_from: ./.rubocop_todo.yml + +AllCops: + TargetRubyVersion: 2.2 + Exclude: + - 'db/**/*' + - 'db/schema.rb' + - 'app/assets/**' + - 'config/**/*' + - 'bin/**/*' + - 'node_modules/**/*' + - 'public/**/*' + - 'tmp/**/*' + - 'vendor/**/*' + - 'doc/**/*' + - 'coverage/**/*' + - Gemfile + - Guardfile + +Layout/EmptyLinesAroundBlockBody: + EnforcedStyle: no_empty_lines + Enabled: true + +Layout/EmptyLinesAroundClassBody: + EnforcedStyle: no_empty_lines + Enabled: true + +Layout/EmptyLinesAroundModuleBody: + EnforcedStyle: no_empty_lines + +Layout/IndentArray: + EnforcedStyle: consistent + +Style/Documentation: + Enabled: false + +Style/Lambda: + EnforcedStyle: literal + +Style/PercentLiteralDelimiters: + PreferredDelimiters: + default: '[]' + '%i': '[]' + +Metrics/PerceivedComplexity: + Enabled: false + +Metrics/CyclomaticComplexity: + Enabled: false + +Metrics/ModuleLength: + Enabled: false + +Metrics/BlockNesting: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..06b1662 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,36 @@ +# Offense count: 13 +Metrics/AbcSize: + Enabled: false + +# Offense count: 43 +# Configuration parameters: CountComments, ExcludedMethods. +Metrics/BlockLength: + Enabled: false + +# Offense count: 137 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Metrics/LineLength: + Enabled: false + +# Offense count: 7 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Enabled: false + +Metrics/ClassLength: + Enabled: false + +# Offense count: 195 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: when_needed, always, never +Style/FrozenStringLiteralComment: + Enabled: false + +# Offense count: 328 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Enabled: false diff --git a/httplog.gemspec b/httplog.gemspec index 54a317a..7151d1f 100644 --- a/httplog.gemspec +++ b/httplog.gemspec @@ -13,7 +13,6 @@ Gem::Specification.new do |gem| gem.summary = 'Log outgoing HTTP requests.' gem.authors = ['Thilo Rusche'] gem.email = 'thilorusche@gmail.com' - gem.files = Dir['lib/**/*'] + ['MIT-LICENSE', 'Rakefile', 'README.md', 'CHANGELOG.md'] gem.homepage = 'http://github.com/trusche/httplog' gem.description = "Log outgoing HTTP requests made from your application. Helpful for tracking API calls of third party gems that don't provide their own log output." diff --git a/lib/httplog/adapters/ethon.rb b/lib/httplog/adapters/ethon.rb index 6072537..3550b1e 100644 --- a/lib/httplog/adapters/ethon.rb +++ b/lib/httplog/adapters/ethon.rb @@ -24,14 +24,11 @@ module Operations def perform return orig_perform unless HttpLog.url_approved?(url) - response_code = nil - bm = Benchmark.realtime do - reponse_code = orig_perform - end + bm = Benchmark.realtime { orig_perform } # Not sure where the actual status code is stored - so let's # extract it from the response header. - status = response_headers.scan(/HTTP\/... (\d{3})/).flatten.first + status = response_headers.scan(%r{HTTP/... (\d{3})}).flatten.first encoding = response_headers.scan(/Content-Encoding: (\S+)/).flatten.first content_type = response_headers.scan(/Content-Type: (\S+(; charset=\S+)?)/).flatten.first diff --git a/lib/httplog/adapters/excon.rb b/lib/httplog/adapters/excon.rb index 99c223d..2165507 100644 --- a/lib/httplog/adapters/excon.rb +++ b/lib/httplog/adapters/excon.rb @@ -51,9 +51,7 @@ def request_call(datum) def response(datum = {}) return orig_response(datum) unless HttpLog.url_approved?(_httplog_url(datum)) - bm = Benchmark.realtime do - datum = orig_response(datum) - end + datum = orig_response(datum) response = datum[:response] headers = response[:headers] || {} HttpLog.log_status(response[:status]) diff --git a/lib/httplog/adapters/http.rb b/lib/httplog/adapters/http.rb index 0a2ff59..21f6a80 100644 --- a/lib/httplog/adapters/http.rb +++ b/lib/httplog/adapters/http.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true if defined?(::HTTP::Client) && defined?(::HTTP::Connection) - module ::HTTP + module ::HTTP # rubocop:disable Style/ClassAndModuleChildren class Client request_method = respond_to?('make_request') ? 'make_request' : 'perform' orig_request_method = "orig_#{request_method}" @@ -14,11 +14,11 @@ class Client HttpLog.log_request(req.verb, req.uri) HttpLog.log_headers(req.headers.to_h) - if defined?(::HTTP::Request::Body) - body = req.body.respond_to?(:source) ? req.body.source : req.body.instance_variable_get(:@body) - else - body = req.body - end + body = if defined?(::HTTP::Request::Body) + req.body.respond_to?(:source) ? req.body.source : req.body.instance_variable_get(:@body) + else + req.body + end HttpLog.log_data(body.to_s) body.rewind if body.respond_to?(:rewind) diff --git a/lib/httplog/configuration.rb b/lib/httplog/configuration.rb index 0010c67..7e9927f 100644 --- a/lib/httplog/configuration.rb +++ b/lib/httplog/configuration.rb @@ -14,7 +14,6 @@ class Configuration :log_status, :log_response, :log_benchmark, - :compact_log, :url_whitelist_pattern, :url_blacklist_pattern, :color, @@ -35,7 +34,6 @@ def initialize @log_status = true @log_response = true @log_benchmark = true - @compact_log = false @url_whitelist_pattern = /.*/ @url_blacklist_pattern = nil @color = false diff --git a/lib/httplog/http_log.rb b/lib/httplog/http_log.rb index 4df062e..2d5ff42 100644 --- a/lib/httplog/http_log.rb +++ b/lib/httplog/http_log.rb @@ -7,10 +7,10 @@ require 'rack' module HttpLog - LOG_PREFIX = '[httplog] ' + LOG_PREFIX = '[httplog] '.freeze class << self - attr_accessor :configuration + attr_writer :configuration def configuration @configuration ||= Configuration.new @@ -64,7 +64,7 @@ def log_benchmark(seconds) log("Benchmark: #{seconds.to_f.round(6)} seconds") end - def log_body(body, encoding=nil, content_type=nil) + def log_body(body, encoding = nil, content_type = nil) return if config.compact_log || !config.log_response unless text_based?(content_type) diff --git a/lib/httplog/version.rb b/lib/httplog/version.rb index 23b9f3e..b118986 100644 --- a/lib/httplog/version.rb +++ b/lib/httplog/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module HttpLog - VERSION = '1.0.2' + VERSION = '1.0.2'.freeze end diff --git a/spec/adapters/http_base_adapter.rb b/spec/adapters/http_base_adapter.rb index fc44030..afe372f 100644 --- a/spec/adapters/http_base_adapter.rb +++ b/spec/adapters/http_base_adapter.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true class HTTPBaseAdapter - def initialize(host, port, path, headers, data, params, protocol = 'http') - @host = host - @port = port - @path = path - @headers = headers - @data = data - @params = params - @protocol = protocol + def initialize(options = {}) + @host = options.fetch(:host, 'localhost') + @port = options.fetch(:port, 80) + @path = options.fetch(:path, '/') + @headers = options.fetch(:headers, {}) + @data = options.fetch(:data, nil) + @params = options.fetch(:params, {}) + @protocol = options.fetch(:protocol, 'http') end def logs_data? diff --git a/spec/adapters/net_http_adapter.rb b/spec/adapters/net_http_adapter.rb index 6b266c3..fb020e0 100644 --- a/spec/adapters/net_http_adapter.rb +++ b/spec/adapters/net_http_adapter.rb @@ -6,16 +6,14 @@ def send_get_request end def send_head_request - http = Net::HTTP.new(@host, @port) - http.head(@path, @headers) + Net::HTTP.new(@host, @port).head(@path, @headers) end def send_post_request - http = Net::HTTP.new(@host, @port) - resp = http.post(@path, @data) + Net::HTTP.new(@host, @port).post(@path, @data) end def send_post_form_request - res = Net::HTTP.post_form(parse_uri, @params) + Net::HTTP.post_form(parse_uri, @params) end end diff --git a/spec/adapters/open_uri_adapter.rb b/spec/adapters/open_uri_adapter.rb index f94720f..8d1d00e 100644 --- a/spec/adapters/open_uri_adapter.rb +++ b/spec/adapters/open_uri_adapter.rb @@ -2,7 +2,7 @@ class OpenUriAdapter < HTTPBaseAdapter def send_get_request - open(parse_uri) + open(parse_uri) # rubocop:disable Security/Open end def expected_response_body diff --git a/spec/http_log_spec.rb b/spec/http_log_spec.rb index b6e01f0..9b0b3e4 100644 --- a/spec/http_log_spec.rb +++ b/spec/http_log_spec.rb @@ -24,7 +24,7 @@ ADAPTERS.each do |adapter_class| context adapter_class, adapter: adapter_class.to_s do - let(:adapter) { adapter_class.new(host, port, path, headers, data, params) } + let(:adapter) { adapter_class.new(host: host, port: port, path: path, headers: headers, data: data, params: params) } context 'with default configuration' do connection_test_method = adapter_class.is_libcurl? ? :to_not : :to @@ -321,7 +321,7 @@ it 'should log a single line with status and benchmark' do adapter.send_get_request - expect(log).to match /\[httplog\] GET http:\/\/#{host}:#{port}#{path}(\?.*)? completed with status code \d{3} in (\d|\.)+/ + expect(log).to match(%r{\[httplog\] GET http://#{host}:#{port}#{path}(\?.*)? completed with status code \d{3} in (\d|\.)+}) expect(log).to_not include(HttpLog::LOG_PREFIX + "Connecting: #{host}:#{port}") expect(log).to_not include(HttpLog::LOG_PREFIX + 'Response:') expect(log).to_not include(HttpLog::LOG_PREFIX + 'Data:')