Skip to content

Commit

Permalink
Add the standardrb linter
Browse files Browse the repository at this point in the history
standardrb provides a relaxed set of linting rules that
provide the benefits of a linter without the headaches
that can come from the slightly excessive rubocop defaults.

New rules we'd like to adopt can be enabled in
`.rubocop.yml`, and similarly rules we don't want
to adopt can be disabled in `.rubocop.yml`.
  • Loading branch information
0x1eef committed Jan 17, 2023
1 parent b6214d8 commit 083e757
Show file tree
Hide file tree
Showing 64 changed files with 1,688 additions and 1,389 deletions.
54 changes: 54 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
##
# Plugins
require:
- standard

##
# Defaults: standardrb
inherit_gem:
standard: config/base.yml

##
# Rules that break from standardrb defaults
Style/StringLiterals:
EnforcedStyle: single_quotes

##
# Disabled rules
Lint/AssignmentInCondition:
Enabled: false
Lint/FloatComparison:
Enabled: false
Lint/ConstantDefinitionInBlock:
Enabled: false
Lint/EmptyWhen:
Exclude:
- "lib/webmachine/dispatcher/route.rb"
Lint/DuplicateMethods:
Exclude:
- "lib/webmachine/application.rb"
Lint/UnderscorePrefixedVariableName:
Exclude:
- "lib/webmachine/trace/resource_proxy.rb"
- "spec/webmachine/dispatcher_spec.rb"
Lint/NestedMethodDefinition:
Exclude:
- "spec/webmachine/decision/flow_spec.rb"
Lint/RescueException:
Exclude:
- "spec/webmachine/decision/fsm_spec.rb"
Lint/RaiseException:
Exclude:
- "spec/webmachine/decision/fsm_spec.rb"
Style/MissingRespondToMissing:
Exclude:
- "lib/webmachine/request.rb"
Style/NilComparison:
Exclude:
- "spec/webmachine/decision/falsey_spec.rb"
Style/GlobalVars:
Exclude:
- "lib/webmachine/decision/conneg.rb"

AllCops:
SuggestExtensions: false
14 changes: 7 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ source 'https://rubygems.org'
gemspec

group :development do
gem "yard", "~> 0.9"
gem "rake", "~> 12.0"
gem 'yard', '~> 0.9'
gem 'rake', '~> 12.0'
end

group :test do
gem "rspec", "~> 3.0", ">= 3.6.0"
gem "rspec-its", "~> 1.2"
gem "rack", "~> 2.0"
gem "rack-test", "~> 0.7"
gem "websocket_parser", "~>1.0"
gem 'rspec', '~> 3.0', '>= 3.6.0'
gem 'rspec-its', '~> 1.2'
gem 'rack', '~> 2.0'
gem 'rack-test', '~> 0.7'
gem 'websocket_parser', '~>1.0'
end

group :guard do
Expand Down
10 changes: 5 additions & 5 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
gemset = ENV['RVM_GEMSET'] || 'webmachine'
gemset = "@#{gemset}" unless gemset.to_s == ''

rvms = %W[ 1.8.7 1.9.2 1.9.3 jruby rbx ].map {|v| "#{v}#{gemset}" }
rvms = %W[1.8.7 1.9.2 1.9.3 jruby rbx].map { |v| "#{v}#{gemset}" }

