Skip to content

Commit

Permalink
rubocop
Browse files Browse the repository at this point in the history
full rubocop integration
  • Loading branch information
trusche committed Apr 26, 2018
1 parent 53e9d52 commit 29f4b58
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 37 deletions.
55 changes: 55 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion httplog.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Gem::Specification.new do |gem|
gem.summary = 'Log outgoing HTTP requests.'
gem.authors = ['Thilo Rusche']
gem.email = '[email protected]'
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."
Expand Down
7 changes: 2 additions & 5 deletions lib/httplog/adapters/ethon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions lib/httplog/adapters/excon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
12 changes: 6 additions & 6 deletions lib/httplog/adapters/http.rb
Original file line number Diff line number Diff line change
@@ -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}"
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions lib/httplog/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Configuration
:log_status,
:log_response,
:log_benchmark,
:compact_log,
:url_whitelist_pattern,
:url_blacklist_pattern,
:color,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/httplog/http_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/httplog/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module HttpLog
VERSION = '1.0.2'
VERSION = '1.0.2'.freeze
end
16 changes: 8 additions & 8 deletions spec/adapters/http_base_adapter.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
8 changes: 3 additions & 5 deletions spec/adapters/net_http_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/adapters/open_uri_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/http_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:')
Expand Down

0 comments on commit 29f4b58

Please sign in to comment.