Skip to content

Commit 083e757

Browse files
committed
Add the standardrb linter
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`.
1 parent b6214d8 commit 083e757

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1688
-1389
lines changed

.rubocop.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
##
2+
# Plugins
3+
require:
4+
- standard
5+
6+
##
7+
# Defaults: standardrb
8+
inherit_gem:
9+
standard: config/base.yml
10+
11+
##
12+
# Rules that break from standardrb defaults
13+
Style/StringLiterals:
14+
EnforcedStyle: single_quotes
15+
16+
##
17+
# Disabled rules
18+
Lint/AssignmentInCondition:
19+
Enabled: false
20+
Lint/FloatComparison:
21+
Enabled: false
22+
Lint/ConstantDefinitionInBlock:
23+
Enabled: false
24+
Lint/EmptyWhen:
25+
Exclude:
26+
- "lib/webmachine/dispatcher/route.rb"
27+
Lint/DuplicateMethods:
28+
Exclude:
29+
- "lib/webmachine/application.rb"
30+
Lint/UnderscorePrefixedVariableName:
31+
Exclude:
32+
- "lib/webmachine/trace/resource_proxy.rb"
33+
- "spec/webmachine/dispatcher_spec.rb"
34+
Lint/NestedMethodDefinition:
35+
Exclude:
36+
- "spec/webmachine/decision/flow_spec.rb"
37+
Lint/RescueException:
38+
Exclude:
39+
- "spec/webmachine/decision/fsm_spec.rb"
40+
Lint/RaiseException:
41+
Exclude:
42+
- "spec/webmachine/decision/fsm_spec.rb"
43+
Style/MissingRespondToMissing:
44+
Exclude:
45+
- "lib/webmachine/request.rb"
46+
Style/NilComparison:
47+
Exclude:
48+
- "spec/webmachine/decision/falsey_spec.rb"
49+
Style/GlobalVars:
50+
Exclude:
51+
- "lib/webmachine/decision/conneg.rb"
52+
53+
AllCops:
54+
SuggestExtensions: false

Gemfile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ source 'https://rubygems.org'
33
gemspec
44

55
group :development do
6-
gem "yard", "~> 0.9"
7-
gem "rake", "~> 12.0"
6+
gem 'yard', '~> 0.9'
7+
gem 'rake', '~> 12.0'
88
end
99

1010
group :test do
11-
gem "rspec", "~> 3.0", ">= 3.6.0"
12-
gem "rspec-its", "~> 1.2"
13-
gem "rack", "~> 2.0"
14-
gem "rack-test", "~> 0.7"
15-
gem "websocket_parser", "~>1.0"
11+
gem 'rspec', '~> 3.0', '>= 3.6.0'
12+
gem 'rspec-its', '~> 1.2'
13+
gem 'rack', '~> 2.0'
14+
gem 'rack-test', '~> 0.7'
15+
gem 'websocket_parser', '~>1.0'
1616
end
1717

1818
group :guard do

Guardfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
gemset = ENV['RVM_GEMSET'] || 'webmachine'
22
gemset = "@#{gemset}" unless gemset.to_s == ''
33

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

6-
guard 'rspec', :cli => "--color --profile", :growl => true, :rvm => rvms do
7-
watch(%r{^lib/webmachine/locale/.+$}) { "spec" }
6+
guard 'rspec', cli: '--color --profile', growl: true, rvm: rvms do
7+
watch(%r{^lib/webmachine/locale/.+$}) { 'spec' }
88
watch(%r{^spec/.+_spec\.rb$})
9-
watch(%r{^lib/(.+)\.rb$}){ |m| "spec/#{m[1]}_spec.rb" }
10-
watch('spec/spec_helper.rb') { "spec" }
9+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
10+
watch('spec/spec_helper.rb') { 'spec' }
1111
end

Rakefile

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
require "bundler/gem_tasks"
1+
require 'bundler/gem_tasks'
22

33
begin
44
require 'yard'
55
require 'yard/rake/yardoc_task'
66
YARD::Rake::YardocTask.new do |doc|
7-
doc.files = Dir["lib/**/*.rb"] + ['README.md']
8-
doc.options = ["-m", "markdown"]
7+
doc.files = Dir['lib/**/*.rb'] + ['README.md']
8+
doc.options = ['-m', 'markdown']
99
end
1010
rescue LoadError
1111
end
1212

13-
desc "Validate the gemspec file."
13+
desc 'Validate the gemspec file.'
1414
task :validate_gemspec do
15-
Gem::Specification.load("webmachine.gemspec").validate
15+
Gem::Specification.load('webmachine.gemspec').validate
1616
end
1717

18-
task :build => :validate_gemspec
18+
task build: :validate_gemspec
1919

20-
desc "Cleans up white space in source files"
20+
desc 'Cleans up white space in source files'
2121
task :clean_whitespace do
2222
no_file_cleaned = true
2323

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

3434
if no_file_cleaned
35-
puts "No files with trailing whitespace found"
35+
puts 'No files with trailing whitespace found'
3636
end
3737
end
3838

3939
require 'rspec/core/rake_task'
4040

41-
desc "Run specs"
41+
desc 'Run specs'
4242
RSpec::Core::RakeTask.new(:spec)
4343

44-
task :default => :spec
44+
task default: :spec

examples/debugger.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
require 'webmachine/trace'
33

44
class MyTracedResource < Webmachine::Resource
5-
def trace?; true; end
5+
def trace?
6+
true
7+
end
68

79
def resource_exists?
810
case request.query['e']
911
when 'true'
1012
true
1113
when 'fail'
12-
raise "BOOM"
14+
raise 'BOOM'
1315
else
1416
false
1517
end
1618
end
1719

1820
def to_html
19-
"<html>You found me.</html>"
21+
'<html>You found me.</html>'
2022
end
2123
end
2224

examples/logging.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def handle_event(event)
1818
resource = event.payload[:resource]
1919
code = event.payload[:code]
2020

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

3535
app.configure do |config|
3636
config.adapter = :WEBrick
37-
config.adapter_options = {:AccessLog => [], :Logger => Logger.new('/dev/null')}
37+
config.adapter_options = {AccessLog: [], Logger: Logger.new('/dev/null')}
3838
end
3939
end
4040

examples/webrick.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ def last_modified
66
end
77

88
def encodings_provided
9-
{ "gzip" => :encode_gzip, "identity" => :encode_identity }
9+
{'gzip' => :encode_gzip, 'identity' => :encode_identity}
1010
end
1111

1212
def to_html
13-
"<html><head><title>Hello from Webmachine</title></head><body>Hello, world!</body></html>"
13+
'<html><head><title>Hello from Webmachine</title></head><body>Hello, world!</body></html>'
1414
end
1515
end
1616

lib/webmachine/adapters/rack.rb

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class Rack < Adapter
4747
# Start the Rack adapter
4848
def run
4949
options = DEFAULT_OPTIONS.merge({
50-
:app => self,
51-
:Port => application.configuration.port,
52-
:Host => application.configuration.ip
50+
app: self,
51+
Port: application.configuration.port,
52+
Host: application.configuration.ip
5353
}).merge(application.configuration.adapter_options)
5454

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

7070
response.headers[SERVER] = VERSION_STRING
7171

72-
rack_status = response.code
72+
rack_status = response.code
7373
rack_headers = response.headers.flattened(NEWLINE)
7474
rack_body = case response.body
75-
when String # Strings are enumerable in ruby 1.8
76-
[response.body]
77-
else
78-
if (io_body = IO.try_convert(response.body))
79-
io_body
80-
elsif response.body.respond_to?(:call)
81-
Webmachine::ChunkedBody.new(Array(response.body.call))
82-
elsif response.body.respond_to?(:each)
83-
# This might be an IOEncoder with a Content-Length, which shouldn't be chunked.
84-
if response.headers[TRANSFER_ENCODING] == "chunked"
85-
Webmachine::ChunkedBody.new(response.body)
86-
else
87-
response.body
88-
end
89-
else
90-
[response.body.to_s]
91-
end
92-
end
75+
when String # Strings are enumerable in ruby 1.8
76+
[response.body]
77+
else
78+
if (io_body = IO.try_convert(response.body))
79+
io_body
80+
elsif response.body.respond_to?(:call)
81+
Webmachine::ChunkedBody.new(Array(response.body.call))
82+
elsif response.body.respond_to?(:each)
83+
# This might be an IOEncoder with a Content-Length, which shouldn't be chunked.
84+
if response.headers[TRANSFER_ENCODING] == 'chunked'
85+
Webmachine::ChunkedBody.new(response.body)
86+
else
87+
response.body
88+
end
89+
else
90+
[response.body.to_s]
91+
end
92+
end
9393

9494
rack_res = RackResponse.new(rack_body, rack_status, rack_headers)
9595
rack_res.finish
9696
end
9797

9898
protected
99+
99100
def routing_tokens(rack_req)
100101
nil # no-op for default, un-mapped rack adapter
101102
end
@@ -105,15 +106,15 @@ def base_uri(rack_req)
105106
end
106107

107108
private
109+
108110
def build_webmachine_request(rack_req, headers)
109111
RackRequest.new(rack_req.request_method,
110-
rack_req.url,
111-
headers,
112-
RequestBody.new(rack_req),
113-
routing_tokens(rack_req),
114-
base_uri(rack_req),
115-
rack_req.env
116-
)
112+
rack_req.url,
113+
headers,
114+
RequestBody.new(rack_req),
115+
routing_tokens(rack_req),
116+
base_uri(rack_req),
117+
rack_req.env)
117118
end
118119

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

131132
def initialize(body, status, headers)
132-
@body = body
133-
@status = status
133+
@body = body
134+
@status = status
134135
@headers = headers
135136
end
136137

137138
def finish
138139
@headers[CONTENT_TYPE] ||= TEXT_HTML if rack_release_enforcing_content_type
139-
@headers.delete(CONTENT_TYPE) if response_without_body
140+
@headers.delete(CONTENT_TYPE) if response_without_body
140141
[@status, @headers, @body]
141142
end
142143

@@ -188,10 +189,13 @@ def to_s
188189
# @yieldparam [String] chunk a chunk of the request body
189190
def each
190191
if @value
191-
@value.each {|chunk| yield chunk }
192+
@value.each { |chunk| yield chunk }
192193
else
193194
@value = []
194-
@request.body.each {|chunk| @value << chunk; yield chunk }
195+
@request.body.each { |chunk|
196+
@value << chunk
197+
yield chunk
198+
}
195199
end
196200
end
197201
end # class RequestBody

lib/webmachine/adapters/rack_mapped.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class RackMapped < Rack
2626

2727
def routing_tokens(rack_req)
2828
routing_match = rack_req.path_info.match(Webmachine::Request::ROUTING_PATH_MATCH)
29-
routing_path = routing_match ? routing_match[1] : ""
29+
routing_path = routing_match ? routing_match[1] : ''
3030
routing_path.split(SLASH)
3131
end
3232

0 commit comments

Comments
 (0)