Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Cop in some Layout departments #435

Merged
merged 16 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,129 @@ AllCops:
- 'examples/**/*'
- 'vendor/**/*'

Layout/BlockEndNewline:
Enabled: true

Layout/EmptyComment:
Enabled: true

Layout/EmptyLines:
Enabled: true

Layout/EndOfLine:
Enabled: true

Layout/ExtraSpacing:
Enabled: true

Layout/IndentationStyle:
Enabled: true

Layout/IndentationWidth:
Enabled: true

Layout/InitialIndentation:
Enabled: true

Layout/LeadingCommentSpace:
Enabled: true

Layout/LeadingEmptyLines:
Enabled: true

Layout/LineContinuationLeadingSpace:
Enabled: true

Layout/LineContinuationSpacing:
Enabled: true

Layout/RedundantLineBreak:
Enabled: true

Layout/SpaceAfterColon:
Enabled: true

Layout/SpaceAfterComma:
Enabled: true

Layout/SpaceAfterMethodName:
Enabled: true

Layout/SpaceAfterNot:
Enabled: true

Layout/SpaceAfterSemicolon:
Enabled: true

Layout/SpaceAroundBlockParameters:
Enabled: true

Layout/SpaceAroundEqualsInParameterDefault:
Enabled: true

Layout/SpaceAroundKeyword:
Enabled: true

Layout/SpaceAroundMethodCallOperator:
Enabled: true

Layout/SpaceAroundOperators:
Enabled: true

Layout/SpaceBeforeBlockBraces:
Enabled: true

Layout/SpaceBeforeBrackets:
Enabled: true

Layout/SpaceBeforeComma:
Enabled: true

Layout/SpaceBeforeComment:
Enabled: true

Layout/SpaceBeforeFirstArg:
Enabled: true

Layout/SpaceBeforeSemicolon:
Enabled: true

Layout/SpaceInLambdaLiteral:
Enabled: true

Layout/SpaceInsideArrayLiteralBrackets:
Enabled: true

Layout/SpaceInsideArrayPercentLiteral:
Enabled: true

Layout/SpaceInsideBlockBraces:
Enabled: true

SpaceInsideHashLiteralBraces:
Enabled: true

Layout/SpaceInsideParens:
Enabled: true

Layout/SpaceInsidePercentLiteralDelimiters:
Enabled: true

Layout/SpaceInsideRangeLiteral:
Enabled: true

Layout/SpaceInsideReferenceBrackets:
Enabled: true

Layout/SpaceInsideStringInterpolation:
Enabled: true

Layout/TrailingEmptyLines:
Enabled: true

Layout/TrailingWhitespace:
Enabled: true

Performance:
Exclude:
- 'test/**/*'
Expand Down
4 changes: 2 additions & 2 deletions committee.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Gem::Specification.new do |s|
s.homepage = "https://github.com/interagent/committee"
s.license = "MIT"

s.executables << "committee-stub"
s.files = Dir["{bin,lib,test}/**/*.rb"]
s.executables << "committee-stub"
s.files = Dir["{bin,lib,test}/**/*.rb"]

s.required_ruby_version = ">= 2.7.0"

Expand Down
7 changes: 1 addition & 6 deletions lib/committee/bin/committee_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ def get_app(schema, options)

# Gets an option parser for command line arguments.
def get_options_parser
options = {
driver: nil,
help: false,
port: 9292,
tolerant: false,
}
options = { driver: nil, help: false, port: 9292, tolerant: false, }

parser = OptionParser.new do |opts|
opts.banner = "Usage: rackup [options] [JSON Schema file]"
Expand Down
2 changes: 1 addition & 1 deletion lib/committee/drivers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def self.load_from_data(hash, schema_path = nil, parser_options: {})
'from next version. Pass config `strict_reference_validation: true` (or false, if you must) ' +
'to quiet this warning.')
opts[:strict_reference_validation] ||= false

openapi = OpenAPIParser.parse_with_filepath(hash, schema_path, opts)
return Committee::Drivers::OpenAPI3::Driver.new.parse(openapi)
end
Expand Down
27 changes: 7 additions & 20 deletions lib/committee/drivers/open_api_2/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,7 @@ def schema_class

# These are fields that the OpenAPI 2 spec considers mandatory to be
# included in the document's top level.
REQUIRED_FIELDS = [
:consumes,
:definitions,
:paths,
:produces,
:swagger,
].map(&:to_s).freeze
REQUIRED_FIELDS = [:consumes, :definitions, :paths, :produces, :swagger,].map(&:to_s).freeze

def find_best_fit_response(link_data)
if response_data = link_data["responses"]["200"] || response_data = link_data["responses"][200]
Expand All @@ -97,8 +91,7 @@ def find_best_fit_response(link_data)
else
# Sort responses so that we can try to prefer any 3-digit status code.
# If there are none, we'll just take anything from the list.
ordered_responses = link_data["responses"].
select { |k, v| k.to_s =~ /[0-9]{3}/ }
ordered_responses = link_data["responses"].select { |k, v| k.to_s =~ /[0-9]{3}/ }
if first = ordered_responses.first
[first[0].to_i, first[1]]
else
Expand All @@ -117,9 +110,7 @@ def parse_definitions!(data)
# that all references to it will still have correct paths (i.e. we can
# still find a resource at '#/definitions/resource' instead of
# '#/resource').
schema = JsonSchema.parse!({
"definitions" => data['definitions'],
})
schema = JsonSchema.parse!({ "definitions" => data['definitions'], })
schema.expand_references!
schema.uri = DEFINITIONS_PSEUDO_URI

