Skip to content

Commit

Permalink
Add top-level class documentation for all defined classes
Browse files Browse the repository at this point in the history
  • Loading branch information
swilgosz committed Sep 21, 2020
1 parent cc7882b commit 45b94bd
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/jsonapi_errors_handler.rb
Expand Up @@ -6,6 +6,8 @@
require 'jsonapi_errors_handler/error_mapper'
require 'jsonapi_errors_handler/error_serializer'

# Allows to handle ruby errors and return the serialized JSON:API output
#
module JsonapiErrorsHandler
PREDEFINED_HASH = {
'JsonapiErrorsHandler::Errors::Invalid' =>
Expand Down
8 changes: 8 additions & 0 deletions lib/jsonapi_errors_handler/configuration.rb
Expand Up @@ -3,15 +3,23 @@
require 'singleton'

module JsonapiErrorsHandler
# Configuration class allowing to set up the initial behavior for the gem
#
class Configuration
include Singleton

attr_writer :handle_unexpected

# Allows to override the configuration options
# @param [Block] - list of options to be overwriten
#
def configure
yield(self) if block_given?
end

# Checks if unexpected errors should be handled by the gem
# @return [Boolean]
#
def handle_unexpected?
@handle_unexpected
end
Expand Down
3 changes: 3 additions & 0 deletions lib/jsonapi_errors_handler/error_mapper.rb
@@ -1,6 +1,9 @@
# frozen_string_literal: true

module JsonapiErrorsHandler
# Maps any of the given error classes into the serializable errors
# from the predefined collection
#
class ErrorMapper
@mapped_errors = {}

Expand Down
2 changes: 2 additions & 0 deletions lib/jsonapi_errors_handler/error_serializer.rb
Expand Up @@ -3,6 +3,8 @@
require 'json'

module JsonapiErrorsHandler
# Serializes the given error to match the JSON:API specification
#
class ErrorSerializer
def initialize(error)
@error = error
Expand Down
2 changes: 2 additions & 0 deletions lib/jsonapi_errors_handler/errors.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module JsonapiErrorsHandler
# A set of predefined, serializable HTTP error objects
#
module Errors
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/jsonapi_errors_handler/keys_stringifier.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module JsonapiErrorsHandler
# Allows to stringify keys on the given hash
#
class KeysStringifier
def self.call(hash)
hash.reduce({}) do |h, (k, v)|
Expand Down
6 changes: 4 additions & 2 deletions spec/error_mapper_spec.rb
Expand Up @@ -44,7 +44,8 @@

it 'returns the original error if the instance had been risen' do
JsonapiErrorsHandler::ErrorMapper.map_errors!(
'JsonapiErrorsHandler::Errors::Forbidden' => 'JsonapiErrorsHandler::Errors::Forbidden'
'JsonapiErrorsHandler::Errors::Forbidden' =>
'JsonapiErrorsHandler::Errors::Forbidden'
)
expect(subject).to eq mapped_error
expect(subject.detail).to eq('test')
Expand All @@ -56,7 +57,8 @@

it 'returns an instance of the risen error klass' do
JsonapiErrorsHandler::ErrorMapper.map_errors!(
'JsonapiErrorsHandler::Errors::Forbidden' => 'JsonapiErrorsHandler::Errors::Forbidden'
'JsonapiErrorsHandler::Errors::Forbidden' =>
'JsonapiErrorsHandler::Errors::Forbidden'
)
expect(subject).to eq mapped_error.new
end
Expand Down

0 comments on commit 45b94bd

Please sign in to comment.