guard 'rspec', :cli => "--color --profile", :growl => true, :rvm => rvms do
watch(%r{^lib/webmachine/locale/.+$}) { "spec" }
guard 'rspec', cli: '--color --profile', growl: true, rvm: rvms do
watch(%r{^lib/webmachine/locale/.+$}) { 'spec' }
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}){ |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { 'spec' }
end
24 changes: 12 additions & 12 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'

begin
require 'yard'
require 'yard/rake/yardoc_task'
YARD::Rake::YardocTask.new do |doc|
doc.files = Dir["lib/**/*.rb"] + ['README.md']
doc.options = ["-m", "markdown"]
doc.files = Dir['lib/**/*.rb'] + ['README.md']
doc.options = ['-m', 'markdown']
end
rescue LoadError
end

desc "Validate the gemspec file."
desc 'Validate the gemspec file.'
task :validate_gemspec do
Gem::Specification.load("webmachine.gemspec").validate
Gem::Specification.load('webmachine.gemspec').validate
end

task :build => :validate_gemspec
task build: :validate_gemspec

desc "Cleans up white space in source files"
desc 'Cleans up white space in source files'
task :clean_whitespace do
no_file_cleaned = true

Dir["**/*.rb"].each do |file|
Dir['**/*.rb'].each do |file|
contents = File.read(file)
cleaned_contents = contents.gsub(/([ \t]+)$/, '')
unless cleaned_contents == contents
no_file_cleaned = false
puts " - Cleaned #{file}"
File.open(file, 'w') { |f| f.write(cleaned_contents) }
File.write(file, cleaned_contents)
end
end

if no_file_cleaned
puts "No files with trailing whitespace found"
puts 'No files with trailing whitespace found'
end
end

require 'rspec/core/rake_task'

desc "Run specs"
desc 'Run specs'
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
8 changes: 5 additions & 3 deletions examples/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
require 'webmachine/trace'

class MyTracedResource < Webmachine::Resource
def trace?; true; end
def trace?
true
end

def resource_exists?
case request.query['e']
when 'true'
true
when 'fail'
raise "BOOM"
raise 'BOOM'
else
false
end
end

def to_html
"<html>You found me.</html>"
'<html>You found me.</html>'
end
end

Expand Down
4 changes: 2 additions & 2 deletions examples/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def handle_event(event)
resource = event.payload[:resource]
code = event.payload[:code]

puts "[%s] method=%s uri=%s code=%d resource=%s time=%.4f" % [
puts '[%s] method=%s uri=%s code=%d resource=%s time=%.4f' % [
Time.now.iso8601, request.method, request.uri.to_s, code, resource,
event.duration
]
Expand All @@ -34,7 +34,7 @@ def handle_event(event)

app.configure do |config|
config.adapter = :WEBrick
config.adapter_options = {:AccessLog => [], :Logger => Logger.new('/dev/null')}
config.adapter_options = {AccessLog: [], Logger: Logger.new('/dev/null')}
end
end

Expand Down
4 changes: 2 additions & 2 deletions examples/webrick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def last_modified
end

def encodings_provided
{ "gzip" => :encode_gzip, "identity" => :encode_identity }
{'gzip' => :encode_gzip, 'identity' => :encode_identity}
end

def to_html
"<html><head><title>Hello from Webmachine</title></head><body>Hello, world!</body></html>"
'<html><head><title>Hello from Webmachine</title></head><body>Hello, world!</body></html>'
end
end

Expand Down
72 changes: 38 additions & 34 deletions lib/webmachine/adapters/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class Rack < Adapter
# Start the Rack adapter
def run
options = DEFAULT_OPTIONS.merge({
:app => self,
:Port => application.configuration.port,
:Host => application.configuration.ip
app: self,
Port: application.configuration.port,
Host: application.configuration.ip
}).merge(application.configuration.adapter_options)

@server = ::Rack::Server.new(options)
Expand All @@ -69,33 +69,34 @@ def call(env)

response.headers[SERVER] = VERSION_STRING

rack_status = response.code
rack_status = response.code
rack_headers = response.headers.flattened(NEWLINE)
rack_body = case response.body
when String # Strings are enumerable in ruby 1.8
[response.body]
else
if (io_body = IO.try_convert(response.body))
io_body
elsif response.body.respond_to?(:call)
Webmachine::ChunkedBody.new(Array(response.body.call))
elsif response.body.respond_to?(:each)
# This might be an IOEncoder with a Content-Length, which shouldn't be chunked.
if response.headers[TRANSFER_ENCODING] == "chunked"
Webmachine::ChunkedBody.new(response.body)
else
response.body
end
else
[response.body.to_s]
end
end
when String # Strings are enumerable in ruby 1.8
[response.body]
else
if (io_body = IO.try_convert(response.body))
io_body
elsif response.body.respond_to?(:call)
Webmachine::ChunkedBody.new(Array(response.body.call))
elsif response.body.respond_to?(:each)
# This might be an IOEncoder with a Content-Length, which shouldn't be chunked.
if response.headers[TRANSFER_ENCODING] == 'chunked'
Webmachine::ChunkedBody.new(response.body)
else
response.body
end
else
[response.body.to_s]
end
end

rack_res = RackResponse.new(rack_body, rack_status, rack_headers)
rack_res.finish
end

protected

def routing_tokens(rack_req)
nil # no-op for default, un-mapped rack adapter
end
Expand All @@ -105,15 +106,15 @@ def base_uri(rack_req)
end

private

def build_webmachine_request(rack_req, headers)
RackRequest.new(rack_req.request_method,
rack_req.url,
headers,
RequestBody.new(rack_req),
routing_tokens(rack_req),
base_uri(rack_req),
rack_req.env
)
rack_req.url,
headers,
RequestBody.new(rack_req),
routing_tokens(rack_req),
base_uri(rack_req),
rack_req.env)
end

class RackRequest < Webmachine::Request
Expand All @@ -129,14 +130,14 @@ class RackResponse
ONE_FIVE = '1.5'.freeze

def initialize(body, status, headers)
@body = body
@status = status
@body = body
@status = status
@headers = headers
end

def finish
@headers[CONTENT_TYPE] ||= TEXT_HTML if rack_release_enforcing_content_type
@headers.delete(CONTENT_TYPE) if response_without_body
@headers.delete(CONTENT_TYPE) if response_without_body
[@status, @headers, @body]
end

Expand Down Expand Up @@ -188,10 +189,13 @@ def to_s
# @yieldparam [String] chunk a chunk of the request body
def each
if @value
@value.each {|chunk| yield chunk }
@value.each { |chunk| yield chunk }
else
@value = []
@request.body.each {|chunk| @value << chunk; yield chunk }
@request.body.each { |chunk|
@value << chunk
yield chunk
}
end
end
end # class RequestBody
Expand Down
2 changes: 1 addition & 1 deletion lib/webmachine/adapters/rack_mapped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RackMapped < Rack

def routing_tokens(rack_req)
routing_match = rack_req.path_info.match(Webmachine::Request::ROUTING_PATH_MATCH)
routing_path = routing_match ? routing_match[1] : ""
routing_path = routing_match ? routing_match[1] : ''
routing_path.split(SLASH)
end

Expand Down
Loading

0 comments on commit 083e757

Please sign in to comment.