Skip to content

Commit f038ed5

Browse files
committed
Implemented Rubocop, a Ruby code static code analyzer.
1 parent 67d3f79 commit f038ed5

Some content is hidden

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

75 files changed

+1226
-1071
lines changed

.rubocop.yml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
LineLength:
2+
Enabled: false
3+
4+
MethodLength:
5+
Enabled: false
6+
7+
ClassLength:
8+
Enabled: false
9+
10+
Documentation:
11+
# don't require classes to be documented
12+
Enabled: false
13+
14+
CollectionMethods:
15+
# don't prefer map to collect, recuce to inject
16+
Enabled: false
17+
18+
Encoding:
19+
# no need to always specify encoding
20+
Enabled: false
21+
22+
HashMethods:
23+
# key? instead of has_key?
24+
# value? instead of has_value?
25+
Enabled: false
26+
27+
StringLiterals:
28+
# use single or double-quoted strings, as you please
29+
Enabled: false
30+
31+
Void:
32+
# == operator used in void context in specs
33+
Enabled: false
34+
35+
SignalException:
36+
# prefer raise to fail
37+
EnforcedStyle: only_raise
38+
39+
RaiseArgs:
40+
# don't care for what kind of raise
41+
Enabled: false
42+
43+
PerlBackrefs:
44+
# TODO: regular expression matching with $1, $2, etc.
45+
Enabled: false
46+
47+
BlockNesting:
48+
# TODO: fix too much nesting
49+
Max: 4
50+
51+
Lambda:
52+
# TODO: replace all lambda with -> or Proc
53+
Enabled: false
54+
55+
Blocks:
56+
# allow multi-line blocks like expect { }
57+
Enabled: false
58+
59+
WordArray:
60+
# %w vs. [ '', ... ]
61+
Enabled: false

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
language: ruby
2+
cache: bundler
13
rvm:
24
- 2.0.0
35
- 1.9.3
46
- jruby-19mode
57
- rbx-19mode
8+
script:
9+
- "bundle exec rubocop"

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Next Release
1212
* [#477](https://github.com/intridea/grape/pull/477): Fixed `default_error_formatter` which takes a format symbol - [@vad4msiu](https://github.com/vad4msiu).
1313
* Your contribution here.
1414

15+
#### Development
16+
17+
* Implemented Rubocop, a Ruby code static code analyzer - [@dblock](https://github.com/dblock).
18+
1519
0.6.0 (9/16/2013)
1620
=================
1721

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ group :development, :test do
1616
gem 'cookiejar'
1717
gem 'rack-contrib'
1818
gem 'redcarpet', :platforms => :ruby
19+
gem 'rubocop', '~> 0.14.1'
1920
end

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ module API
10151015
expose :user_name
10161016
expose :text, documentation: { type: "string", desc: "Status update text." }
10171017
expose :ip, if: { type: :full }
1018-
expose :user_type, user_id, if: lambda{ |status, options| status.user.public? }
1018+
expose :user_type, user_id, if: lambda { |status, options| status.user.public? }
10191019
expose :digest { |status, options| Digest::MD5.hexdigest(status.txt) }
10201020
expose :replies, using: API::Status, as: :replies
10211021
end

lib/grape/api.rb

+81-52
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def reset!
2626
end
2727

2828
def compile
29-
@instance = self.new
29+
@instance = new
3030
end
3131

3232
def change!
@@ -81,12 +81,12 @@ def do_not_route_options!
8181
# version 'v2'
8282
#
8383
# get '/main' do
84-
# {:some => 'data'}
84+
# {some: 'data'}
8585
# end
8686
#
8787
# version 'v1' do
8888
# get '/main' do
89-
# {:legacy => 'data'}
89+
# {legacy: 'data'}
9090
# end
9191
# end
9292
# end
@@ -95,7 +95,7 @@ def version(*args, &block)
9595
if args.any?
9696
options = args.pop if args.last.is_a? Hash
9797
options ||= {}
98-
options = {:using => :path}.merge!(options)
98+
options = { using: :path }.merge(options)
9999

100100
raise Grape::Exceptions::MissingVendorOption.new if options[:using] == :header && !options.has_key?(:vendor)
101101

@@ -111,7 +111,7 @@ def version(*args, &block)
111111

112112
# Add a description to the next namespace or function.
113113
def desc(description, options = {})
114-
@last_description = options.merge(:description => description)
114+
@last_description = options.merge(description: description)
115115
end
116116

117117
# Specify the default format for the API's serializers.
@@ -209,9 +209,7 @@ def rescue_from(*args, &block)
209209
end
210210

211211
options = args.last.is_a?(Hash) ? args.pop : {}
212-
if options.has_key?(:with)
213-
handler ||= proc { options[:with] }
214-
end
212+
handler ||= proc { options[:with] } if options.has_key?(:with)
215213

216214
if handler
217215
args.each do |arg|
@@ -220,8 +218,12 @@ def rescue_from(*args, &block)
220218
end
221219

222220
imbue(:rescue_options, options)
223-
set(:rescue_all, true) and return if args.include?(:all)
224-
imbue(:rescued_errors, args)
221+
222+
if args.include?(:all)
223+
set(:rescue_all, true)
224+
else
225+
imbue(:rescued_errors, args)
226+
end
225227
end
226228

227229
# Allows you to specify a default representation entity for a
@@ -230,10 +232,10 @@ def rescue_from(*args, &block)
230232
#
231233
# @example
232234
# class ExampleAPI < Grape::API
233-
# represent User, :with => Entity::User
235+
# represent User, with: Entity::User
234236
#
235237
# get '/me' do
236-
# present current_user # :with => Entity::User is assumed
238+
# present current_user # with: Entity::User is assumed
237239
# end
238240
# end
239241
#
@@ -276,7 +278,7 @@ def helpers(new_mod = nil, &block)
276278
include new_mod
277279
end
278280
end
279-
mod.class_eval &block if block_given?
281+
mod.class_eval(&block) if block_given?
280282
set(:helpers, mod)
281283
else
282284
mod = Module.new
@@ -292,7 +294,7 @@ def helpers(new_mod = nil, &block)
292294
# only `:http_basic`, `:http_digest` and `:oauth2` are supported.
293295
def auth(type = nil, options = {}, &block)
294296
if type
295-
set(:auth, {:type => type.to_sym, :proc => block}.merge(options))
297+
set(:auth, { type: type.to_sym, proc: block }.merge(options))
296298
else
297299
settings[:auth]
298300
end
@@ -318,14 +320,14 @@ def mount(mounts)
318320
mounts.each_pair do |app, path|
319321
if app.respond_to?(:inherit_settings, true)
320322
app_settings = settings.clone
321-
mount_path = Rack::Mount::Utils.normalize_path([ settings[:mount_path], path ].compact.join("/"))
323+
mount_path = Rack::Mount::Utils.normalize_path([settings[:mount_path], path].compact.join("/"))
322324
app_settings.set :mount_path, mount_path
323325
app.inherit_settings(app_settings)
324326
end
325327
endpoints << Grape::Endpoint.new(settings.clone, {
326-
:method => :any,
327-
:path => path,
328-
:app => app
328+
method: :any,
329+
path: path,
330+
app: app
329331
})
330332
end
331333
end
@@ -339,14 +341,14 @@ def mount(mounts)
339341
# @example Defining a basic route.
340342
# class MyAPI < Grape::API
341343
# route(:any, '/hello') do
342-
# {:hello => 'world'}
344+
# {hello: 'world'}
343345
# end
344346
# end
345347
def route(methods, paths = ['/'], route_options = {}, &block)
346348
endpoint_options = {
347-
:method => methods,
348-
:path => paths,
349-
:route_options => (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {})
349+
method: methods,
350+
path: paths,
351+
route_options: (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {})
350352
}
351353
endpoints << Grape::Endpoint.new(settings.clone, endpoint_options, &block)
352354

@@ -366,13 +368,33 @@ def after(&block)
366368
imbue(:afters, [block])
367369
end
368370

369-
def get(paths = ['/'], options = {}, &block); route('GET', paths, options, &block) end
370-
def post(paths = ['/'], options = {}, &block); route('POST', paths, options, &block) end
371-
def put(paths = ['/'], options = {}, &block); route('PUT', paths, options, &block) end
372-
def head(paths = ['/'], options = {}, &block); route('HEAD', paths, options, &block) end
373-
def delete(paths = ['/'], options = {}, &block); route('DELETE', paths, options, &block) end
374-
def options(paths = ['/'], options = {}, &block); route('OPTIONS', paths, options, &block) end
375-
def patch(paths = ['/'], options = {}, &block); route('PATCH', paths, options, &block) end
371+
def get(paths = ['/'], options = {}, &block)
372+
route('GET', paths, options, &block)
373+
end
374+
375+
def post(paths = ['/'], options = {}, &block)
376+
route('POST', paths, options, &block)
377+
end
378+
379+
def put(paths = ['/'], options = {}, &block)
380+
route('PUT', paths, options, &block)
381+
end
382+
383+
def head(paths = ['/'], options = {}, &block)
384+
route('HEAD', paths, options, &block)
385+
end
386+
387+
def delete(paths = ['/'], options = {}, &block)
388+
route('DELETE', paths, options, &block)
389+
end
390+
391+
def options(paths = ['/'], options = {}, &block)
392+
route('OPTIONS', paths, options, &block)
393+
end
394+
395+
def patch(paths = ['/'], options = {}, &block)
396+
route('PATCH', paths, options, &block)
397+
end
376398

377399
def namespace(space = nil, options = {}, &block)
378400
if space || block_given?
@@ -427,7 +449,10 @@ def use(middleware_class, *args, &block)
427449
# and arguments that are currently applied to the
428450
# application.
429451
def middleware
430-
settings.stack.inject([]){|a,s| a += s[:middleware] if s[:middleware]; a}
452+
settings.stack.inject([]) do |a, s|
453+
a += s[:middleware] if s[:middleware]
454+
a
455+
end
431456
end
432457

433458
# An array of API routes.
@@ -440,9 +465,11 @@ def versions
440465
end
441466

442467
def cascade(value = nil)
443-
value.nil? ?
444-
(settings.has_key?(:cascade) ? !! settings[:cascade] : true) :
468+
if value.nil?
469+
settings.has_key?(:cascade) ? !!settings[:cascade] : true
470+
else
445471
set(:cascade, value)
472+
end
446473
end
447474

448475
protected
@@ -459,15 +486,15 @@ def prepare_routes
459486
# block passed in. Allows for simple 'before' setups
460487
# of settings stack pushes.
461488
def nest(*blocks, &block)
462-
blocks.reject!{|b| b.nil?}
489+
blocks.reject! { |b| b.nil? }
463490
if blocks.any?
464491
settings.push # create a new context to eval the follow
465-
instance_eval &block if block_given?
466-
blocks.each{|b| instance_eval &b}
467-
settings.pop # when finished, we pop the context
492+
instance_eval(&block) if block_given?
493+
blocks.each { |b| instance_eval(&b) }
494+
settings.pop # when finished, we pop the context
468495
reset_validations!
469496
else
470-
instance_eval &block
497+
instance_eval(&block)
471498
end
472499
end
473500

@@ -497,7 +524,7 @@ def initialize
497524
def call(env)
498525
status, headers, body = @route_set.call(env)
499526
headers.delete('X-Cascade') unless cascade?
500-
[ status, headers, body ]
527+
[status, headers, body]
501528
end
502529

503530
# Some requests may return a HTTP 404 error if grape cannot find a matching
@@ -509,8 +536,8 @@ def call(env)
509536
# errors from reaching upstream. This is effectivelly done by unsetting
510537
# X-Cascade. Default :cascade is true.
511538
def cascade?
512-
return !! self.class.settings[:cascade] if self.class.settings.has_key?(:cascade)
513-
return !! self.class.settings[:version_options][:cascade] if self.class.settings[:version_options] && self.class.settings[:version_options].has_key?(:cascade)
539+
return !!self.class.settings[:cascade] if self.class.settings.has_key?(:cascade)
540+
return !!self.class.settings[:version_options][:cascade] if self.class.settings[:version_options] && self.class.settings[:version_options].has_key?(:cascade)
514541
true
515542
end
516543

@@ -523,32 +550,34 @@ def cascade?
523550
# will return an HTTP 405 response for any HTTP method that the resource
524551
# cannot handle.
525552
def add_head_not_allowed_methods
526-
allowed_methods = Hash.new{|h,k| h[k] = [] }
527-
resources = self.class.endpoints.map do |endpoint|
528-
endpoint.options[:app] && endpoint.options[:app].respond_to?(:endpoints) ?
529-
endpoint.options[:app].endpoints.map(&:routes) :
553+
allowed_methods = Hash.new { |h, k| h[k] = [] }
554+
resources = self.class.endpoints.map do |endpoint|
555+
if endpoint.options[:app] && endpoint.options[:app].respond_to?(:endpoints)
556+
endpoint.options[:app].endpoints.map(&:routes)
557+
else
530558
endpoint.routes
559+
end
531560
end
532561
resources.flatten.each do |route|
533562
allowed_methods[route.route_compiled] << route.route_method
534563
end
535564
allowed_methods.each do |path_info, methods|
536-
if methods.include?('GET') && ! methods.include?("HEAD") && ! self.class.settings[:do_not_route_head]
537-
methods = methods | [ 'HEAD' ]
565+
if methods.include?('GET') && !methods.include?("HEAD") && !self.class.settings[:do_not_route_head]
566+
methods = methods | ['HEAD']
538567
end
539568
allow_header = (["OPTIONS"] | methods).join(", ")
540569
unless methods.include?("OPTIONS") || self.class.settings[:do_not_route_options]
541-
@route_set.add_route( proc { [204, { 'Allow' => allow_header }, []]}, {
542-
:path_info => path_info,
543-
:request_method => "OPTIONS"
570+
@route_set.add_route(proc { [204, { 'Allow' => allow_header }, []] }, {
571+
path_info: path_info,
572+
request_method: "OPTIONS"
544573
})
545574
end
546575
not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - methods
547576
not_allowed_methods << "OPTIONS" if self.class.settings[:do_not_route_options]
548577
not_allowed_methods.each do |bad_method|
549-
@route_set.add_route( proc { [405, { 'Allow' => allow_header, 'Content-Type' => 'text/plain' }, []]}, {
550-
:path_info => path_info,
551-
:request_method => bad_method
578+
@route_set.add_route(proc { [405, { 'Allow' => allow_header, 'Content-Type' => 'text/plain' }, []] }, {
579+
path_info: path_info,
580+
request_method: bad_method
552581
})
553582
end
554583
end

0 commit comments

Comments
 (0)