blacksheep 0.2.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f9f95a300f2d7c84826615cb86a66fabe8ecdd680edabf6b9e960973d8f5f83
4
- data.tar.gz: 3cdfd65ec9647bed1ff5df0261b000227f395d379a9c310e8def4de9b0439c83
3
+ metadata.gz: 84572df9e128c7565cfb17bfb7135451b33bc3b3d12ef97a2c3a3f319843f4d2
4
+ data.tar.gz: 79884c76bd997723295b04efaf0d5e32b086dd6a281f648bcbe6d01412710690
5
5
  SHA512:
6
- metadata.gz: ac515283bff176a47ffa4b6159e169c7a3776bc1e2d0f587182e479420b805b0633e2f909a9720bbff15bcc68776ce9f180e51e6a014e2e2ef2ae261b6114d94
7
- data.tar.gz: 1c7008322b347109e570b87f8a83b6512a7380c4021e779655fb80f8bd426efc9aad3a1a606c73dd09e7735f2a85dd927e5f8f472ce5650323ec7d90283c82cf
6
+ metadata.gz: 8819a2a83c63b4c3c6d6c9aaf3ccfcab848ebe70dc49b81033dea1df578626905577dcb18ef80a5c7af0e7b34685c3de90d88a4f3a532a1ea35caa0b7468d590
7
+ data.tar.gz: b31d65fedd9a8f0870b4be19baeb7ebe8f9941071a030f8d9ba97fab16d0be46a9613960c0600ef582702aeba4b0bbc1e587cd09fd5b6ffe69d90ccb0adfec45
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- blacksheep (0.2.2)
4
+ blacksheep (0.4.0)
5
5
  dry-matcher
6
6
 
7
7
  GEM
data/lib/blacksheep.rb CHANGED
@@ -1,9 +1,12 @@
1
1
  require "blacksheep/version"
2
2
  require "blacksheep/action_result"
3
+ require "blacksheep/action_error"
3
4
  require "blacksheep/action_decorator"
5
+ require "blacksheep/decorators/error_handler"
4
6
  require "blacksheep/decorators/default_error_handler"
5
7
  require "blacksheep/decorators/json_transformer"
6
8
  require "blacksheep/decorators/result_matcher"
9
+ require "blacksheep/decorators/localizer"
7
10
  require "blacksheep/action"
8
11
  require "blacksheep/version"
9
12
 
@@ -28,11 +28,16 @@ module Blacksheep
28
28
  @@decorators ||= []
29
29
  end
30
30
 
31
+ #
32
+ # Adds a decorator to the list of decorated to be applied on Balcksheep::Actions
33
+ # @param decorator [type] [description]
34
+ #
35
+ # @return [type] [description]
31
36
  def add_decorator(decorator)
32
- decorators << decorator
37
+ decorators << decorator unless decorators.include?(decorator)
33
38
  end
34
39
 
35
- def new(*)
40
+ def new(*arguments, &block)
36
41
  instance = super
37
42
 
38
43
  decorators.each do |decorator|
@@ -1,9 +1,29 @@
1
1
  require 'delegate'
2
2
 
3
3
  module Blacksheep
4
+ # @class Blacksheep::ActionDecorator
4
5
  class ActionDecorator < SimpleDelegator
6
+
7
+ # Access to the decorators class - since original class is overwritten to return the model class.
8
+ #
9
+ # @return [Class] The class of the deocrator.
10
+ # @see [#class]
11
+ alias_method :__class__, :class
12
+
5
13
  def class
6
14
  __getobj__.class
7
15
  end
16
+
17
+ #
18
+ # Just for curiosity - get the decorators chain
19
+ #
20
+ # @return [type] [description]
21
+ def decorators_chain
22
+ decorated = __getobj__
23
+ chain = decorated.kind_of?(Blacksheep::ActionDecorator) ? decorated.decorators_chain : [ decorated.class ]
24
+
25
+ chain.unshift(self.__class__)
26
+ end
27
+
8
28
  end
9
29
  end
@@ -0,0 +1,16 @@
1
+ module Blacksheep
2
+ #
3
+ # @class Blacksheep::ActionError
4
+ class ActionError < StandardError
5
+ attr_reader :identifier, :title
6
+
7
+ def initialize(message, title: 'Error', identifier: 'undefined', status: :internal_server_error)
8
+ @identifier = identifier
9
+ @title = title
10
+ @status = status
11
+
12
+ super(message)
13
+ end
14
+
15
+ end
16
+ end
@@ -3,11 +3,42 @@ module Blacksheep
3
3
 
4
4
  attr_reader :data, :status
5
5
 
6
+ @render_http_status = true
7
+
6
8
  def initialize(data, status)
7
9
  @data = data
8
10
  @status = status
9
11
  end
10
12
 
13
+ class << self
14
+ attr_accessor :render_http_status
15
+
16
+ def success(message)
17
+ json = {
18
+ _meta: {
19
+ message: message
20
+ }
21
+ }
22
+
23
+ new(json, :ok)
24
+ end
25
+
26
+ def error(title: 'Error', message:, status: :internal_server_error, pointer: 'unspecified')
27
+ json = {
28
+ errors: [
29
+ pointer: {
30
+ source: pointer
31
+ },
32
+ title: title,
33
+ detail: message,
34
+ ]
35
+ }
36
+ status = :internal_server_error
37
+
38
+ new(json, status)
39
+ end
40
+ end
41
+
11
42
  def set_data(value)
