blacksheep 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5e427c080e5f5eaf01f76d9436ff18a36e183318acb2b942105566b34be7c03
4
- data.tar.gz: 22221c3900e3dd7d09c796e15e14d765c07cd307add2bbb39696b0767a17f088
3
+ metadata.gz: 84572df9e128c7565cfb17bfb7135451b33bc3b3d12ef97a2c3a3f319843f4d2
4
+ data.tar.gz: 79884c76bd997723295b04efaf0d5e32b086dd6a281f648bcbe6d01412710690
5
5
  SHA512:
6
- metadata.gz: cf17a2e2d7732162e0578567d8f04206da221f63d766294670c8b549126e24dd43d5a56451eabb4af6a8a37889ea4aa483a9c77b85c85b38308aec6fe76550c0
7
- data.tar.gz: 02cfecdffd2c6d09c3dca2440b6f130e33ae564ab6ba06ba729733f1c2bcc0d9c6605e8db2ef86e307cce4f7bc9250da0f133cb24551b2ad32337ac7d1e23f54
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.3.2)
4
+ blacksheep (0.4.0)
5
5
  dry-matcher
6
6
 
7
7
  GEM
@@ -3,12 +3,16 @@ 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
 
11
13
  class << self
14
+ attr_accessor :render_http_status
15
+
12
16
  def success(message)
13
17
  json = {
14
18
  _meta: {
@@ -51,19 +55,32 @@ module Blacksheep
51
55
  @status == :ok
52
56
  end
53
57
 
54
- def render_json(json_wrap: 'data')
55
- {
56
- json: wrap(@data, json_wrap: json_wrap),
57
- status: status
58
- }
58
+ def render_http_status
59
+ self.class.render_http_status
59
60
  end
60
61
 
61
- 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
62
68
 
63
- def wrap(json, json_wrap:)
64
- 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
65
81
 
66
- wrap.present? ? { wrap => json } : json
82
+ def wrap_json(json_wrap)
83
+ json_wrap.present? && success? ? { json_wrap => @data } : @data
67
84
  end
68
85
 
69
86
  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.3.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.3.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-06-02 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
@@ -101,7 +101,7 @@ licenses:
101
101
  metadata:
102
102
  homepage_uri: http://verticonaut.me
103
103
  source_code_uri: https://github.com/verticonaut/blacksheep
104
- post_install_message:
104
+ post_install_message:
105
105
  rdoc_options: []
106
106
  require_paths:
107
107
  - lib
@@ -116,8 +116,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  requirements: []
119
- rubygems_version: 3.0.8
120
- signing_key:
119
+ rubygems_version: 3.0.3
120
+ signing_key:
121
121
  specification_version: 4
122
122
  summary: Support for API acrtions
123
123
  test_files: []