From 771ce410d33efa62c70b58ecc958c792ab1db794 Mon Sep 17 00:00:00 2001 From: Roman Samoilov <2270393+rsamoilov@users.noreply.github.com> Date: Fri, 26 Jul 2024 19:05:50 +0100 Subject: [PATCH 1/3] Code style --- .rubocop.yml | 1052 +++++++++++++++++ Gemfile | 1 + lib/rage/cli.rb | 3 +- lib/rage/controller/api.rb | 14 +- lib/rage/cookies.rb | 12 +- lib/rage/ext/active_record/connection_pool.rb | 2 +- lib/rage/fiber.rb | 6 +- lib/rage/fiber_scheduler.rb | 2 +- lib/rage/logger/json_formatter.rb | 2 +- lib/rage/logger/logger.rb | 2 +- lib/rage/logger/text_formatter.rb | 2 +- lib/rage/middleware/cors.rb | 4 +- lib/rage/middleware/reloader.rb | 2 +- lib/rage/params_parser.rb | 2 +- lib/rage/router/backend.rb | 10 +- lib/rage/router/constrainer.rb | 2 +- lib/rage/router/dsl.rb | 12 +- .../dsl_plugins/legacy_hash_notation.rb | 2 +- .../dsl_plugins/legacy_root_notation.rb | 2 +- lib/rage/router/handler_storage.rb | 2 +- lib/rage/sidekiq_session.rb | 2 +- spec/controller/api/before_actions_spec.rb | 4 +- spec/controller/api/session_spec.rb | 2 +- spec/controller/api/wrap_parameters_spec.rb | 134 +-- spec/cors_middleware_spec.rb | 4 +- spec/fiber_scheduler_spec.rb | 6 +- spec/fiber_spec.rb | 12 +- .../app/controllers/async_controller.rb | 8 +- spec/integration/test_app/config/routes.rb | 2 +- spec/logger_spec.rb | 2 +- spec/multi_application_spec.rb | 4 +- spec/params_parser_spec.rb | 2 +- spec/router/controller_spec.rb | 31 +- spec/rspec/rspec_spec.rb | 6 +- spec/setup_spec.rb | 22 +- 35 files changed, 1214 insertions(+), 163 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..3a7ddc7c --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,1052 @@ +AllCops: + TargetRubyVersion: 3.1 + Exclude: [] + DisabledByDefault: true + SuggestExtensions: false + +Bundler/DuplicatedGem: + Enabled: true + Include: + - '**/*.gemfile' + - '**/Gemfile' + - '**/gems.rb' + +Bundler/InsecureProtocolSource: + Enabled: true + Include: + - '**/*.gemfile' + - '**/Gemfile' + - '**/gems.rb' + +Gemspec/DeprecatedAttributeAssignment: + Enabled: true + +Gemspec/DuplicatedAssignment: + Enabled: true + Include: + - '**/*.gemspec' + +Layout/AccessModifierIndentation: + Enabled: true + EnforcedStyle: indent + IndentationWidth: ~ + +Layout/ArgumentAlignment: + Enabled: true + EnforcedStyle: with_fixed_indentation + +Layout/ArrayAlignment: + Enabled: true + EnforcedStyle: with_fixed_indentation + +Layout/BeginEndAlignment: + Enabled: true + EnforcedStyleAlignWith: start_of_line + Severity: warning + +Layout/BlockAlignment: + Enabled: true + EnforcedStyleAlignWith: either + +Layout/BlockEndNewline: + Enabled: true + +Layout/CaseIndentation: + Enabled: true + EnforcedStyle: end + +Layout/ClosingHeredocIndentation: + Enabled: true + +Layout/ClosingParenthesisIndentation: + Enabled: true + +Layout/CommentIndentation: + Enabled: true + +Layout/ConditionPosition: + Enabled: true + +Layout/DefEndAlignment: + Enabled: true + EnforcedStyleAlignWith: start_of_line + Severity: warning + +Layout/DotPosition: + Enabled: true + EnforcedStyle: trailing + +Layout/ElseAlignment: + Enabled: true + +Layout/EmptyComment: + Enabled: true + AllowBorderComment: true + AllowMarginComment: true + +Layout/EmptyLineAfterMagicComment: + Enabled: true + +Layout/EmptyLineBetweenDefs: + Enabled: true + AllowAdjacentOneLineDefs: true + NumberOfEmptyLines: 1 + +Layout/EmptyLines: + Enabled: true + +Layout/EmptyLinesAroundAccessModifier: + Enabled: true + +Layout/EmptyLinesAroundArguments: + Enabled: true + +Layout/EmptyLinesAroundBeginBody: + Enabled: true + +Layout/EmptyLinesAroundBlockBody: + Enabled: true + EnforcedStyle: no_empty_lines + +Layout/EmptyLinesAroundClassBody: + Enabled: true + EnforcedStyle: no_empty_lines + +Layout/EmptyLinesAroundMethodBody: + Enabled: true + +Layout/EmptyLinesAroundModuleBody: + Enabled: true + EnforcedStyle: no_empty_lines + +Layout/EndAlignment: + Enabled: true + EnforcedStyleAlignWith: variable + Severity: warning + +Layout/EndOfLine: + Enabled: true + EnforcedStyle: native + +Layout/ExtraSpacing: + Enabled: true + AllowForAlignment: false + AllowBeforeTrailingComments: true + ForceEqualSignAlignment: false + +Layout/FirstArgumentIndentation: + Enabled: true + EnforcedStyle: consistent + IndentationWidth: ~ + +Layout/FirstArrayElementIndentation: + Enabled: true + EnforcedStyle: consistent + IndentationWidth: ~ + +Layout/FirstHashElementIndentation: + Enabled: true + EnforcedStyle: consistent + IndentationWidth: ~ + +Layout/HashAlignment: + Enabled: true + EnforcedHashRocketStyle: key + EnforcedColonStyle: key + EnforcedLastArgumentHashStyle: always_inspect + +Layout/HeredocIndentation: + Enabled: true + +Layout/IndentationConsistency: + Enabled: true + EnforcedStyle: normal + +Layout/IndentationStyle: + Enabled: true + IndentationWidth: ~ + +Layout/IndentationWidth: + Enabled: true + Width: 2 + AllowedPatterns: [] + +Layout/InitialIndentation: + Enabled: true + +Layout/LeadingCommentSpace: + Enabled: true + +Layout/LeadingEmptyLines: + Enabled: true + +Layout/LineContinuationSpacing: + Enabled: true + +Layout/MultilineArrayBraceLayout: + Enabled: true + EnforcedStyle: symmetrical + +Layout/MultilineBlockLayout: + Enabled: true + +Layout/MultilineHashBraceLayout: + Enabled: true + EnforcedStyle: symmetrical + +Layout/MultilineMethodCallBraceLayout: + Enabled: true + EnforcedStyle: symmetrical + +Layout/MultilineMethodCallIndentation: + Enabled: true + EnforcedStyle: indented + IndentationWidth: ~ + +Layout/MultilineMethodDefinitionBraceLayout: + Enabled: true + EnforcedStyle: symmetrical + +Layout/MultilineOperationIndentation: + Enabled: true + EnforcedStyle: aligned + +Layout/ParameterAlignment: + Enabled: true + EnforcedStyle: with_fixed_indentation + IndentationWidth: ~ + +Layout/RescueEnsureAlignment: + 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 + EnforcedStyleInsidePipes: no_space + +Layout/SpaceAroundEqualsInParameterDefault: + Enabled: true + EnforcedStyle: space + +Layout/SpaceAroundKeyword: + Enabled: true + +Layout/SpaceAroundMethodCallOperator: + Enabled: true + +Layout/SpaceAroundOperators: + Enabled: true + AllowForAlignment: true + +Layout/SpaceBeforeBlockBraces: + Enabled: true + EnforcedStyle: space + EnforcedStyleForEmptyBraces: space + +Layout/SpaceBeforeComma: + Enabled: true + +Layout/SpaceBeforeComment: + Enabled: true + +Layout/SpaceBeforeFirstArg: + Enabled: true + AllowForAlignment: true + +Layout/SpaceBeforeSemicolon: + Enabled: true + +Layout/SpaceInLambdaLiteral: + Enabled: true + EnforcedStyle: require_no_space + +Layout/SpaceInsideArrayLiteralBrackets: + Enabled: true + EnforcedStyle: no_space + EnforcedStyleForEmptyBrackets: no_space + +Layout/SpaceInsideArrayPercentLiteral: + Enabled: true + +Layout/SpaceInsideBlockBraces: + Enabled: true + EnforcedStyle: space + EnforcedStyleForEmptyBraces: no_space + SpaceBeforeBlockParameters: true + +Layout/SpaceInsideHashLiteralBraces: + Enabled: true + EnforcedStyle: space + EnforcedStyleForEmptyBraces: no_space + +Layout/SpaceInsideParens: + Enabled: true + EnforcedStyle: no_space + +Layout/SpaceInsidePercentLiteralDelimiters: + Enabled: true + +Layout/SpaceInsideRangeLiteral: + Enabled: true + +Layout/SpaceInsideReferenceBrackets: + Enabled: true + EnforcedStyle: no_space + EnforcedStyleForEmptyBrackets: no_space + +Layout/SpaceInsideStringInterpolation: + Enabled: true + EnforcedStyle: no_space + +Layout/TrailingEmptyLines: + Enabled: true + EnforcedStyle: final_newline + +Layout/TrailingWhitespace: + Enabled: true + AllowInHeredoc: true + +Lint/AmbiguousAssignment: + Enabled: true + +Lint/AmbiguousOperator: + Enabled: true + +Lint/AssignmentInCondition: + Enabled: true + AllowSafeAssignment: true + # Intentionally disable autocorrect to force us to intentionally decide + # whether assignment is intended as opposed to comparison + AutoCorrect: false + +Lint/BigDecimalNew: + Enabled: true + +Lint/BinaryOperatorWithIdenticalOperands: + Enabled: true + +Lint/CircularArgumentReference: + Enabled: true + +Lint/ConstantOverwrittenInRescue: + Enabled: true + +Lint/Debugger: + Enabled: true + +Lint/DeprecatedClassMethods: + Enabled: true + +Lint/DeprecatedConstants: + Enabled: true + +Lint/DeprecatedOpenSSLConstant: + Enabled: true + +Lint/DuplicateCaseCondition: + Enabled: true + +Lint/DuplicateElsifCondition: + Enabled: true + +Lint/DuplicateHashKey: + Enabled: true + +Lint/DuplicateMagicComment: + Enabled: true + +Lint/DuplicateMethods: + Enabled: true + +Lint/DuplicateRegexpCharacterClassElement: + Enabled: true + +Lint/DuplicateRequire: + Enabled: true + +Lint/DuplicateRescueException: + Enabled: true + +Lint/EachWithObjectArgument: + Enabled: true + +Lint/ElseLayout: + Enabled: true + +Lint/EmptyEnsure: + Enabled: true + +Lint/EmptyExpression: + Enabled: true + +Lint/EmptyInterpolation: + Enabled: true + +Lint/EmptyWhen: + Enabled: true + AllowComments: true + +Lint/EnsureReturn: + Enabled: true + +Lint/ErbNewArguments: + Enabled: true + +Lint/FlipFlop: + Enabled: true + +Lint/FloatComparison: + Enabled: true + +Lint/FloatOutOfRange: + Enabled: true + +Lint/IdentityComparison: + Enabled: true + +Lint/ImplicitStringConcatenation: + Enabled: true + +Lint/IneffectiveAccessModifier: + Enabled: true + +Lint/InheritException: + Enabled: true + EnforcedStyle: runtime_error + +Lint/InterpolationCheck: + Enabled: true + +Lint/ItWithoutArgumentsInBlock: + Enabled: true + +Lint/LiteralAsCondition: + Enabled: true + +Lint/LiteralAssignmentInCondition: + Enabled: true + +Lint/LiteralInInterpolation: + Enabled: true + +Lint/Loop: + Enabled: true + +Lint/MissingCopEnableDirective: + Enabled: true + MaximumRangeSize: .inf + +Lint/MixedCaseRange: + Enabled: true + +Lint/MixedRegexpCaptureTypes: + Enabled: true + +Lint/MultipleComparison: + Enabled: true + +Lint/NestedMethodDefinition: + Enabled: true + +Lint/NestedPercentLiteral: + Enabled: true + +Lint/NextWithoutAccumulator: + Enabled: true + +Lint/NonDeterministicRequireOrder: + Enabled: true + +Lint/NonLocalExitFromIterator: + Enabled: true + +Lint/NumberedParameterAssignment: + Enabled: true + +Lint/OrAssignmentToConstant: + Enabled: true + +Lint/OrderedMagicComments: + Enabled: true + +Lint/OutOfRangeRegexpRef: + Enabled: true + +Lint/ParenthesesAsGroupedExpression: + Enabled: true + +Lint/PercentSymbolArray: + Enabled: true + +Lint/RaiseException: + Enabled: true + +Lint/RandOne: + Enabled: true + +Lint/RedundantRegexpQuantifiers: + Enabled: true + +Lint/RedundantRequireStatement: + Enabled: true + +Lint/RedundantSplatExpansion: + Enabled: true + +Lint/RedundantStringCoercion: + Enabled: true + +Lint/RedundantWithIndex: + Enabled: true + +Lint/RedundantWithObject: + Enabled: true + +Lint/RefinementImportMethods: + Enabled: true + +Lint/RegexpAsCondition: + Enabled: true + +Lint/RequireParentheses: + Enabled: true + +Lint/RequireRangeParentheses: + Enabled: true + +Lint/RequireRelativeSelfPath: + Enabled: true + +Lint/RescueType: + Enabled: true + +Lint/ReturnInVoidContext: + Enabled: true + +Lint/SafeNavigationChain: + Enabled: true + AllowedMethods: + - present? + - blank? + - presence + - try + - try! + +Lint/SafeNavigationConsistency: + Enabled: true + AllowedMethods: + - present? + - blank? + - presence + - try + - try! + +Lint/SafeNavigationWithEmpty: + Enabled: true + +Lint/SelfAssignment: + Enabled: true + +Lint/ShadowedArgument: + Enabled: true + IgnoreImplicitReferences: false + +Lint/ShadowedException: + Enabled: true + +Lint/SymbolConversion: + Enabled: true + +Lint/Syntax: + Enabled: true + +Lint/TopLevelReturnWithArgument: + Enabled: true + +Lint/TrailingCommaInAttributeDeclaration: + Enabled: true + +Lint/TripleQuotes: + Enabled: true + +Lint/UnifiedInteger: + Enabled: true + +Lint/UriEscapeUnescape: + Enabled: true + +Lint/UriRegexp: + Enabled: true + +Lint/UselessAssignment: + Enabled: true + +Lint/UselessRescue: + Enabled: true + +Lint/UselessRuby2Keywords: + Enabled: true + +Lint/UselessSetterCall: + Enabled: true + +Lint/UselessTimes: + Enabled: true + +Migration/DepartmentName: + Enabled: true + +Naming/BinaryOperatorParameterName: + Enabled: true + +Naming/BlockParameterName: + Enabled: true + MinNameLength: 1 + AllowNamesEndingInNumbers: true + AllowedNames: [] + ForbiddenNames: [] + +Naming/ConstantName: + Enabled: true + +Naming/HeredocDelimiterCase: + Enabled: true + EnforcedStyle: uppercase + +Naming/VariableName: + Enabled: true + EnforcedStyle: snake_case + +Security/CompoundHash: + Enabled: true + +Security/JSONLoad: + Enabled: true + +Security/Open: + Enabled: true + +Security/YAMLLoad: + Enabled: true + +Style/Alias: + Enabled: true + EnforcedStyle: prefer_alias_method + +Style/AndOr: + Enabled: true + +Style/ArrayJoin: + Enabled: true + +Style/Attr: + Enabled: true + +Style/BarePercentLiterals: + Enabled: true + EnforcedStyle: bare_percent + +Style/BeginBlock: + Enabled: true + +Style/BlockComments: + Enabled: true + +Style/CharacterLiteral: + Enabled: true + +Style/ClassCheck: + Enabled: true + EnforcedStyle: is_a? + +Style/ClassEqualityComparison: + Enabled: true + +Style/ClassMethods: + Enabled: true + +Style/ColonMethodCall: + Enabled: true + +Style/ColonMethodDefinition: + Enabled: true + +Style/CommandLiteral: + Enabled: true + EnforcedStyle: mixed + AllowInnerBackticks: false + +Style/ComparableClamp: + Enabled: true + +Style/DefWithParentheses: + Enabled: true + +Style/Dir: + Enabled: true + +Style/DirEmpty: + Enabled: true + +Style/EachForSimpleLoop: + Enabled: true + +Style/EachWithObject: + Enabled: true + +Style/EmptyBlockParameter: + Enabled: true + +Style/EmptyCaseCondition: + Enabled: true + +Style/EmptyElse: + Enabled: true + AllowComments: true + EnforcedStyle: both + +Style/EmptyLambdaParameter: + Enabled: true + +Style/EmptyLiteral: + Enabled: true + +Style/EmptyMethod: + Enabled: true + EnforcedStyle: expanded + +Style/Encoding: + Enabled: true + +Style/EndBlock: + Enabled: true + +Style/ExactRegexpMatch: + Enabled: true +Style/FileRead: + Enabled: true + +Style/FileWrite: + Enabled: true + +Style/For: + Enabled: true + EnforcedStyle: each + +Style/HashConversion: + Enabled: true + +Style/HashExcept: + Enabled: true + +Style/IdenticalConditionalBranches: + Enabled: true + +Style/IfInsideElse: + Enabled: true + +Style/IfUnlessModifierOfIfUnless: + Enabled: true + +Style/IfWithBooleanLiteralBranches: + Enabled: true + +Style/IfWithSemicolon: + Enabled: true + +Style/KeywordParametersOrder: + Enabled: true + +Style/LambdaCall: + Enabled: true + EnforcedStyle: call + +Style/LineEndConcatenation: + Enabled: true + +Style/MapCompactWithConditionalBlock: + Enabled: true + +Style/MethodCallWithoutArgsParentheses: + Enabled: true + AllowedMethods: [] + +Style/MissingRespondToMissing: + Enabled: true + +Style/MixinGrouping: + Enabled: true + EnforcedStyle: separated + +Style/MixinUsage: + Enabled: true + +Style/MultilineIfModifier: + Enabled: true + +Style/MultilineIfThen: + Enabled: true + +Style/MultilineMemoization: + Enabled: true + EnforcedStyle: keyword + +Style/MultilineWhenThen: + Enabled: true + +Style/NegatedWhile: + Enabled: true + +Style/NestedFileDirname: + Enabled: true + +Style/NestedModifier: + Enabled: true + +Style/NestedParenthesizedCalls: + Enabled: true + AllowedMethods: + - be + - be_a + - be_an + - be_between + - be_falsey + - be_kind_of + - be_instance_of + - be_truthy + - be_within + - eq + - eql + - end_with + - include + - match + - raise_error + - respond_to + - start_with + +Style/NestedTernaryOperator: + Enabled: true + +Style/NilComparison: + Enabled: true + EnforcedStyle: predicate + +Style/NilLambda: + Enabled: true + +Style/NonNilCheck: + Enabled: true + IncludeSemanticChanges: false + +Style/Not: + Enabled: true + +Style/NumericLiteralPrefix: + Enabled: true + EnforcedOctalStyle: zero_with_o + +Style/OneLineConditional: + Enabled: true + +Style/OptionalArguments: + Enabled: true + +Style/ParenthesesAroundCondition: + Enabled: true + AllowSafeAssignment: true + AllowInMultilineConditions: false + +Style/Proc: + Enabled: true + +Style/QuotedSymbols: + Enabled: true + EnforcedStyle: same_as_string_literals + +Style/RandomWithOffset: + Enabled: true + +Style/RedundantArrayConstructor: + Enabled: true + +Style/RedundantAssignment: + Enabled: true + +Style/RedundantBegin: + Enabled: true + +Style/RedundantCondition: + Enabled: true + +Style/RedundantConditional: + Enabled: true + +Style/RedundantCurrentDirectoryInPath: + Enabled: true + +Style/RedundantDoubleSplatHashBraces: + Enabled: true + +Style/RedundantException: + Enabled: true + +Style/RedundantFileExtensionInRequire: + Enabled: true + +Style/RedundantFreeze: + Enabled: true + +Style/RedundantHeredocDelimiterQuotes: + Enabled: true + +Style/RedundantInterpolation: + Enabled: true + +Style/RedundantLineContinuation: + Enabled: true + +Style/RedundantPercentQ: + Enabled: true + +Style/RedundantRegexpArgument: + Enabled: true + +Style/RedundantRegexpCharacterClass: + Enabled: true + +Style/RedundantRegexpConstructor: + Enabled: true + +Style/RedundantRegexpEscape: + Enabled: true + +Style/RedundantReturn: + Enabled: true + AllowMultipleReturnValues: false + +Style/RedundantSelf: + Enabled: true + +Style/RedundantSort: + Enabled: true + +Style/RedundantSortBy: + Enabled: true + +Style/RedundantStringEscape: + Enabled: true + +Style/RescueStandardError: + Enabled: true + EnforcedStyle: implicit + +Style/Sample: + Enabled: true + +Style/SelfAssignment: + Enabled: true + +Style/Semicolon: + Enabled: true + AllowAsExpressionSeparator: true + +Style/SingleLineMethods: + Enabled: true + AllowIfMethodIsEmpty: false + +Style/SlicingWithRange: + Enabled: true + +Style/StabbyLambdaParentheses: + Enabled: true + EnforcedStyle: require_parentheses + +Style/StderrPuts: + Enabled: true + +Style/StringChars: + Enabled: true + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes + ConsistentQuotesInMultiline: false + +Style/StringLiteralsInInterpolation: + Enabled: true + EnforcedStyle: double_quotes + +Style/Strip: + Enabled: true + +Style/SuperArguments: + Enabled: true + +Style/SuperWithArgsParentheses: + Enabled: true + +Style/SymbolLiteral: + Enabled: true + +Style/TrailingBodyOnClass: + Enabled: true + +Style/TrailingBodyOnMethodDefinition: + Enabled: true + +Style/TrailingBodyOnModule: + Enabled: true + +Style/TrailingCommaInArguments: + Enabled: true + EnforcedStyleForMultiline: no_comma + +Style/TrailingCommaInArrayLiteral: + Enabled: true + EnforcedStyleForMultiline: no_comma + +Style/TrailingCommaInBlockArgs: + Enabled: true + +Style/TrailingCommaInHashLiteral: + Enabled: true + EnforcedStyleForMultiline: no_comma + +Style/TrailingMethodEndStatement: + Enabled: true + +Style/UnlessElse: + Enabled: true + +Style/UnlessLogicalOperators: + Enabled: true + EnforcedStyle: forbid_mixed_logical_operators + +Style/UnpackFirst: + Enabled: true + +Style/VariableInterpolation: + Enabled: true + +Style/WhenThen: + Enabled: true + +Style/WhileUntilDo: + Enabled: true + +Style/YAMLFileRead: + Enabled: true diff --git a/Gemfile b/Gemfile index e502a1b5..705e55eb 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "yard" +gem "rubocop", "~> 1.65", require: false group :test do gem "http" diff --git a/lib/rage/cli.rb b/lib/rage/cli.rb index 72905501..ec4175a0 100644 --- a/lib/rage/cli.rb +++ b/lib/rage/cli.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "thor" require "rack" @@ -40,7 +41,7 @@ def server ::Iodine.start end - desc 'routes', 'List all routes.' + desc "routes", "List all routes." option :grep, aliases: "-g", desc: "Filter routes by pattern" option :help, aliases: "-h", desc: "Show this message." def routes diff --git a/lib/rage/controller/api.rb b/lib/rage/controller/api.rb index aa4f937b..8f079ed0 100644 --- a/lib/rage/controller/api.rb +++ b/lib/rage/controller/api.rb @@ -7,6 +7,7 @@ class << self # registering means defining a new method which calls the action, makes additional calls (e.g. before actions) and # sends a correct response down to the server; # returns the name of the newly defined method; + # rubocop:disable Layout/IndentationWidth, Layout/EndAlignment, Layout/HeredocIndentation def __register_action(action) raise Rage::Errors::RouterError, "The action '#{action}' could not be found for #{self}" unless method_defined?(action) @@ -80,7 +81,7 @@ def __register_action(action) wrap_parameters_chunk = if __wrap_parameters_key <<~RUBY wrap_key = self.class.__wrap_parameters_key - if !@__params.key?(wrap_key) && @__env['CONTENT_TYPE'] + if !@__params.key?(wrap_key) && @__env["CONTENT_TYPE"] wrap_options = self.class.__wrap_parameters_options wrapped_params = if wrap_options[:include].any? @__params.slice(*wrap_options[:include]) @@ -94,7 +95,7 @@ def __register_action(action) RUBY end - class_eval <<~RUBY, __FILE__, __LINE__ + 1 + class_eval <<~RUBY, __FILE__, __LINE__ + 1 def __run_#{action} #{if activerecord_loaded <<~RUBY @@ -137,6 +138,7 @@ def __run_#{action} end RUBY end + # rubocop:enable all # @private attr_writer :__before_actions, :__after_actions, :__rescue_handlers @@ -236,7 +238,7 @@ def before_action(action_name = nil, **opts, &block) if @__before_actions.nil? @__before_actions = [action] - elsif i = @__before_actions.find_index { |a| a[:name] == action_name } + elsif (i = @__before_actions.find_index { |a| a[:name] == action_name }) @__before_actions[i] = action else @__before_actions << action @@ -262,7 +264,7 @@ def after_action(action_name = nil, **opts, &block) if @__after_actions.nil? @__after_actions = [action] - elsif i = @__after_actions.find_index { |a| a[:name] == action_name } + elsif (i = @__after_actions.find_index { |a| a[:name] == action_name }) @__after_actions[i] = action else @__after_actions << action @@ -312,7 +314,7 @@ def skip_before_action(action_name, only: nil, except: nil) # wrap_parameters :user, exclude: %i[address] def wrap_parameters(key, include: [], exclude: []) @__wrap_parameters_key = key - @__wrap_parameters_options = {include:, exclude:} + @__wrap_parameters_options = { include:, exclude: } end private @@ -325,7 +327,7 @@ def prepare_action_params(action_name = nil, **opts, &block) raise ArgumentError, "No handler provided. Pass the `action_name` parameter or provide a block." end - _only, _except, _if, _unless = opts.values_at(:only, :except, :if, :unless) + _only, _except, _if, _unless = opts.values_at(:only, :except, :if, :unless) action = { name: action_name, diff --git a/lib/rage/cookies.rb b/lib/rage/cookies.rb index db083b83..70772531 100644 --- a/lib/rage/cookies.rb +++ b/lib/rage/cookies.rb @@ -97,7 +97,7 @@ def []=(key, value) return end - if domain = value[:domain] + if (domain = value[:domain]) host = @env["HTTP_HOST"] _domain = if domain.is_a?(String) @@ -141,7 +141,7 @@ def request_cookies return @request_cookies if @parsed @parsed = true - if cookie_header = @env["HTTP_COOKIE"] + if (cookie_header = @env["HTTP_COOKIE"]) cookie_header.split(/; */n).each do |cookie| next if cookie.empty? key, value = cookie.split("=", 2).yield_self { |k, _| [k.to_sym, _] } @@ -193,7 +193,7 @@ def load(value) nil rescue RbNaCl::CryptoError i ||= 0 - if box = fallback_boxes[i] + if (box = fallback_boxes[i]) i += 1 retry end @@ -230,10 +230,8 @@ def primary_box end def fallback_boxes - @fallback_boxes ||= begin - Rage.config.fallback_secret_key_base.map do |key| - RbNaCl::SimpleBox.from_secret_key(RbNaCl::Hash.blake2b(key, digest_size: 32, salt: SALT)) - end + @fallback_boxes ||= Rage.config.fallback_secret_key_base.map do |key| + RbNaCl::SimpleBox.from_secret_key(RbNaCl::Hash.blake2b(key, digest_size: 32, salt: SALT)) end end end # class << self diff --git a/lib/rage/ext/active_record/connection_pool.rb b/lib/rage/ext/active_record/connection_pool.rb index afc25f6b..c817bafa 100644 --- a/lib/rage/ext/active_record/connection_pool.rb +++ b/lib/rage/ext/active_record/connection_pool.rb @@ -97,7 +97,7 @@ def connection # Signal that the fiber is finished with the current connection and it can be returned to the pool. def release_connection(owner = Fiber.current) - if conn = @__in_use.delete(owner) + if (conn = @__in_use.delete(owner)) conn.__idle_since = Process.clock_gettime(Process::CLOCK_MONOTONIC) @__connections << conn Iodine.publish("ext:ar-connection-released", "", Iodine::PubSub::PROCESS) if @__blocked.length > 0 diff --git a/lib/rage/fiber.rb b/lib/rage/fiber.rb index ceedff5f..dda50921 100644 --- a/lib/rage/fiber.rb +++ b/lib/rage/fiber.rb @@ -2,7 +2,7 @@ ## # Rage provides a simple and efficient API to wait on several instances of IO at the same time - {Fiber.await}. -# +# # Let's say we have the following controller: # ```ruby # class UsersController < RageController::API @@ -34,7 +34,7 @@ # end # ``` # With this change, if each request takes 1 second to execute, the total execution time will still be 1 second. -# +# # ## Creating fibers # Many developers see fibers as "lightweight threads" that should be used in conjunction with fiber pools, the same way we use thread pools for threads.
# Instead, it makes sense to think of fibers as regular Ruby objects. We don't use a pool of arrays when we need to create an array - we create a new object and let Ruby and the GC do their job.
@@ -68,7 +68,7 @@ def __set_id @__rage_id = object_id.to_s end - # @private + # @private def __get_id @__rage_id end diff --git a/lib/rage/fiber_scheduler.rb b/lib/rage/fiber_scheduler.rb index 3934df70..b577012d 100644 --- a/lib/rage/fiber_scheduler.rb +++ b/lib/rage/fiber_scheduler.rb @@ -66,7 +66,7 @@ def kernel_sleep(duration = nil) end # TODO: GC works a little strange with this closure; - # + # # def timeout_after(duration, exception_class = Timeout::Error, *exception_arguments, &block) # fiber, block_status = Fiber.current, :running # ::Iodine.run_after((duration * 1000).to_i) do diff --git a/lib/rage/logger/json_formatter.rb b/lib/rage/logger/json_formatter.rb index 32a98a3f..c36af518 100644 --- a/lib/rage/logger/json_formatter.rb +++ b/lib/rage/logger/json_formatter.rb @@ -15,7 +15,7 @@ def call(severity, timestamp, _, message) context.each { |k, v| context_msg << "\"#{k}\":#{v.to_json}," } end - if final = logger[:final] + if (final = logger[:final]) params, env = final[:params], final[:env] if params && params[:controller] return "{\"tags\":[\"#{tags[0]}\"],\"timestamp\":\"#{timestamp}\",\"pid\":\"#{@pid}\",\"level\":\"info\",\"method\":\"#{env["REQUEST_METHOD"]}\",\"path\":\"#{env["PATH_INFO"]}\",\"controller\":\"#{Rage::Router::Util.path_to_name(params[:controller])}\",\"action\":\"#{params[:action]}\",#{context_msg}\"status\":#{final[:response][0]},\"duration\":#{final[:duration]}}\n" diff --git a/lib/rage/logger/logger.rb b/lib/rage/logger/logger.rb index bb6c7d3d..338e4139 100644 --- a/lib/rage/logger/logger.rb +++ b/lib/rage/logger/logger.rb @@ -8,7 +8,7 @@ # [fecbba0735355738] timestamp=2023-10-19T11:12:56+00:00 pid=1825 level=info message=hello # ``` # In the log entry above, `timestamp`, `pid`, `level`, and `message` are keys, while `fecbba0735355738` is a tag. -# +# # Use {tagged} to add custom tags to an entry: # ```ruby # Rage.logger.tagged("ApiCall") do diff --git a/lib/rage/logger/text_formatter.rb b/lib/rage/logger/text_formatter.rb index c03bc7a8..7d5b8dc0 100644 --- a/lib/rage/logger/text_formatter.rb +++ b/lib/rage/logger/text_formatter.rb @@ -15,7 +15,7 @@ def call(severity, timestamp, _, message) context.each { |k, v| context_msg << "#{k}=#{v} " } end - if final = logger[:final] + if (final = logger[:final]) params, env = final[:params], final[:env] if params && params[:controller] return "[#{tags[0]}] timestamp=#{timestamp} pid=#{@pid} level=info method=#{env["REQUEST_METHOD"]} path=#{env["PATH_INFO"]} controller=#{Rage::Router::Util.path_to_name(params[:controller])} action=#{params[:action]} #{context_msg}status=#{final[:response][0]} duration=#{final[:duration]}\n" diff --git a/lib/rage/middleware/cors.rb b/lib/rage/middleware/cors.rb index 28bd1b5d..7f06c1dd 100644 --- a/lib/rage/middleware/cors.rb +++ b/lib/rage/middleware/cors.rb @@ -19,7 +19,7 @@ def call(env) response ensure - if !$! && origin = @cors_check.call(env) + if !$! && (origin = @cors_check.call(env)) headers = response[1] headers["Access-Control-Allow-Origin"] = origin if @origins != "*" @@ -99,7 +99,7 @@ def allow(*origins, methods: "*", allow_headers: "*", expose_headers: nil, max_a def create_headers headers = { "Access-Control-Allow-Origin" => "", - "Access-Control-Allow-Methods" => @methods, + "Access-Control-Allow-Methods" => @methods } if @allow_headers diff --git a/lib/rage/middleware/reloader.rb b/lib/rage/middleware/reloader.rb index 1609e107..28026c95 100644 --- a/lib/rage/middleware/reloader.rb +++ b/lib/rage/middleware/reloader.rb @@ -8,7 +8,7 @@ def initialize(app) def call(env) Rage.code_loader.reload @app.call(env) -rescue Exception => e + rescue Exception => e exception_str = "#{e.class} (#{e.message}):\n#{e.backtrace.join("\n")}" puts(exception_str) [500, {}, [exception_str]] diff --git a/lib/rage/params_parser.rb b/lib/rage/params_parser.rb index 7bdca349..87747416 100644 --- a/lib/rage/params_parser.rb +++ b/lib/rage/params_parser.rb @@ -29,7 +29,7 @@ def self.prepare(env, url_params) url_params end - rescue => e + rescue raise Rage::Errors::BadRequest end diff --git a/lib/rage/router/backend.rb b/lib/rage/router/backend.rb index 4fdf0c52..b05fc68e 100644 --- a/lib/rage/router/backend.rb +++ b/lib/rage/router/backend.rb @@ -51,7 +51,7 @@ def mount(path, handler, methods) def on(method, path, handler, constraints: {}, defaults: nil) raise "Path could not be empty" if path&.empty? - if match_index = (path =~ OPTIONAL_PARAM_REGEXP) + if (match_index = (path =~ OPTIONAL_PARAM_REGEXP)) raise ArgumentError, "Optional Parameter has to be the last parameter of the path" if path.length != match_index + $&.length path_full = path.sub(OPTIONAL_PARAM_REGEXP, "/#{$1}") @@ -200,11 +200,9 @@ def __on(method, path, handler, constraints, defaults, meta) end @routes.each do |existing_route| - if ( - existing_route[:method] == method && - existing_route[:pattern] == pattern && - existing_route[:constraints] == constraints - ) + if existing_route[:method] == method && + existing_route[:pattern] == pattern && + existing_route[:constraints] == constraints raise ArgumentError, "Method '#{method}' already declared for route '#{pattern}' with constraints '#{constraints.inspect}'" end end diff --git a/lib/rage/router/constrainer.rb b/lib/rage/router/constrainer.rb index ae0be4ef..9685818f 100644 --- a/lib/rage/router/constrainer.rb +++ b/lib/rage/router/constrainer.rb @@ -73,7 +73,7 @@ def __build_derive_constraints if key == :host lines << " host: env['HTTP_HOST'.freeze]," else - raise ArgumentError, 'unknown non-custom strategy for compiling constraint derivation function' + raise ArgumentError, "unknown non-custom strategy for compiling constraint derivation function" end else lines << " #{strategy.name}: @strategies[#{key}].derive_constraint(env)," diff --git a/lib/rage/router/dsl.rb b/lib/rage/router/dsl.rb index 6340ddfb..be3bb60f 100644 --- a/lib/rage/router/dsl.rb +++ b/lib/rage/router/dsl.rb @@ -213,7 +213,7 @@ def namespace(path, **options, &block) @path_prefixes << path_prefix @module_prefixes << module_prefix - instance_eval &block + instance_eval(&block) @path_prefixes.pop @module_prefixes.pop @@ -253,7 +253,7 @@ def scope(opts, &block) @module_prefixes << opts[:module] if opts[:module] @controllers << opts[:controller] if opts[:controller] - instance_eval &block + instance_eval(&block) @path_prefixes.pop if opts[:path] @module_prefixes.pop if opts[:module] @@ -269,7 +269,7 @@ def scope(opts, &block) # end def defaults(defaults, &block) @defaults << defaults - instance_eval &block + instance_eval(&block) @defaults.pop end @@ -282,7 +282,7 @@ def defaults(defaults, &block) # end def controller(controller, &block) @controllers << controller - instance_eval &block + instance_eval(&block) @controllers.pop end @@ -297,7 +297,7 @@ def controller(controller, &block) def collection(&block) orig_path_prefixes = @path_prefixes @path_prefixes = @path_prefixes[0...-1] if @path_prefixes.last&.start_with?(":") - instance_eval &block + instance_eval(&block) @path_prefixes = orig_path_prefixes end @@ -317,7 +317,7 @@ def member(&block) @path_prefixes = [*@path_prefixes[0...-1], ":#{member_prefix}"] end - instance_eval &block + instance_eval(&block) @path_prefixes = orig_path_prefixes end diff --git a/lib/rage/router/dsl_plugins/legacy_hash_notation.rb b/lib/rage/router/dsl_plugins/legacy_hash_notation.rb index 1a0713a9..7fb6c4ad 100644 --- a/lib/rage/router/dsl_plugins/legacy_hash_notation.rb +++ b/lib/rage/router/dsl_plugins/legacy_hash_notation.rb @@ -40,7 +40,7 @@ def mount(*args, **kwargs) options = kwargs.except(app).merge(at: at) super(app, **options) else - super(*args, **kwargs) + super end end end diff --git a/lib/rage/router/dsl_plugins/legacy_root_notation.rb b/lib/rage/router/dsl_plugins/legacy_root_notation.rb index 82946f46..09eb72fd 100644 --- a/lib/rage/router/dsl_plugins/legacy_root_notation.rb +++ b/lib/rage/router/dsl_plugins/legacy_root_notation.rb @@ -8,7 +8,7 @@ def root(*args, **kwargs) if args.length == 1 && args[0].is_a?(String) && kwargs.empty? super(to: args[0]) else - super(*args, **kwargs) + super end end end diff --git a/lib/rage/router/handler_storage.rb b/lib/rage/router/handler_storage.rb index cd348e57..1a895d7d 100644 --- a/lib/rage/router/handler_storage.rb +++ b/lib/rage/router/handler_storage.rb @@ -64,7 +64,7 @@ def compile_create_params_object(param_keys, defaults, meta) end end - eval "->(param_values) { { #{lines.join(',')} } }" + eval "->(param_values) { { #{lines.join(",")} } }" end def get_handler_matching_constraints(_derived_constraints) diff --git a/lib/rage/sidekiq_session.rb b/lib/rage/sidekiq_session.rb index 90cd8cb4..00912fcf 100644 --- a/lib/rage/sidekiq_session.rb +++ b/lib/rage/sidekiq_session.rb @@ -13,7 +13,7 @@ class Rage::SidekiqSession SESSION_KEY = "rage.sidekiq.session" def self.with_session(env) - env["rack.session"] = session = self.new(env) + env["rack.session"] = session = new(env) response = yield if session.changed diff --git a/spec/controller/api/before_actions_spec.rb b/spec/controller/api/before_actions_spec.rb index 521d3167..a15518e5 100644 --- a/spec/controller/api/before_actions_spec.rb +++ b/spec/controller/api/before_actions_spec.rb @@ -261,7 +261,7 @@ def setup_3 end end - it 'raises an error if the action name is missing and a block is not pass' do + it "raises an error if the action name is missing and a block is not pass" do expect do Class.new(RageController::API) { before_action only: [:index] @@ -269,7 +269,7 @@ def setup_3 end.to raise_error("No handler provided. Pass the `action_name` parameter or provide a block.") end - context 'case 8' do + context "case 8" do let(:klass) { ControllerApiBeforeActionsSpec::TestController8 } it "correctly runs before actions" do diff --git a/spec/controller/api/session_spec.rb b/spec/controller/api/session_spec.rb index a5459698..46e1b2c0 100644 --- a/spec/controller/api/session_spec.rb +++ b/spec/controller/api/session_spec.rb @@ -83,7 +83,7 @@ context "when writing a session" do let(:new_session) do _, session_cookie = subject.headers.find { |k, _| k == "Set-Cookie" } - session_value = session_cookie.match(/#{Rage::Session::KEY}\=(\S+);/)[1] + session_value = session_cookie.match(/#{Rage::Session::KEY}=(\S+);/)[1] Rage::Cookies::EncryptedJar.load( Rack::Utils.unescape(session_value, Encoding::UTF_8) diff --git a/spec/controller/api/wrap_parameters_spec.rb b/spec/controller/api/wrap_parameters_spec.rb index cc044a8a..b1076e34 100644 --- a/spec/controller/api/wrap_parameters_spec.rb +++ b/spec/controller/api/wrap_parameters_spec.rb @@ -1,6 +1,6 @@ RSpec.describe RageController::API do - describe 'parameters wrapping logic' do - context 'when parameters wrapper is not declared' do + describe "parameters wrapping logic" do + context "when parameters wrapper is not declared" do let(:controller) do Class.new(RageController::API) do def index @@ -10,15 +10,15 @@ def index end it "doesn't wrap the parameters" do - initial_params = {param: :value} - expected_result = {param: :value} + initial_params = { param: :value } + expected_result = { param: :value } - response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"}) + response = run_action(controller, :index, params: initial_params, env: { "CONTENT_TYPE" => "application/json" }) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end - context 'when parameters wrapper is declared without options' do + context "when parameters wrapper is declared without options" do let(:controller) do Class.new(RageController::API) do wrap_parameters :root @@ -30,26 +30,26 @@ def index end context "and wrapping root doesn't conflict with parameter key" do - context 'and CONTENT_TYPE header is blank' do + context "and CONTENT_TYPE header is blank" do it "doesn't wrap the parameters into a nested hash" do - initial_params = {param: :value} - expected_result = {param: :value} + initial_params = { param: :value } + expected_result = { param: :value } response = run_action(controller, :index, params: initial_params) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end - context 'and CONTENT_TYPE header is present' do - it 'wraps the parameters into a nested hash without the reserved params' do - initial_params = {param: :value, action: :action, controller: :controller} - expected_result = {param: :value, action: :action, controller: :controller, root: {param: :value}} + context "and CONTENT_TYPE header is present" do + it "wraps the parameters into a nested hash without the reserved params" do + initial_params = { param: :value, action: :action, controller: :controller } + expected_result = { param: :value, action: :action, controller: :controller, root: { param: :value } } response = run_action( controller, :index, params: initial_params, - env: {'CONTENT_TYPE' => "application/json"} + env: { "CONTENT_TYPE" => "application/json" } ) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) @@ -57,18 +57,18 @@ def index end end - context 'and wrapping root conflicts with parameter key' do + context "and wrapping root conflicts with parameter key" do it "doesn't wrap the parameters into a nested hash" do - initial_params = {root: :value, param: :value, action: :action, controller: :controller} - expected_result = {root: :value, param: :value, action: :action, controller: :controller} + initial_params = { root: :value, param: :value, action: :action, controller: :controller } + expected_result = { root: :value, param: :value, action: :action, controller: :controller } - response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"}) + response = run_action(controller, :index, params: initial_params, env: { "CONTENT_TYPE" => "application/json" }) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end end - context 'when parameters wrapper is declared with :include option' do + context "when parameters wrapper is declared with :include option" do let(:controller) do Class.new(RageController::API) do wrap_parameters :root, include: %i[param_a param_b] @@ -79,35 +79,35 @@ def index end end - context 'and params to include are present in request' do - it 'wraps the params that are set to be included' do - initial_params = {param_a: :value, param_b: :value, param_c: :value, action: :action, controller: :controller} + context "and params to include are present in request" do + it "wraps the params that are set to be included" do + initial_params = { param_a: :value, param_b: :value, param_c: :value, action: :action, controller: :controller } expected_result = { param_a: :value, param_b: :value, param_c: :value, action: :action, controller: :controller, - root: {param_a: :value, param_b: :value} + root: { param_a: :value, param_b: :value } } - response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"}) + response = run_action(controller, :index, params: initial_params, env: { "CONTENT_TYPE" => "application/json" }) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end context "and params to include aren't present in request" do - it 'adds empty hash under wrapping key to params' do - initial_params = {param_c: :value, action: :action, controller: :controller} - expected_result = {param_c: :value, action: :action, controller: :controller, root: {}} + it "adds empty hash under wrapping key to params" do + initial_params = { param_c: :value, action: :action, controller: :controller } + expected_result = { param_c: :value, action: :action, controller: :controller, root: {} } - response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"}) + response = run_action(controller, :index, params: initial_params, env: { "CONTENT_TYPE" => "application/json" }) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end end - context 'when parameters wrapper is declared with :exclude option' do + context "when parameters wrapper is declared with :exclude option" do let(:controller) do Class.new(RageController::API) do wrap_parameters :root, exclude: %i[param_a param_b] @@ -118,35 +118,35 @@ def index end end - context 'and params to exclude are present in request' do - it 'wraps the params except those that are set to be excluded and those that need to be excluded by default' do - initial_params = {param_a: :value, param_b: :value, param_c: :value, action: :action, controller: :controller} + context "and params to exclude are present in request" do + it "wraps the params except those that are set to be excluded and those that need to be excluded by default" do + initial_params = { param_a: :value, param_b: :value, param_c: :value, action: :action, controller: :controller } expected_result = { param_a: :value, param_b: :value, param_c: :value, action: :action, controller: :controller, - root: {param_c: :value} + root: { param_c: :value } } - response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"}) + response = run_action(controller, :index, params: initial_params, env: { "CONTENT_TYPE" => "application/json" }) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end context "and params to exclude aren't present in request" do - it 'wraps the params except those that need to be excluded by default ' do - initial_params = {param_c: :value, action: :action, controller: :controller} - expected_result = {param_c: :value, action: :action, controller: :controller, root: {param_c: :value}} + it "wraps the params except those that need to be excluded by default " do + initial_params = { param_c: :value, action: :action, controller: :controller } + expected_result = { param_c: :value, action: :action, controller: :controller, root: { param_c: :value } } - response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"}) + response = run_action(controller, :index, params: initial_params, env: { "CONTENT_TYPE" => "application/json" }) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end end - context 'when parameters wrapper is declared with both :exclude and :include options' do + context "when parameters wrapper is declared with both :exclude and :include options" do let(:controller) do Class.new(RageController::API) do wrap_parameters :root, exclude: %i[param_a], include: %i[param_a] @@ -157,16 +157,16 @@ def index end end - it 'wraps the params using the :include option' do - initial_params = {param_a: :value, param_b: :value} - expected_result = {param_a: :value, param_b: :value, root: {param_a: :value}} + it "wraps the params using the :include option" do + initial_params = { param_a: :value, param_b: :value } + expected_result = { param_a: :value, param_b: :value, root: { param_a: :value } } - response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"}) + response = run_action(controller, :index, params: initial_params, env: { "CONTENT_TYPE" => "application/json" }) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end - context 'controller inheritance' do + context "controller inheritance" do let(:grandchild_controller) do Class.new(child_controller) do def index @@ -175,7 +175,7 @@ def index end end - context 'when parameters wrapper is declared in parent controller' do + context "when parameters wrapper is declared in parent controller" do let(:parent_controller) do Class.new(RageController::API) do wrap_parameters :parent_root, include: %i[parent_param] @@ -186,8 +186,8 @@ def index end end - context 'and parameters wrapper is declared in child controller' do - context 'and child wrapper is declared without options' do + context "and parameters wrapper is declared in child controller" do + context "and child wrapper is declared without options" do let(:child_controller) do Class.new(parent_controller) do wrap_parameters :child_root @@ -198,39 +198,39 @@ def index end end - let(:initial_params) { {parent_param: :value, child_param: :value} } + let(:initial_params) { { parent_param: :value, child_param: :value } } let(:expected_result) do { parent_param: :value, child_param: :value, - child_root: {parent_param: :value, child_param: :value} + child_root: { parent_param: :value, child_param: :value } } end - it 'wraps params of child controller using wrapping key of child controller without options' do + it "wraps params of child controller using wrapping key of child controller without options" do response = run_action( child_controller, :index, params: initial_params, - env: {'CONTENT_TYPE' => "application/json"} + env: { "CONTENT_TYPE" => "application/json" } ) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end - it 'wraps params of grandchild controller using wrapping key of child controller without options' do + it "wraps params of grandchild controller using wrapping key of child controller without options" do response = run_action( grandchild_controller, :index, params: initial_params, - env: {'CONTENT_TYPE' => "application/json"} + env: { "CONTENT_TYPE" => "application/json" } ) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end end - context 'and child wrapper is defined with options' do + context "and child wrapper is defined with options" do let(:child_controller) do Class.new(parent_controller) do wrap_parameters :child_root, include: %i[child_param] @@ -241,26 +241,26 @@ def index end end - let(:initial_params) { {parent_param: :value, child_param: :value} } - let(:expected_result) { {parent_param: :value, child_param: :value, child_root: {child_param: :value}} } + let(:initial_params) { { parent_param: :value, child_param: :value } } + let(:expected_result) { { parent_param: :value, child_param: :value, child_root: { child_param: :value } } } - it 'wraps params of child controller using wrapping key and options of child controller' do + it "wraps params of child controller using wrapping key and options of child controller" do response = run_action( child_controller, :index, params: initial_params, - env: {'CONTENT_TYPE' => "application/json"} + env: { "CONTENT_TYPE" => "application/json" } ) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end - it 'wraps params of grandchild controller using wrapping key and options of child controller' do + it "wraps params of grandchild controller using wrapping key and options of child controller" do response = run_action( grandchild_controller, :index, params: initial_params, - env: {'CONTENT_TYPE' => "application/json"} + env: { "CONTENT_TYPE" => "application/json" } ) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) @@ -268,7 +268,7 @@ def index end end - context 'and parameters wrapper is not declared in child controller' do + context "and parameters wrapper is not declared in child controller" do let(:child_controller) do Class.new(parent_controller) do def index @@ -277,26 +277,26 @@ def index end end - let(:initial_params) { {parent_param: :value, child_param: :value} } - let(:expected_result) { {parent_param: :value, child_param: :value, parent_root: {parent_param: :value}} } + let(:initial_params) { { parent_param: :value, child_param: :value } } + let(:expected_result) { { parent_param: :value, child_param: :value, parent_root: { parent_param: :value } } } - it 'wraps params of child controller using wrapping key and options of parent controller' do + it "wraps params of child controller using wrapping key and options of parent controller" do response = run_action( child_controller, :index, params: initial_params, - env: {'CONTENT_TYPE' => "application/json"} + env: { "CONTENT_TYPE" => "application/json" } ) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) end - it 'wraps params of grandchild controller using wrapping key and options of parent controller' do + it "wraps params of grandchild controller using wrapping key and options of parent controller" do response = run_action( child_controller, :index, params: initial_params, - env: {'CONTENT_TYPE' => "application/json"} + env: { "CONTENT_TYPE" => "application/json" } ) expect(response).to match([200, instance_of(Hash), [expected_result.to_json]]) diff --git a/spec/cors_middleware_spec.rb b/spec/cors_middleware_spec.rb index c94b0aed..beb7c9f8 100644 --- a/spec/cors_middleware_spec.rb +++ b/spec/cors_middleware_spec.rb @@ -412,7 +412,7 @@ end it "sets correct headers" do - expect(subject).to eq([200, { "Access-Control-Allow-Origin" => "http://localhost:3000", "Access-Control-Allow-Credentials"=>"true" }, ["test response"]]) + expect(subject).to eq([200, { "Access-Control-Allow-Origin" => "http://localhost:3000", "Access-Control-Allow-Credentials" => "true" }, ["test response"]]) end context "with preflight requests" do @@ -444,7 +444,7 @@ end it "sets correct headers" do - expect(subject).to eq([200, { "Access-Control-Allow-Origin" => "http://localhost:3000", "Access-Control-Expose-Headers"=>"*" }, ["test response"]]) + expect(subject).to eq([200, { "Access-Control-Allow-Origin" => "http://localhost:3000", "Access-Control-Expose-Headers" => "*" }, ["test response"]]) end context "with preflight requests" do diff --git a/spec/fiber_scheduler_spec.rb b/spec/fiber_scheduler_spec.rb index 89f48c1c..ba6d2267 100644 --- a/spec/fiber_scheduler_spec.rb +++ b/spec/fiber_scheduler_spec.rb @@ -74,7 +74,7 @@ within_reactor do Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 1) do |http| request = Net::HTTP::Get.new(uri) - response = http.request(request) + http.request(request) end raise "test failed!" @@ -163,7 +163,7 @@ port: uri.port, username: uri.user, password: uri.password, - database: uri.path[1..-1] + database: uri.path[1..] ) end @@ -318,7 +318,7 @@ Fiber.await(Fiber.schedule { sleep(1) }) end - -> { expect((1..1.5)).to include(result) } + -> { expect((1..1.5)).to include(result) } end end end diff --git a/spec/fiber_spec.rb b/spec/fiber_spec.rb index f5a7fc1e..5d814f13 100644 --- a/spec/fiber_spec.rb +++ b/spec/fiber_spec.rb @@ -20,7 +20,7 @@ within_reactor do result = Fiber.await([ Fiber.schedule { 10 }, - Fiber.schedule { 20 }, + Fiber.schedule { 20 } ]) -> { expect(result).to eq([10, 20]) } @@ -33,7 +33,7 @@ result = Fiber.await([ Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/long-http-get?i=#{num[0]}")) }, - Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/long-http-get?i=#{num[1]}")) }, + Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/long-http-get?i=#{num[1]}")) } ]) -> { expect(result).to eq([(num[0] * 10).to_s, (num[1] * 10).to_s]) } @@ -48,7 +48,7 @@ Fiber.schedule { 111 }, Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/long-http-get?i=#{num[0]}")) }, Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/long-http-get?i=#{num[1]}")) }, - Fiber.schedule { 222 }, + Fiber.schedule { 222 } ]) -> { expect(result).to eq([111, (num[0] * 10).to_s, (num[1] * 10).to_s, 222]) } @@ -61,7 +61,7 @@ result = Fiber.await([ Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/long-http-get?i=#{num[0]}")) }, - Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=#{num[1]}")) }, + Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=#{num[1]}")) } ]) -> { expect(result).to eq([(num[0] * 10).to_s, (num[1] * 10).to_s]) } @@ -74,7 +74,7 @@ Fiber.await([ Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/long-http-get?i=#{rand}")) }, Fiber.schedule { sleep(1) }, - Fiber.schedule { sleep(1) }, + Fiber.schedule { sleep(1) } ]) end @@ -100,7 +100,7 @@ num = rand result = Fiber.await([ - Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=#{num}")) }, + Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=#{num}")) } ]) -> { expect(result).to eq([(num * 10).to_s]) } diff --git a/spec/integration/test_app/app/controllers/async_controller.rb b/spec/integration/test_app/app/controllers/async_controller.rb index eed3eb79..ec751907 100644 --- a/spec/integration/test_app/app/controllers/async_controller.rb +++ b/spec/integration/test_app/app/controllers/async_controller.rb @@ -2,11 +2,11 @@ class AsyncController < RageController::API def sum - i1 = Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=#{5.7}")) - i2 = Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=#{3.4}")) + i1 = Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=5.7")) + i2 = Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=3.4")) i3, i4 = Fiber.await([ - Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=#{1.8}")) }, - Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=#{8.3}")) }, + Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=1.8")) }, + Fiber.schedule { Net::HTTP.get(URI("#{ENV["TEST_HTTP_URL"]}/instant-http-get?i=8.3")) } ]) render plain: i1.to_i + i2.to_i + i3.to_i + i4.to_i diff --git a/spec/integration/test_app/config/routes.rb b/spec/integration/test_app/config/routes.rb index e863aea0..efc58bbe 100644 --- a/spec/integration/test_app/config/routes.rb +++ b/spec/integration/test_app/config/routes.rb @@ -24,5 +24,5 @@ get "logs/custom", to: "logs#custom" get "logs/fiber", to: "logs#fiber" - mount -> (_) { [200, {}, ""] }, at: "/admin" + mount ->(_) { [200, {}, ""] }, at: "/admin" end diff --git a/spec/logger_spec.rb b/spec/logger_spec.rb index d4394d6c..4290bbe3 100644 --- a/spec/logger_spec.rb +++ b/spec/logger_spec.rb @@ -237,7 +237,7 @@ it "doesn't add a string entry" do expect( - subject.fatal "this is a test message" + subject.fatal("this is a test message") ).to be(false) end diff --git a/spec/multi_application_spec.rb b/spec/multi_application_spec.rb index 81fa0d16..ed777985 100644 --- a/spec/multi_application_spec.rb +++ b/spec/multi_application_spec.rb @@ -3,7 +3,7 @@ RSpec.describe "Rage Multi App" do subject { Rage.multi_application.call(env) } - let(:env) { { "PATH_INFO"=> "/" } } + let(:env) { { "PATH_INFO" => "/" } } let(:rails_verifier) { double } let(:rage_verifier) { double } @@ -51,7 +51,7 @@ end context "with Rails internal request" do - let(:env) { { "PATH_INFO"=> "/rails/action_mailbox" } } + let(:env) { { "PATH_INFO" => "/rails/action_mailbox" } } let(:rage_response) { [200, {}, []] } let(:rails_response) { :test_rails_response } diff --git a/spec/params_parser_spec.rb b/spec/params_parser_spec.rb index d92a5eb5..68df9b24 100644 --- a/spec/params_parser_spec.rb +++ b/spec/params_parser_spec.rb @@ -29,7 +29,7 @@ "IODINE_HAS_BODY" => !!body, "QUERY_STRING" => query_string, "CONTENT_TYPE" => content_type, - "rack.input" => rack_input, + "rack.input" => rack_input } end diff --git a/spec/router/controller_spec.rb b/spec/router/controller_spec.rb index 81fd424b..d36d5707 100644 --- a/spec/router/controller_spec.rb +++ b/spec/router/controller_spec.rb @@ -85,26 +85,25 @@ def get_all end RSpec.describe Rage::Request do - describe 'Request' do - describe '#headers' do - it 'returns request headers with both meta-variable and original names' do + describe "Request" do + describe "#headers" do + it "returns request headers with both meta-variable and original names" do env = { - 'CONTENT_TYPE' => 'application/json', - 'HTTP_ACCEPT_LANGUAGE' => 'en-US', - 'HTTP_SOME_OTHER_HEADER' => 'value', - 'HTTP_ACCEPT_LANGUAGE' => 'en-US', - 'HTTP_VARY' => 'Accept-Language', + "CONTENT_TYPE" => "application/json", + "HTTP_SOME_OTHER_HEADER" => "value", + "HTTP_ACCEPT_LANGUAGE" => "en-US", + "HTTP_VARY" => "Accept-Language" } request = Rage::Request.new(env) - expect(request.headers['Content-Type']).to eq('application/json') - expect(request.headers['CONTENT_TYPE']).to eq('application/json') - expect(request.headers['Accept-Language']).to eq('en-US') - expect(request.headers['HTTP_ACCEPT_LANGUAGE']).to eq('en-US') - expect(request.headers['non-existent-header']).to be_nil - expect(request.headers['vary']).to eq('Accept-Language') - expect(request.headers['VARY']).to eq('Accept-Language') - expect(request.headers['Vary']).to eq('Accept-Language') + expect(request.headers["Content-Type"]).to eq("application/json") + expect(request.headers["CONTENT_TYPE"]).to eq("application/json") + expect(request.headers["Accept-Language"]).to eq("en-US") + expect(request.headers["HTTP_ACCEPT_LANGUAGE"]).to eq("en-US") + expect(request.headers["non-existent-header"]).to be_nil + expect(request.headers["vary"]).to eq("Accept-Language") + expect(request.headers["VARY"]).to eq("Accept-Language") + expect(request.headers["Vary"]).to eq("Accept-Language") end end end diff --git a/spec/rspec/rspec_spec.rb b/spec/rspec/rspec_spec.rb index 4066eb3b..8f595137 100644 --- a/spec/rspec/rspec_spec.rb +++ b/spec/rspec/rspec_spec.rb @@ -10,8 +10,8 @@ def params_action def headers_action if request.headers["Content-Type"] == "text/plain" && - request.headers["Cache-Control"] == "max-age=604800, must-revalidate" && - request.headers["Last-Modified"] == "Sun, 03 Sep 2017 00:17:02 GMT" + request.headers["Cache-Control"] == "max-age=604800, must-revalidate" && + request.headers["Last-Modified"] == "Sun, 03 Sep 2017 00:17:02 GMT" head :ok else head :bad_request @@ -21,7 +21,7 @@ def headers_action def fibers_action i = Fiber.await([ Fiber.schedule { 10 }, - Fiber.schedule { 11 }, + Fiber.schedule { 11 } ]) render plain: i.sum diff --git a/spec/setup_spec.rb b/spec/setup_spec.rb index 2126e147..1ec5ad95 100644 --- a/spec/setup_spec.rb +++ b/spec/setup_spec.rb @@ -1,16 +1,16 @@ -RSpec.describe 'Setup' do - let(:valid_env) { 'development' } - let(:invalid_env) { 'develop' } - let(:setup_file) { File.expand_path('../lib/rage/setup.rb', __dir__) } +RSpec.describe "Setup" do + let(:valid_env) { "development" } + let(:invalid_env) { "develop" } + let(:setup_file) { File.expand_path("../lib/rage/setup.rb", __dir__) } before do allow(Rage).to receive(:env).and_return(env) - allow(Rage).to receive(:root).and_return(File.expand_path('..', __dir__)) + allow(Rage).to receive(:root).and_return(File.expand_path("..", __dir__)) allow(Rage).to receive_message_chain(:code_loader, :setup).and_return(true) allow(Iodine).to receive(:patch_rack).and_return(true) end - context 'when environment name is valid' do + context "when environment name is valid" do let(:env) { valid_env } before do @@ -18,17 +18,17 @@ allow_any_instance_of(Object).to receive(:require_relative).with("#{Rage.root}/config/routes").and_return(true) end - it 'loads the environment without error' do + it "loads the environment without error" do expect { load setup_file }.not_to raise_error end end - context 'when environment name is invalid' do + context "when environment name is invalid" do let(:env) { invalid_env } - it 'raises a custom error with a meaningful message' do - expect { load setup_file } - .to raise_error(LoadError, "The <#{invalid_env}> environment could not be found. Please check the environment name.") + it "raises a custom error with a meaningful message" do + expect { load setup_file }. + to raise_error(LoadError, "The <#{invalid_env}> environment could not be found. Please check the environment name.") end end end From 44b57b1c2690fd57071cf2a9c22816bd8b538243 Mon Sep 17 00:00:00 2001 From: Roman Samoilov <2270393+rsamoilov@users.noreply.github.com> Date: Fri, 26 Jul 2024 19:22:59 +0100 Subject: [PATCH 2/3] Add CI step --- .github/workflows/main.yml | 12 ++++++++++++ .rubocop.yml | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 510e93f8..63644c96 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,6 +32,18 @@ jobs: TEST_MYSQL_URL: ${{ secrets.TEST_MYSQL_URL }} ENABLE_EXTERNAL_TESTS: ${{ secrets.ENABLE_EXTERNAL_TESTS }} run: bundle exec rake + linter: + runs-on: ubuntu-latest + name: Code Style + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ruby + bundler-cache: true + - name: Run Linter + run: bundle exec rubocop docs: runs-on: ubuntu-latest name: Docs diff --git a/.rubocop.yml b/.rubocop.yml index 3a7ddc7c..02faf18d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,7 @@ AllCops: TargetRubyVersion: 3.1 - Exclude: [] + Exclude: + - vendor/bundle/**/* DisabledByDefault: true SuggestExtensions: false From 716e1e9fa6f37c77b14eb91d3d6a9e8e495d89c9 Mon Sep 17 00:00:00 2001 From: Roman Samoilov <2270393+rsamoilov@users.noreply.github.com> Date: Fri, 26 Jul 2024 19:36:06 +0100 Subject: [PATCH 3/3] Remove Rubocop config from release files --- rage.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rage.gemspec b/rage.gemspec index 794ca2ef..e095bb11 100644 --- a/rage.gemspec +++ b/rage.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(__dir__) do `git ls-files -z`.split("\x0").reject do |f| - (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor]) + (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor .rubocop]) end end spec.bindir = "exe"