12
43
  @data = value
13
44
 
@@ -24,19 +55,32 @@ module Blacksheep
24
55
  @status == :ok
25
56
  end
26
57
 
27
- def render_json(json_wrap: 'data')
28
- {
29
- json: wrap(@data, json_wrap: json_wrap),
30
- status: status
31
- }
58
+ def render_http_status
59
+ self.class.render_http_status
32
60
  end
33
61
 
34
- private
62
+ def render_json(json_wrap: 'data', render_http_status: self.render_http_status, to: nil)
63
+ if to.nil?
64
+ return {
65
+ json: wrap_json(json_wrap)
66
+ }
67
+ end
35
68
 
36
- def wrap(json, json_wrap:)
37
- wrap = success? ? json_wrap : nil
69
+ if to.respond_to?(:render)
70
+ if render_http_status
71
+ to.render json: wrap_json(json_wrap), status: status
72
+ else
73
+ to.render json: wrap_json(json_wrap)
74
+ end
75
+ else
76
+ raise ArgumentError, "render target #{to.class} does not respond to #render"
77
+ end
78
+ end
79
+
80
+ private
38
81
 
39
- wrap.present? ? { wrap => json } : json
82
+ def wrap_json(json_wrap)
83
+ json_wrap.present? && success? ? { json_wrap => @data } : @data
40
84
  end
41
85
 
42
86
  end
@@ -7,64 +7,57 @@ module Blacksheep
7
7
  def handle(exception)
8
8
  json = status = nil
9
9
 
10
- # case exception
11
- # when Exceptions::ValidationException
12
- # errors = []
13
- # exception.model.errors.each do |attribute, message|
14
- # errors << {
15
- # title: "'#{attribute}' validation error",
16
- # detail: message,
17
- # }
18
- # end
19
- # json = {
20
- # errors: errors
21
- # }
22
- # status = :unprocessable_entity # 422
23
- # when Pundit::NotAuthorizedError
24
- # json = {
25
- # errors: [
26
- # pointer: {
27
- # source: not_authorized_pointer(exception)
28
- # },
29
- # title: "#{exception.class}",
30
- # detail: "#{exception.message}",
31
- # ]
32
- # }
33
- # status = :unauthorized # 401
34
- # when Exceptions::AuthenticationInvalid
35
- # json = {
36
- # errors: [
37
- # pointer: {
38
- # source: 'Secured Module'
39
- # },
40
- # title: "#{exception.class}",
41
- # detail: "#{exception.message}",
42
- # ]
43
- # }
44
- # status = :unauthorized # 401
45
- # else
46
- # json = {
47
- # errors: [
48
- # pointer: {
49
- # source: 'Internal'
50
- # },
51
- # title: "#{exception.class}",
52
- # detail: "#{exception.message}",
53
- # ]
54
- # }
55
- # status = :internal_server_error # 500
56
- # end
10
+ case exception
11
+ when Blacksheep::ActionError
12
+ json = {
13
+ errors: [
14
+ pointer: {
15
+ source: exception.backtrace.first,
16
+ identifier: exception.identifier,
17
+ },
18
+ title: exception.title,
19
+ detail: exception.message,
20
+ ]
21
+ }
57
22
 
58
- json = {
59
- errors: [
60
- pointer: {
61
- source: 'Internal'
62
- },
63
- title: "#{exception.class}",
64
- detail: "#{exception.message}",
65
- ]
66
- }
67
- status = :internal_server_error # 500
23
+ status = exception.status
24
+
25
+ # when Pundit::NotAuthorizedError
26
+ # json = {
27
+ # errors: [
28
+ # pointer: {
29
+ # source: not_authorized_pointer(exception)
30
+ # },
31
+ # title: "#{exception.class}",
32
+ # detail: "#{exception.message}",
33
+ # ]
34
+ # }
35
+ # status = :unauthorized # 401
36
+ # when Exceptions::AuthenticationInvalid
37
+ # json = {
38
+ # errors: [
39
+ # pointer: {
40
+ # source: 'Secured Module'
41
+ # },
42
+ # title: "#{exception.class}",
43
+ # detail: "#{exception.message}",
44
+ # ]
45
+ # }
46
+ # status = :unauthorized # 401
47
+
48
+ else
49
+ json = {
50
+ errors: [
51
+ pointer: {
52
+ source: 'Internal'
53
+ },
54
+ title: "#{exception.class}",
55
+ detail: "#{exception.message}",
56
+ ]
57
+ }
58
+ end
59
+
60
+ status ||= :internal_server_error
68
61
 
69
62
  ActionResult.new(json, status)
70
63
  end
@@ -1,7 +1,7 @@
1
1
  module Blacksheep
2
2
  module Decorators
3
- # @class Blacksheep::Decorators::ErrorHandler
4
- class ErrorHandler
3
+ # @module Blacksheep::Decorators::ErrorHandler
4
+ module ErrorHandler
5
5
 
