From f3ad954fcf0cacc11c1968ea203c93f293dc2267 Mon Sep 17 00:00:00 2001 From: James Stonehill Date: Sun, 25 Aug 2019 13:31:18 +0100 Subject: [PATCH] Add Rubocop --- .rubocop.yml | 30 +++++++ .rubocop_todo.yml | 85 +++++++++++++++++++ .travis.yml | 3 + Gemfile | 2 + Rakefile | 4 +- api_error_handler.gemspec | 20 ++++- bin/console | 1 + lib/api_error_handler.rb | 6 +- lib/api_error_handler/action_controller.rb | 6 +- lib/api_error_handler/error_id_generator.rb | 7 +- lib/api_error_handler/error_reporter.rb | 14 ++- lib/api_error_handler/errors.rb | 2 + .../serializers/base_serializer.rb | 6 +- lib/api_error_handler/serializers/json.rb | 4 +- lib/api_error_handler/serializers/json_api.rb | 4 +- lib/api_error_handler/serializers/xml.rb | 6 +- lib/api_error_handler/version.rb | 2 + .../error_id_generator_spec.rb | 2 + spec/api_error_handler/error_reporter_spec.rb | 4 +- .../serializers/json_api_spec.rb | 4 +- .../serializers/json_spec.rb | 4 +- .../api_error_handler/serializers/xml_spec.rb | 2 + spec/api_error_handler_spec.rb | 2 + spec/spec_helper.rb | 2 + 24 files changed, 202 insertions(+), 20 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..74bedbb --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,30 @@ +inherit_from: .rubocop_todo.yml + +AllCops: + TargetRubyVersion: 2.3 + Exclude: + - 'vendor/**/*' + - 'rails_*/**/*' + +Style/StringLiterals: + EnforcedStyle: double_quotes + +Metrics/LineLength: + Max: 100 + +Style/MethodCallWithoutArgsParentheses: + Enabled: false + +Style/TrailingCommaInArrayLiteral: + EnforcedStyleForMultiline: comma + +Style/TrailingCommaInHashLiteral: + EnforcedStyleForMultiline: comma + +Metrics/BlockLength: + Exclude: + - 'spec/**/*.rb' + - 'api_error_handler.gemspec' + +Style/Documentation: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..b30eee0 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,85 @@ +# This configuration was generated by +# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 100` +# on 2019-08-25 13:23:25 +0100 using RuboCop version 0.74.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: TreatCommentsAsGroupSeparators, Include. +# Include: **/*.gemspec +Gemspec/OrderedDependencies: + Exclude: + - 'api_error_handler.gemspec' + +# Offense count: 1 +# Cop supports --auto-correct. +Layout/ClosingParenthesisIndentation: + Exclude: + - 'spec/api_error_handler/serializers/xml_spec.rb' + +# Offense count: 2 +# Configuration parameters: Max. +Metrics/AbcSize: + Exclude: + - 'lib/api_error_handler.rb' + - 'lib/api_error_handler/error_reporter.rb' + +# Offense count: 1 +# Configuration parameters: Max. +Metrics/CyclomaticComplexity: + Exclude: + - 'lib/api_error_handler/error_reporter.rb' + +# Offense count: 3 +# Configuration parameters: CountComments, Max, ExcludedMethods. +Metrics/MethodLength: + Exclude: + - 'lib/api_error_handler.rb' + - 'lib/api_error_handler/error_reporter.rb' + - 'lib/api_error_handler/error_id_generator.rb' + - 'lib/api_error_handler/serializers/json_api.rb' + +# Offense count: 1 +# Configuration parameters: Max. +Metrics/PerceivedComplexity: + Exclude: + - 'lib/api_error_handler/error_reporter.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Style/ExpandPathArguments: + Exclude: + - 'api_error_handler.gemspec' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: implicit, explicit +Style/RescueStandardError: + Exclude: + - 'lib/api_error_handler.rb' + - 'spec/api_error_handler/serializers/json_api_spec.rb' + - 'spec/api_error_handler/serializers/json_spec.rb' + - 'spec/api_error_handler/serializers/xml_spec.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInArrayLiteral: + Exclude: + - 'lib/api_error_handler/serializers/json_api.rb' + - 'spec/api_error_handler/serializers/json_api_spec.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInHashLiteral: + Exclude: + - 'lib/api_error_handler.rb' + - 'lib/api_error_handler/serializers/json.rb' + - 'lib/api_error_handler/serializers/json_api.rb' diff --git a/.travis.yml b/.travis.yml index 0ecd017..81518de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,9 @@ matrix: - rvm: 2.5.1 gemfile: ./rails_6_test_app/Gemfile script: cd rails_6_test_app && bundle exec rspec + - rvm: 2.3.8 + gemfile: Gemfile + script: bundle exec rubocop - rvm: 2.3.8 gemfile: Gemfile script: bundle exec rspec diff --git a/Gemfile b/Gemfile index 32ad83f..e1c4a1c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source "https://rubygems.org" # Specify your gem's dependencies in api_error_handler.gemspec diff --git a/Rakefile b/Rakefile index b7e9ed5..b6ae734 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,8 @@ +# frozen_string_literal: true + require "bundler/gem_tasks" require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) -task :default => :spec +task default: :spec diff --git a/api_error_handler.gemspec b/api_error_handler.gemspec index b63611c..a84232b 100644 --- a/api_error_handler.gemspec +++ b/api_error_handler.gemspec @@ -1,3 +1,4 @@ +# frozen_string_literal: true lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) @@ -8,16 +9,26 @@ Gem::Specification.new do |spec| spec.version = ApiErrorHandler::VERSION spec.authors = ["James Stonehill"] spec.email = ["james.stonehill@gmail.com"] - spec.required_ruby_version = '~> 2.3' + spec.required_ruby_version = "~> 2.3" + + spec.summary = <<~SUMMARY + A gem that helps you easily handle exceptions in your Rails API and return + informative responses to the client. + SUMMARY + + spec.description = <<~DESCRIPTION + A gem that helps you easily handle exceptions in your Ruby on Rails API and + return informative responses to the client by serializing exceptions into JSON + and other popular API formats and returning a response with a status code that + makes sense based on the exception. + DESCRIPTION - spec.summary = %q{A gem that helps you easily handle exceptions in your Rails API and return informative responses to the client.} - spec.description = %q{A gem that helps you easily handle exceptions in your Ruby on Rails API and return informative responses to the client by serializing exceptions into JSON and other popular API formats and returning a response with a status code that makes sense based on the exception.} spec.homepage = "https://github.com/jamesstonehill/api_error_handler" spec.license = "MIT" # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. - spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do + spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|rails_.+)/}) } end spec.bindir = "exe" @@ -31,4 +42,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 2.0" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec-rails", "~> 3.0" + spec.add_development_dependency "rubocop", "~> 0.74.0" end diff --git a/bin/console b/bin/console index b7900bc..a177b3d 100755 --- a/bin/console +++ b/bin/console @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true require "bundler/setup" require "api_error_handler" diff --git a/lib/api_error_handler.rb b/lib/api_error_handler.rb index af608c7..5250d89 100644 --- a/lib/api_error_handler.rb +++ b/lib/api_error_handler.rb @@ -1,8 +1,12 @@ +# frozen_string_literal: true + require_relative "./api_error_handler/version" require_relative "./api_error_handler/action_controller" require_relative "./api_error_handler/error_id_generator" require_relative "./api_error_handler/error_reporter" -Dir[File.join(__dir__, 'api_error_handler', 'serializers', "*.rb")].each { |file| require file } +Dir[File.join(__dir__, "api_error_handler", "serializers", "*.rb")].each do |file| + require file +end module ApiErrorHandler SERIALIZERS_BY_FORMAT = { diff --git a/lib/api_error_handler/action_controller.rb b/lib/api_error_handler/action_controller.rb index 175a149..f3fd0c1 100644 --- a/lib/api_error_handler/action_controller.rb +++ b/lib/api_error_handler/action_controller.rb @@ -1,5 +1,7 @@ -require 'active_support/lazy_load_hooks' -require 'action_controller' +# frozen_string_literal: true + +require "active_support/lazy_load_hooks" +require "action_controller" ActiveSupport.on_load :action_controller do ::ActionController::Base.send :extend, ApiErrorHandler diff --git a/lib/api_error_handler/error_id_generator.rb b/lib/api_error_handler/error_id_generator.rb index e4aa285..c747c24 100644 --- a/lib/api_error_handler/error_id_generator.rb +++ b/lib/api_error_handler/error_id_generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "securerandom" require_relative "./errors" @@ -11,7 +13,10 @@ def self.run(error_id_option) elsif error_id_option.nil? nil else - raise(InvalidOptionError, "Unable to handle `#{error_id_option}` as argument for the `:error_id` option.") + raise( + InvalidOptionError, + "Unable to handle `#{error_id_option}` as argument for the `:error_id` option." + ) end end end diff --git a/lib/api_error_handler/error_reporter.rb b/lib/api_error_handler/error_reporter.rb index 45a8357..d6de713 100644 --- a/lib/api_error_handler/error_reporter.rb +++ b/lib/api_error_handler/error_reporter.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "logger" require_relative "./errors" @@ -23,14 +25,22 @@ def report(error, error_id: nil) extra = error_id ? { error_id: error_id } : {} Raven.capture_exception(error, extra: extra) else - raise(InvalidOptionError, "`#{@strategy.inspect}` is an invalid argument for the `:error_id` option.") + raise( + InvalidOptionError, + "`#{@strategy.inspect}` is an invalid argument for the `:error_id` option." + ) end end private def raise_dependency_error(missing_constant:) - raise MissingDependencyError, "You selected the #{@strategy.inspect} error reporter option but the #{missing_constant} constant is not defined. If you wish to use this error reporting option you must have the #{@strategy} client gem installed." + raise MissingDependencyError, <<~MESSAGE + You selected the #{@strategy.inspect} error reporter option but the + #{missing_constant} constant is not defined. If you wish to use this + error reporting option you must have the #{@strategy} client gem + installed. + MESSAGE end end end diff --git a/lib/api_error_handler/errors.rb b/lib/api_error_handler/errors.rb index 3d20458..eff776c 100644 --- a/lib/api_error_handler/errors.rb +++ b/lib/api_error_handler/errors.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ApiErrorHandler class Error < StandardError; end diff --git a/lib/api_error_handler/serializers/base_serializer.rb b/lib/api_error_handler/serializers/base_serializer.rb index f9ce1ac..5902d41 100644 --- a/lib/api_error_handler/serializers/base_serializer.rb +++ b/lib/api_error_handler/serializers/base_serializer.rb @@ -1,9 +1,11 @@ -require 'rack/utils' +# frozen_string_literal: true + +require "rack/utils" module ApiErrorHandler module Serializers class BaseSerializer - DEFAULT_STATUS_CODE = "500".freeze + DEFAULT_STATUS_CODE = "500" def initialize(error, status) @error = error diff --git a/lib/api_error_handler/serializers/json.rb b/lib/api_error_handler/serializers/json.rb index 49e6960..cf907bf 100644 --- a/lib/api_error_handler/serializers/json.rb +++ b/lib/api_error_handler/serializers/json.rb @@ -1,4 +1,6 @@ -require_relative './base_serializer' +# frozen_string_literal: true + +require_relative "./base_serializer" module ApiErrorHandler module Serializers diff --git a/lib/api_error_handler/serializers/json_api.rb b/lib/api_error_handler/serializers/json_api.rb index f41ced8..0854b2c 100644 --- a/lib/api_error_handler/serializers/json_api.rb +++ b/lib/api_error_handler/serializers/json_api.rb @@ -1,4 +1,6 @@ -require_relative './base_serializer' +# frozen_string_literal: true + +require_relative "./base_serializer" module ApiErrorHandler module Serializers diff --git a/lib/api_error_handler/serializers/xml.rb b/lib/api_error_handler/serializers/xml.rb index 2086872..16d245d 100644 --- a/lib/api_error_handler/serializers/xml.rb +++ b/lib/api_error_handler/serializers/xml.rb @@ -1,5 +1,7 @@ -require 'active_support/core_ext/hash/conversions' -require_relative './base_serializer' +# frozen_string_literal: true + +require "active_support/core_ext/hash/conversions" +require_relative "./base_serializer" module ApiErrorHandler module Serializers diff --git a/lib/api_error_handler/version.rb b/lib/api_error_handler/version.rb index f2ccdbb..53f41f8 100644 --- a/lib/api_error_handler/version.rb +++ b/lib/api_error_handler/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ApiErrorHandler VERSION = "0.1.0" end diff --git a/spec/api_error_handler/error_id_generator_spec.rb b/spec/api_error_handler/error_id_generator_spec.rb index 694fbbe..6cfb4c5 100644 --- a/spec/api_error_handler/error_id_generator_spec.rb +++ b/spec/api_error_handler/error_id_generator_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative "../../lib/api_error_handler/error_id_generator" RSpec.describe ApiErrorHandler::ErrorIdGenerator do diff --git a/spec/api_error_handler/error_reporter_spec.rb b/spec/api_error_handler/error_reporter_spec.rb index f28c057..a9fc088 100644 --- a/spec/api_error_handler/error_reporter_spec.rb +++ b/spec/api_error_handler/error_reporter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative "../../lib/api_error_handler/error_reporter" RSpec.describe ApiErrorHandler::ErrorReporter do @@ -70,7 +72,7 @@ it "Reports to Honeybadger without an error id" do stub_const("Raven", double) - expect(Raven).to receive(:capture_exception).with(error, extra: { }) + expect(Raven).to receive(:capture_exception).with(error, extra: {}) reporter.report(error) end diff --git a/spec/api_error_handler/serializers/json_api_spec.rb b/spec/api_error_handler/serializers/json_api_spec.rb index 3fc3aa4..d2243b8 100644 --- a/spec/api_error_handler/serializers/json_api_spec.rb +++ b/spec/api_error_handler/serializers/json_api_spec.rb @@ -1,4 +1,6 @@ -require 'json' +# frozen_string_literal: true + +require "json" require_relative "../../../lib/api_error_handler/serializers/json_api" RSpec.describe ApiErrorHandler::Serializers::JsonApi do diff --git a/spec/api_error_handler/serializers/json_spec.rb b/spec/api_error_handler/serializers/json_spec.rb index f51977b..c1a5c0f 100644 --- a/spec/api_error_handler/serializers/json_spec.rb +++ b/spec/api_error_handler/serializers/json_spec.rb @@ -1,4 +1,6 @@ -require 'json' +# frozen_string_literal: true + +require "json" require_relative "../../../lib/api_error_handler/serializers/json" RSpec.describe ApiErrorHandler::Serializers::Json do diff --git a/spec/api_error_handler/serializers/xml_spec.rb b/spec/api_error_handler/serializers/xml_spec.rb index f2979e4..f28b6f4 100644 --- a/spec/api_error_handler/serializers/xml_spec.rb +++ b/spec/api_error_handler/serializers/xml_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative "../../../lib/api_error_handler/serializers/xml" RSpec.describe ApiErrorHandler::Serializers::Xml do diff --git a/spec/api_error_handler_spec.rb b/spec/api_error_handler_spec.rb index 512e90e..a193749 100644 --- a/spec/api_error_handler_spec.rb +++ b/spec/api_error_handler_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ApiErrorHandler do it "has a version number" do expect(ApiErrorHandler::VERSION).not_to be nil diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d5b585a..162a403 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "bundler/setup" require "api_error_handler"