Skip to content

Commit

Permalink
Merge pull request #7 from jamesstonehill/add-rubocop
Browse files Browse the repository at this point in the history
Add Rubocop
  • Loading branch information
jamesstonehill authored Aug 26, 2019
2 parents 793523e + f3ad954 commit 5e97978
Show file tree
Hide file tree
Showing 24 changed files with 202 additions and 20 deletions.
30 changes: 30 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
85 changes: 85 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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'
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in api_error_handler.gemspec
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -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
20 changes: 16 additions & 4 deletions api_error_handler.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
Expand All @@ -8,16 +9,26 @@ Gem::Specification.new do |spec|
spec.version = ApiErrorHandler::VERSION
spec.authors = ["James Stonehill"]
spec.email = ["[email protected]"]
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"
Expand All @@ -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
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "api_error_handler"
Expand Down
6 changes: 5 additions & 1 deletion lib/api_error_handler.rb
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
6 changes: 4 additions & 2 deletions lib/api_error_handler/action_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/api_error_handler/error_id_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "securerandom"
require_relative "./errors"

Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions lib/api_error_handler/error_reporter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "logger"
require_relative "./errors"

Expand All @@ -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
2 changes: 2 additions & 0 deletions lib/api_error_handler/errors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApiErrorHandler
class Error < StandardError; end

Expand Down
6 changes: 4 additions & 2 deletions lib/api_error_handler/serializers/base_serializer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/api_error_handler/serializers/json.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require_relative './base_serializer'
# frozen_string_literal: true

require_relative "./base_serializer"

module ApiErrorHandler
module Serializers
Expand Down
4 changes: 3 additions & 1 deletion lib/api_error_handler/serializers/json_api.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require_relative './base_serializer'
# frozen_string_literal: true

require_relative "./base_serializer"

module ApiErrorHandler
module Serializers
Expand Down
6 changes: 4 additions & 2 deletions lib/api_error_handler/serializers/xml.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/api_error_handler/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApiErrorHandler
VERSION = "0.1.0"
end
2 changes: 2 additions & 0 deletions spec/api_error_handler/error_id_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "../../lib/api_error_handler/error_id_generator"

RSpec.describe ApiErrorHandler::ErrorIdGenerator do
Expand Down
4 changes: 3 additions & 1 deletion spec/api_error_handler/error_reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "../../lib/api_error_handler/error_reporter"

RSpec.describe ApiErrorHandler::ErrorReporter do
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion spec/api_error_handler/serializers/json_api_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion spec/api_error_handler/serializers/json_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/api_error_handler/serializers/xml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "../../../lib/api_error_handler/serializers/xml"

RSpec.describe ApiErrorHandler::Serializers::Xml do
Expand Down
2 changes: 2 additions & 0 deletions spec/api_error_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/setup"
require "api_error_handler"

Expand Down

0 comments on commit 5e97978

Please sign in to comment.