blacksheep 0.2.3 → 0.4.1

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: 4b520c717b2b72704b7f6854ce506fca8e47773a3b2fe48d54344d2622a9ac8b
4
- data.tar.gz: b3dfe30943535dbc03e6a9e81c088e2267528c775aa484524d12710e50b2cf64
3
+ metadata.gz: bff82a7d7c387c7f72cd750bab7db223ec98ca693dfe07b5de48733b40a07df8
4
+ data.tar.gz: 866abc7ed1b93db2edca0d6a619a7b5c566ab4bac844d4560ea1b6a0d81036c8
5
5
  SHA512:
6
- metadata.gz: 3d3ee10c9acc1381e10e95101c42d7bbd17b6972c94b526aabc28b1b142bec56634da37b2ff0bd71f752ec158846ba888439af2d87bd3120c4fdda7e0a75c010
7
- data.tar.gz: '0401960e858de894e369599234431f8c91014e5ea0419924d00bb20396c48dd50d3faa04f2fa0d266f79e3d10795e9ad93ffa1ae45aa72b232d786a0a44db8e4'
6
+ metadata.gz: bc737ddd30b3d74b227635f4bde894345b92184310d391d88f073d715d59295998cd0cb25c0a556fb4f5a83ae4c109db6d9d721caa79dc1e4023fa78166fa0ac
7
+ data.tar.gz: 0c2f456acf1c54cb6236abc36c925abff77363078f4ae07a2d92db745147688fd3e40c5e65df280d3d914f312e77c779ceca2747035e713d716d716d95ee15a7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- blacksheep (0.2.3)
4
+ blacksheep (0.4.1)
5
5
  dry-matcher
6
6
 
7
7
  GEM
data/lib/blacksheep.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "blacksheep/version"
2
2
  require "blacksheep/action_result"
3
+ require "blacksheep/action_error"
3
4
  require "blacksheep/action_decorator"
4
5
  require "blacksheep/decorators/error_handler"
5
6
  require "blacksheep/decorators/default_error_handler"
@@ -0,0 +1,16 @@
1
+ module Blacksheep
2
+ #
3
+ # @class Blacksheep::ActionError
4
+ class ActionError < StandardError
5
+ attr_reader :identifier, :title, :status
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
@@ -10,11 +10,9 @@ module Blacksheep
10
10
 
11
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)
@@ -22,11 +20,9 @@ module Blacksheep
22
20
 
23
21
  transformed_params = self.transform_params(params)
24
22
 
25
- json = super(transformed_params, current_user: current_user, **options, &block)
26
-
27
- transformed_json = transform_result(json)
23
+ result = super(transformed_params, current_user: current_user, **options, &block)
28
24
 
29
- ActionResult.new(transformed_json, :ok)
25
+ as_transformed_action_result(result)
30
26
  end
31
27
 
32
28
 
@@ -50,16 +46,15 @@ module Blacksheep
50
46
  # Transform the obj with key in snake_case to the source case.
51
47
  # NOTE: leading underscored are preserved (e.g. _my_laptop => _myLaptop)
52
48
  #
53
- # @param obj [Array, Hash] A result structure
49
+ # @param obj [Array, Hash, ActionResult] A result structure
54
50
  # @return [Array, Hash] The rsult structure with keys converted to source caseing
55
51
  # @see #camelize_keys
56
- def transform_result(obj)
52
+ def as_transformed_action_result(obj)
57
53
  is_action_result, data = if obj.kind_of?(Blacksheep::ActionResult)
58
54
  [ true, obj.data ]
59
55
  else
60
56
  [ false, obj ]
61
57
  end
62
-
63
58
  converted_data = case @case
64
59
  when 'snake', 'as_is'
65
60
  data
@@ -69,7 +64,7 @@ module Blacksheep
69
64
  raise Blacksheep::Error, "unknown_case #{@case}"
70
65
  end
71
66
 
72
- is_action_result ? obj.set_data(converted_data) : converted_data
67
+ is_action_result ? obj.set_data(converted_data) : ActionResult.new(converted_data, :ok)
73
68
  end
74
69
 
75
70
  #
@@ -1,3 +1,3 @@
1
1
  module Blacksheep
2
- VERSION = "0.2.3"
2
+ VERSION = "0.4.1"
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.3
4
+ version: 0.4.1
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-18 00:00:00.000000000 Z
11
+ date: 2021-07-10 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: []