Expand Down Expand Up @@ -182,8 +173,7 @@ def parse_routes!(data, schema, store)

# A link need not necessarily specify a target schema.
if response_data["schema"]
target_schemas_data["properties"][href]["properties"][method] =
response_data["schema"]
target_schemas_data["properties"][href]["properties"][method] = response_data["schema"]
end
end

Expand All @@ -199,10 +189,8 @@ def parse_routes!(data, schema, store)
# #parse_definitions!, but what we're doing here is prefixing references
# with a specialized internal URI so that they can reference definitions
# from another document in the store.
schemas =
rewrite_references_and_parse(schemas_data, store)
target_schemas =
rewrite_references_and_parse(target_schemas_data, store)
schemas = rewrite_references_and_parse(schemas_data, store)
target_schemas = rewrite_references_and_parse(target_schemas_data, store)

# As noted above, now that we've parsed our aggregate response schema, go
# back through each link and them their response schema.
Expand All @@ -218,8 +206,7 @@ def parse_routes!(data, schema, store)
end

# response
link.target_schema =
target_schemas.properties[link.href].properties[method]
link.target_schema = target_schemas.properties[link.href].properties[method]
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/committee/drivers/open_api_2/parameter_schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def call
check_required_fields!(body_param)

if link_data["parameters"].detect { |p| p["in"] == "form" } != nil
raise ArgumentError, "Committee: can't mix body parameter " \
"with form parameters."
raise ArgumentError, "Committee: can't mix body parameter with form parameters."
end

schema_data = body_param["schema"]
Expand Down
7 changes: 2 additions & 5 deletions lib/committee/drivers/open_api_2/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ def initialize(link_data)

private

LINK_REQUIRED_FIELDS = [
:name
].map(&:to_s).freeze
LINK_REQUIRED_FIELDS = [:name].map(&:to_s).freeze

attr_accessor :link_data

def check_required_fields!(param_data)
LINK_REQUIRED_FIELDS.each do |field|
if !param_data[field]
raise ArgumentError,
"Committee: no #{field} section in link data."
raise ArgumentError, "Committee: no #{field} section in link data."
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/committee/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BadRequest < Error
class InvalidRequest < Error
attr_reader :original_error

def initialize(error_message=nil, original_error: nil)
def initialize(error_message = nil, original_error: nil)
@original_error = original_error
super(error_message)
end
Expand All @@ -19,7 +19,7 @@ def initialize(error_message=nil, original_error: nil)
class InvalidResponse < Error
attr_reader :original_error

def initialize(error_message=nil, original_error: nil)
def initialize(error_message = nil, original_error: nil)
@original_error = original_error
super(error_message)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/committee/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Committee
module Middleware
class Base
def initialize(app, options={})
def initialize(app, options = {})
@app = app

@error_class = options.fetch(:error_class, Committee::ValidationError)
Expand All @@ -14,7 +14,7 @@ def initialize(app, options={})
@schema = self.class.get_schema(options)

@router = @schema.build_router(options)
@accept_request_filter = options[:accept_request_filter] || -> (_) { true }
@accept_request_filter = options[:accept_request_filter] || ->(_) { true }
end

def call(env)
Expand Down
2 changes: 1 addition & 1 deletion lib/committee/middleware/request_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Committee
module Middleware
class RequestValidation < Base
def initialize(app, options={})
def initialize(app, options = {})
super

@strict = options[:strict]
Expand Down
2 changes: 1 addition & 1 deletion lib/committee/middleware/stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Committee
module Middleware
class Stub < Base
def initialize(app, options={})
def initialize(app, options = {})
super

# A bug in Committee's cache implementation meant that it wasn't working
Expand Down
7 changes: 3 additions & 4 deletions lib/committee/request_unpacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def indifferent_params(object)
end
end

def initialize(options={})
def initialize(options = {})
@allow_form_params = options[:allow_form_params]
@allow_get_body = options[:allow_get_body]
@allow_query_params = options[:allow_query_params]
Expand All @@ -32,7 +32,7 @@ def unpack_request_params(request)
# if Content-Type is empty or JSON, and there was a request body, try to
# interpret it as JSON
params = if !request.media_type || request.media_type =~ %r{application/(?:.*\+)?json}
parse_json(request)
parse_json(request)
elsif @optimistic_json
begin
parse_json(request)
Expand Down Expand Up @@ -87,8 +87,7 @@ def parse_json(request)
# We want a hash specifically. '42', 42, and [42] will all be
# decoded properly, but we can't use them here.
if !hash.is_a?(Hash)
raise BadRequest,
"Invalid JSON input. Require object with parameters as keys."
raise BadRequest, "Invalid JSON input. Require object with parameters as keys."
end
self.class.indifferent_params(hash)
end
Expand Down
Loading
Loading