Skip to content

Commit 3fcb64d

Browse files
author
Sebastian Wilgosz
authored
Merge pull request #16 from driggl/14-configure-rubocop
Refactor to meet rubocop expectations #14
2 parents 66b665d + b346993 commit 3fcb64d

19 files changed

+151
-98
lines changed

Gemfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
source "https://rubygems.org"
1+
# frozen_string_literal: true
22

3-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3+
source 'https://rubygems.org'
4+
5+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
46

57
# Specify your gem's dependencies in jsonapi_errors_handler.gemspec
68
gemspec

Rakefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
require "bundler/gem_tasks"
2-
require "rspec/core/rake_task"
1+
# frozen_string_literal: true
2+
3+
require 'bundler/gem_tasks'
4+
require 'rspec/core/rake_task'
35

46
RSpec::Core::RakeTask.new(:spec)
57

6-
task :default => :spec
8+
task default: :spec

bin/console

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

3-
require "bundler/setup"
4-
require "jsonapi_errors_handler"
4+
require 'bundler/setup'
5+
require 'jsonapi_errors_handler'
56

67
# You can add fixtures and/or initialization code here to make experimenting
78
# with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "jsonapi_errors_handler"
1011
# require "pry"
1112
# Pry.start
1213

13-
require "irb"
14+
require 'irb'
1415
IRB.start(__FILE__)

jsonapi_errors_handler.gemspec

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
1-
lib = File.expand_path("../lib", __FILE__)
1+
# frozen_string_literal: true
2+
3+
lib = File.expand_path('lib', __dir__)
24
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3-
require "jsonapi_errors_handler/version"
5+
require 'jsonapi_errors_handler/version'
46

57
Gem::Specification.new do |spec|
6-
spec.name = "jsonapi_errors_handler"
8+
spec.name = 'jsonapi_errors_handler'
79
spec.version = JsonapiErrorsHandler::VERSION
8-
spec.authors = ["Sebastian Wilgosz"]
9-
spec.email = ["[email protected]"]
10+
spec.authors = ['Sebastian Wilgosz']
11+
spec.email = ['[email protected]']
1012

11-
spec.summary = %q{A JSON API error handler for ruby applications.}
12-
spec.description = %q{This gem is a convienient wrapper for your application errors. It allows you to map any error to a nicely formatted standard HTTP error response. }
13-
spec.homepage = "https://github.com/driggl/jsonapi_errors_handler"
14-
spec.license = "MIT"
13+
spec.summary = 'A JSON API error handler for ruby applications.'
14+
spec.description = <<~STRING
15+
This gem is a convienient wrapper for your application errors.
16+
It allows you to map any error to a nicely formatted standard HTTP error response.
17+
STRING
18+
spec.homepage = 'https://github.com/driggl/jsonapi_errors_handler'
19+
spec.license = 'MIT'
1520

1621
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
1722
# to allow pushing to a single host or delete this section to allow pushing to any host.
1823
if spec.respond_to?(:metadata)
1924
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
2025

21-
spec.metadata["homepage_uri"] = spec.homepage
22-
spec.metadata["source_code_uri"] = "https://github.com/driggl/jsonapi_errors_handler"
26+
spec.metadata['homepage_uri'] = spec.homepage
27+
spec.metadata['source_code_uri'] = 'https://github.com/driggl/jsonapi_errors_handler'
2328
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
2429
else
25-
raise "RubyGems 2.0 or newer is required to protect against " \
26-
"public gem pushes."
30+
raise 'RubyGems 2.0 or newer is required to protect against ' \
31+
'public gem pushes.'
2732
end
2833

2934
# Specify which files should be added to the gem when it is released.
3035
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31-
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
36+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
3237
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
3338
end
34-
spec.bindir = "exe"
39+
spec.bindir = 'exe'
3540
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36-
spec.require_paths = ["lib"]
41+
spec.require_paths = ['lib']
3742

38-
spec.add_development_dependency "bundler", "~> 1.16"
39-
spec.add_development_dependency "rake", "~> 10.0"
40-
spec.add_development_dependency "rspec", "~> 3.0"
43+
spec.add_development_dependency 'bundler', '~> 1.16'
44+
spec.add_development_dependency 'rake', '~> 10.0'
45+
spec.add_development_dependency 'rspec', '~> 3.0'
4146
end