6
6
  def call(*)
7
7
  super
@@ -5,27 +5,24 @@ module Blacksheep
5
5
 
6
6
  attr_reader :case, :params
7
7
 
8
- def call(params, **options)
8
+ def call(params, current_user: nil, **options)
9
9
  detect_case(params)
10
10
 
11
- transformed_params = self.transformed_params(params)
11
+ transformed_params = self.transform_params(params)
12
12
 
13
- json = super(transformed_params, **options)
13
+ result = super(transformed_params, **options)
14
14
 
15
- transformed_json = transform_result(json)
16
-
17
- ActionResult.new(transformed_json, :ok)
15
+ as_transformed_action_result(result)
18
16
  end
19
17
 
20
18
  def perform(params, current_user: nil, **options, &block)
21
19
  detect_case(params)
22
20
 
23
- transformed_params = self.transformed_params(params)
21
+ transformed_params = self.transform_params(params)
24
22
 
25
- json = block.call(transformed_params)
26
- transformed_json = transform_result(json)
23
+ result = super(transformed_params, current_user: current_user, **options, &block)
27
24
 
28
- ActionResult.new(transformed_json, :ok)
25
+ as_transformed_action_result(result)
29
26
  end
30
27
 
31
28
 
@@ -34,7 +31,7 @@ module Blacksheep
34
31
  #
35
32
  # @return [Array, Hash] The params converted into snake_case
36
33
  # @see #snakecase_keys
37
- def transformed_params(params)
34
+ def transform_params(params)
38
35
  case @case
39
36
  when 'snake', 'as_is'
40
37
  params
@@ -49,16 +46,15 @@ module Blacksheep
49
46
  # Transform the obj with key in snake_case to the source case.
50
47
  # NOTE: leading underscored are preserved (e.g. _my_laptop => _myLaptop)
51
48
  #
52
- # @param obj [Array, Hash] A result structure
49
+ # @param obj [Array, Hash, ActionResult] A result structure
53
50
  # @return [Array, Hash] The rsult structure with keys converted to source caseing
54
51
  # @see #camelize_keys
55
- def transform_result(obj)
52
+ def as_transformed_action_result(obj)
56
53
  is_action_result, data = if obj.kind_of?(Blacksheep::ActionResult)
57
54
  [ true, obj.data ]
58
55
  else
59
56
  [ false, obj ]
60
57
  end
61
-
62
58
  converted_data = case @case
63
59
  when 'snake', 'as_is'
64
60
  data
@@ -68,7 +64,7 @@ module Blacksheep
68
64
  raise Blacksheep::Error, "unknown_case #{@case}"
69
65
  end
70
66
 
71
- is_action_result ? obj.set_data(converted_data) : converted_data
67
+ is_action_result ? obj.set_data(converted_data) : ActionResult.new(converted_data, :ok)
72
68
  end
73
69
 
74
70
  #
@@ -1,10 +1,10 @@
1
1
  module Blacksheep
2
2
  module Decorators
3
3
 
4
- # @class Blacksheep::Decorators::Localozer
4
+ # @class Blacksheep::Decorators::Localizer
5
5
  class Localizer < ActionDecorator
6
6
 
7
- def call(params, **)
7
+ def call(params, current_user: nil, **options)
8
8
  if (locale = params[:_locale])
9
9
  I18n.with_locale(locale) do
10
10
  super
@@ -14,7 +14,8 @@ module Blacksheep
14
14
  end
15
15
  end
16
16
 
17
- def perform(params, **)
17
+ def perform(params, current_user: nil, **options)
18
+
18
19
  if (locale = params[:_locale])
19
20
  I18n.with_locale(locale) do
20
21
  super
@@ -1,3 +1,3 @@
1
1
  module Blacksheep
2
- VERSION = "0.2.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blacksheep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Schweizer
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-17 00:00:00.000000000 Z
11
+ date: 2021-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-matcher
@@ -87,6 +87,7 @@ files:
87
87
  - lib/blacksheep.rb
88
88
  - lib/blacksheep/action.rb
89
89
  - lib/blacksheep/action_decorator.rb
90
+ - lib/blacksheep/action_error.rb
90
91
  - lib/blacksheep/action_result.rb
91
92
  - lib/blacksheep/decorators/default_error_handler.rb
92
93
  - lib/blacksheep/decorators/error_handler.rb
@@ -100,7 +101,7 @@ licenses:
100
101
  metadata:
101
102
  homepage_uri: http://verticonaut.me
102
103
  source_code_uri: https://github.com/verticonaut/blacksheep
103
- post_install_message:
104
+ post_install_message:
104
105
  rdoc_options: []
105
106
  require_paths:
106
107
  - lib
@@ -115,8 +116,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  - !ruby/object:Gem::Version
116
117
  version: '0'
117
118
  requirements: []
118
- rubygems_version: 3.0.8
119
- signing_key:
119
+ rubygems_version: 3.0.3
120
+ signing_key:
120
121
  specification_version: 4
121
122
  summary: Support for API acrtions
122
123
  test_files: []