lib/jsonapi_errors_handler.rb

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1+
# frozen_string_literal: true
2+
13
require 'irb'
2-
require "jsonapi_errors_handler/version"
3-
require "jsonapi_errors_handler/errors"
4-
require "jsonapi_errors_handler/error_mapper"
5-
require "jsonapi_errors_handler/error_serializer"
4+
require 'jsonapi_errors_handler/version'
5+
require 'jsonapi_errors_handler/errors'
6+
require 'jsonapi_errors_handler/error_mapper'
7+
require 'jsonapi_errors_handler/error_serializer'
68

79
module JsonapiErrorsHandler
810
def self.included(base)
911
base.class_eval do
10-
ErrorMapper.map_errors!({
12+
ErrorMapper.map_errors!(
1113
'JsonapiErrorsHandler::Errors::Invalid' => 'JsonapiErrorsHandler::Errors::Invalid',
1214
'JsonapiErrorsHandler::Errors::Forbidden' => 'JsonapiErrorsHandler::Errors::Forbidden',
1315
'JsonapiErrorsHandler::Errors::NotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
1416
'JsonapiErrorsHandler::Errors::Unauthorized' => 'JsonapiErrorsHandler::Errors::Unauthorized'
15-
})
17+
)
18+
end
19+
end
1620

17-
def handle_error(e)
18-
mapped = map_error(e)
19-
log_error(e) if respond_to?(:log_error) && !mapped
20-
mapped ||= ::JsonapiErrorsHandler::Errors::StandardError.new
21-
render_error(mapped)
22-
end
21+
def handle_error(error)
22+
mapped = map_error(error)
23+
log_error(error) if respond_to?(:log_error) && !mapped
24+
mapped ||= ::JsonapiErrorsHandler::Errors::StandardError.new
25+
render_error(mapped)
26+
end
2327

24-
def map_error(e)
25-
error_klass = e.class.name
26-
return nil if !ErrorMapper.mapped_error?(error_klass)
27-
Object.const_get(ErrorMapper.mapped_errors[error_klass]).new
28-
end
28+
def map_error(error)
29+
error_klass = error.class.name
30+
return nil unless ErrorMapper.mapped_error?(error_klass)
2931

30-
def render_error(error)
31-
render json: ::JsonapiErrorsHandler::ErrorSerializer.new(error), status: error.status
32-
end
33-
end
32+
Object.const_get(ErrorMapper.mapped_errors[error_klass]).new
33+
end
34+
35+
def render_error(error)
36+
render json: ::JsonapiErrorsHandler::ErrorSerializer.new(error), status: error.status
3437
end
3538
end

lib/jsonapi_errors_handler/error_mapper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
# frozen_string_literal: true
2+
13
module JsonapiErrorsHandler
24
class ErrorMapper
35
@@mapped_errors = {}
46
def self.mapped_errors
57
@@mapped_errors
68
end
79

8-
def self.map_errors!(errors_hash={})
10+
def self.map_errors!(errors_hash = {})
911
@@mapped_errors.merge!(errors_hash)
1012
end
1113

1214
def self.mapped_error?(error_klass)
13-
mapped_errors.values.include?(error_klass)
15+
mapped_errors.value?(error_klass)
1416
end
1517
end
1618
end
17-

lib/jsonapi_errors_handler/error_serializer.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'json'
24

35
module JsonapiErrorsHandler
@@ -10,7 +12,7 @@ def to_h
1012
serializable_hash
1113
end
1214

13-
def to_json(_payload=nil)
15+
def to_json(_payload = nil)
1416
to_h.to_json
1517
end
1618

@@ -25,4 +27,3 @@ def serializable_hash
2527
attr_reader :error
2628
end
2729
end
28-

lib/jsonapi_errors_handler/errors.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# frozen_string_literal: true
2+
13
module JsonapiErrorsHandler
24
module Errors
3-
45
end
56
end
67

lib/jsonapi_errors_handler/errors/forbidden.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ module Errors
55
class Forbidden < ::JsonapiErrorsHandler::Errors::StandardError
66
def initialize(message: nil)
77
super(
8-
title: "Forbidden request",
8+
title: 'Forbidden request',
99
status: 403,
10-
detail: message || "You have no rights to access this resource",
11-
source: { pointer: "/request/headers/authorization" }
10+
detail: message || 'You have no rights to access this resource',
11+
source: { pointer: '/request/headers/authorization' }
1212
)
1313
end
1414
end

lib/jsonapi_errors_handler/errors/invalid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Invalid < ::JsonapiErrorsHandler::Errors::StandardError
66
def initialize(errors: {})
77
@errors = errors
88
@status = 422
9-
@title = "Invalid request"
9+
@title = 'Invalid request'
1010
end
1111

1212
def serializable_hash

0 commit comments

Comments
 (0)