pi-resources-rails 0.1.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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +5 -0
- data/lib/pi_resources/method_not_overridden_error.rb +9 -0
- data/lib/pi_resources/params_helpers.rb +15 -0
- data/lib/pi_resources/railtie.rb +6 -0
- data/lib/pi_resources/renderers.rb +21 -0
- data/lib/pi_resources/resources_controller.rb +168 -0
- data/lib/pi_resources/serializer/base.rb +56 -0
- data/lib/pi_resources/serializer/exception_serializer.rb +38 -0
- data/lib/pi_resources/serializer/hash_serializer.rb +52 -0
- data/lib/pi_resources/serializer/money_serializer.rb +37 -0
- data/lib/pi_resources/serializer/object_serializer.rb +103 -0
- data/lib/pi_resources/serializer/resource_errors_serializer.rb +47 -0
- data/lib/pi_resources/serializer/smart_hash_serializer.rb +35 -0
- data/lib/pi_resources/serializer/validation_response.rb +17 -0
- data/lib/pi_resources/serializer/validation_response_serializer.rb +36 -0
- data/lib/pi_resources/serializer.rb +6 -0
- data/lib/pi_resources/version.rb +5 -0
- data/lib/pi_resources.rb +27 -0
- data/lib/tasks/pi_resource_tasks.rake +5 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cd8544e753ce20c755b5a633b82236c5e385a4265ce856f11066621b75bd8957
|
4
|
+
data.tar.gz: eacbef0fc52c69b02dc9d1584d369329460b00b44eec9eeb79ed9fe19ca10303
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cc5e99e7249c72f4bf1a2c5677e679ae9e45e6577d0031f83298048106b9f3b40fd5753cb6219e4581a92999db4df708184f03158df6b222cbb5bbd19b08436e
|
7
|
+
data.tar.gz: 862f5be3a73c083b65315ea8087d3183160ec641e3589ff642c044ee1011c055482270f39fd99c17b64f4610107e6cb64369fed2537d48d1a9120c112330ad14
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023 Umberto Peserico
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# PiResources
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "pi_resources"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install pi_resources
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PiResources
|
4
|
+
module ParamsHelpers
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def request_params
|
8
|
+
request.request_parameters.to_h.deep_symbolize_keys
|
9
|
+
end
|
10
|
+
|
11
|
+
def query_params
|
12
|
+
request.query_parameters.deep_transform_keys(&:underscore).deep_symbolize_keys
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ActionController::Renderers.add :resource do |resource, options|
|
4
|
+
options = options.slice(:serializer, :meta, :message, :context, :message, :location)
|
5
|
+
|
6
|
+
puts options.inspect
|
7
|
+
|
8
|
+
result = options[:serializer].new(resource, meta: options[:meta], message: options[:message]).as_json
|
9
|
+
|
10
|
+
self.content_type = Mime[:json]
|
11
|
+
|
12
|
+
result
|
13
|
+
end
|
14
|
+
|
15
|
+
ActionController::Renderers.add :resource_errors do |errors, _options|
|
16
|
+
result = PiResources::Serializer::ResourceErrorsSerializer.new(errors).as_json
|
17
|
+
|
18
|
+
self.content_type = Mime[:json]
|
19
|
+
|
20
|
+
result
|
21
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'debug'
|
4
|
+
|
5
|
+
module PiResources
|
6
|
+
module ResourcesController
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
def index
|
10
|
+
@resource_collection = resource_collection_resolver
|
11
|
+
|
12
|
+
render resource: @resource_collection,
|
13
|
+
serializer: index_resource_serializer,
|
14
|
+
context: context
|
15
|
+
end
|
16
|
+
|
17
|
+
def show
|
18
|
+
@resource = resource_finder
|
19
|
+
|
20
|
+
render resource: @resource,
|
21
|
+
serializer: show_resource_serializer,
|
22
|
+
context: context,
|
23
|
+
location: resource_location
|
24
|
+
end
|
25
|
+
|
26
|
+
def create
|
27
|
+
@resource = resource_creator
|
28
|
+
|
29
|
+
if @resource.valid?
|
30
|
+
render resource: @resource,
|
31
|
+
serializer: create_resource_serializer,
|
32
|
+
context: context,
|
33
|
+
message: create_message,
|
34
|
+
location: resource_location
|
35
|
+
else
|
36
|
+
render resource_errors: @resource.errors
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def update
|
41
|
+
@resource = resource_updater
|
42
|
+
|
43
|
+
if @resource.valid?
|
44
|
+
render resource: @resource,
|
45
|
+
serializer: update_resource_serializer,
|
46
|
+
context: context,
|
47
|
+
message: update_message,
|
48
|
+
location: resource_location
|
49
|
+
else
|
50
|
+
render resource_errors: @resource.errors
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def destroy
|
55
|
+
@resource = resource_destroyer
|
56
|
+
|
57
|
+
if @resource.valid?
|
58
|
+
render resource: @resource,
|
59
|
+
serializer: destroy_resource_serializer,
|
60
|
+
context: context,
|
61
|
+
message: destroy_message,
|
62
|
+
location: resource_location
|
63
|
+
else
|
64
|
+
render resource_errors: @resource.errors
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
def context
|
71
|
+
@context ||= {}
|
72
|
+
end
|
73
|
+
|
74
|
+
def resource_service
|
75
|
+
resource_service_class.new
|
76
|
+
end
|
77
|
+
|
78
|
+
def resource_service_class
|
79
|
+
raise MethodNotOverriddenError.new(__method__, self)
|
80
|
+
end
|
81
|
+
|
82
|
+
def resource_serializer
|
83
|
+
raise MethodNotOverriddenError.new(__method__, self)
|
84
|
+
end
|
85
|
+
|
86
|
+
def resource_location
|
87
|
+
raise MethodNotOverriddenError.new(__method__, self)
|
88
|
+
end
|
89
|
+
|
90
|
+
def resource_params_root_key
|
91
|
+
raise MethodNotOverriddenError.new(__method__, self)
|
92
|
+
end
|
93
|
+
|
94
|
+
def create_message
|
95
|
+
nil
|
96
|
+
end
|
97
|
+
|
98
|
+
def update_message
|
99
|
+
nil
|
100
|
+
end
|
101
|
+
|
102
|
+
def destroy_message
|
103
|
+
nil
|
104
|
+
end
|
105
|
+
|
106
|
+
def resource_params
|
107
|
+
@resource_params ||= request_params.fetch(resource_params_root_key)
|
108
|
+
rescue KeyError => e
|
109
|
+
raise ActionController::ParameterMissing, e
|
110
|
+
end
|
111
|
+
|
112
|
+
def context=(context)
|
113
|
+
self.context.merge!(context)
|
114
|
+
end
|
115
|
+
|
116
|
+
def index_resource_serializer
|
117
|
+
resource_serializer
|
118
|
+
end
|
119
|
+
|
120
|
+
def show_resource_serializer
|
121
|
+
resource_serializer
|
122
|
+
end
|
123
|
+
|
124
|
+
def create_resource_serializer
|
125
|
+
resource_serializer
|
126
|
+
end
|
127
|
+
|
128
|
+
def update_resource_serializer
|
129
|
+
resource_serializer
|
130
|
+
end
|
131
|
+
|
132
|
+
def destroy_resource_serializer
|
133
|
+
resource_serializer
|
134
|
+
end
|
135
|
+
|
136
|
+
def resource_collection_resolver
|
137
|
+
resource_service.all
|
138
|
+
end
|
139
|
+
|
140
|
+
def resource_finder
|
141
|
+
resource_service.find(resource_key_param)
|
142
|
+
end
|
143
|
+
|
144
|
+
def resource_creator
|
145
|
+
resource_service.create(resource_create_params)
|
146
|
+
end
|
147
|
+
|
148
|
+
def resource_updater
|
149
|
+
resource_service.update(resource_key_param, resource_update_params)
|
150
|
+
end
|
151
|
+
|
152
|
+
def resource_destroyer
|
153
|
+
resource_service.destroy(resource_key_param)
|
154
|
+
end
|
155
|
+
|
156
|
+
def resource_key_param
|
157
|
+
params[:id]
|
158
|
+
end
|
159
|
+
|
160
|
+
def resource_create_params
|
161
|
+
resource_params
|
162
|
+
end
|
163
|
+
|
164
|
+
def resource_update_params
|
165
|
+
resource_params
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PiResources
|
4
|
+
module Serializer
|
5
|
+
module Base
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
TRANSFORMS_MAPPING = {
|
9
|
+
camel_lower: %i[camelize lower],
|
10
|
+
underscore: :underscore,
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
included do
|
14
|
+
cattr_accessor :transform_method_value, default: TRANSFORMS_MAPPING[:camel_lower]
|
15
|
+
end
|
16
|
+
|
17
|
+
def as_json(opts = nil)
|
18
|
+
JSON.generate(result, opts)
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_h
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
alias to_hash to_h
|
26
|
+
|
27
|
+
def serializable_hash!
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def run_key_transform!(key)
|
32
|
+
key.to_s.send(*transform_method_value).to_sym
|
33
|
+
end
|
34
|
+
|
35
|
+
class_methods do
|
36
|
+
def transform_method(transform_method)
|
37
|
+
unless TRANSFORMS_MAPPING.key?(transform_method)
|
38
|
+
raise StandardError, "Transform method must be equal to #{TRANSFORMS_MAPPING.keys}"
|
39
|
+
end
|
40
|
+
|
41
|
+
self.transform_method_value = TRANSFORMS_MAPPING[transform_method]
|
42
|
+
end
|
43
|
+
|
44
|
+
def collection?(resource)
|
45
|
+
resource.is_a?(Enumerable) && !resource.respond_to?(:each_pair)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def result
|
52
|
+
@result ||= serializable_hash!
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pi_resources/serializer/base'
|
4
|
+
|
5
|
+
module PiResources
|
6
|
+
module Serializer
|
7
|
+
class ExceptionSerializer
|
8
|
+
include Base
|
9
|
+
|
10
|
+
attr_reader :error, :status, :exception, :root
|
11
|
+
|
12
|
+
def initialize(status: nil, error: nil, exception: nil, root: true)
|
13
|
+
@error = error
|
14
|
+
@status = status
|
15
|
+
@exception = exception
|
16
|
+
@root = root
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def serializable_hash!
|
22
|
+
data = {}
|
23
|
+
result = {}
|
24
|
+
|
25
|
+
data[run_key_transform!(:status)] = status
|
26
|
+
data[run_key_transform!(:error)] = error if error
|
27
|
+
data[run_key_transform!(:exception)] = exception if exception
|
28
|
+
|
29
|
+
if root
|
30
|
+
result[run_key_transform!(:validation_response)] = data
|
31
|
+
result
|
32
|
+
else
|
33
|
+
data
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'pi_resources/serializer/base'
|
6
|
+
|
7
|
+
module PiResources
|
8
|
+
module Serializer
|
9
|
+
class HashSerializer
|
10
|
+
include Base
|
11
|
+
|
12
|
+
attr_reader :hash, :meta, :message, :root
|
13
|
+
|
14
|
+
def initialize(resource, meta: nil, message: nil, context: nil, root: true)
|
15
|
+
@hash = resource
|
16
|
+
@resource = resource
|
17
|
+
@meta = meta
|
18
|
+
@message = message
|
19
|
+
@context = context || {}
|
20
|
+
@root = root
|
21
|
+
end
|
22
|
+
|
23
|
+
def data
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def serializable_hash!
|
30
|
+
if root
|
31
|
+
result = {}
|
32
|
+
|
33
|
+
result[run_key_transform!(:data)] = serialize_data
|
34
|
+
result[run_key_transform!(:meta)] = meta&.deep_transform_keys { |key| run_key_transform!(key) } if meta
|
35
|
+
result[run_key_transform!(:message)] = message if message
|
36
|
+
|
37
|
+
result
|
38
|
+
else
|
39
|
+
serialize_data
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def serialize_data
|
44
|
+
if self.class.collection? @resource
|
45
|
+
@resource.map { |o| o.deep_transform_keys { |key| run_key_transform!(key) } }
|
46
|
+
else
|
47
|
+
@resource&.deep_transform_keys { |key| run_key_transform!(key) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pi_resources/serializer/object_serializer'
|
4
|
+
|
5
|
+
module PiResources
|
6
|
+
module Serializer
|
7
|
+
class MoneySerializer
|
8
|
+
include ObjectSerializer
|
9
|
+
|
10
|
+
attribute :cents, method: :cents
|
11
|
+
attribute :amount, method: :amount
|
12
|
+
attribute :currency, method: :currency
|
13
|
+
attribute :formatted_text, method: :formatted_text
|
14
|
+
attribute :symbol, method: :symbol
|
15
|
+
|
16
|
+
def cents(record)
|
17
|
+
record.cents
|
18
|
+
end
|
19
|
+
|
20
|
+
def amount(record)
|
21
|
+
record.to_f
|
22
|
+
end
|
23
|
+
|
24
|
+
def currency(record)
|
25
|
+
record.currency.iso_code
|
26
|
+
end
|
27
|
+
|
28
|
+
def formatted_text(record)
|
29
|
+
record.format(symbol: "#{record.currency.symbol} ")
|
30
|
+
end
|
31
|
+
|
32
|
+
def symbol(record)
|
33
|
+
record.currency.symbol
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pi_resources/serializer/base'
|
4
|
+
|
5
|
+
module PiResources
|
6
|
+
module Serializer
|
7
|
+
module ObjectSerializer
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
include Base
|
10
|
+
|
11
|
+
included do
|
12
|
+
class << self
|
13
|
+
attr_writer :attributes_to_serialize
|
14
|
+
|
15
|
+
def attributes_to_serialize
|
16
|
+
@attributes_to_serialize ||= []
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :resource, :message, :context, :root
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(resource, meta: nil, message: nil, context: nil, root: true)
|
24
|
+
@resource = resource
|
25
|
+
@meta = meta
|
26
|
+
@message = message
|
27
|
+
@context = context || {}
|
28
|
+
@root = root
|
29
|
+
end
|
30
|
+
|
31
|
+
class_methods do
|
32
|
+
def attribute(key, options = {})
|
33
|
+
attributes_to_serialize.push({
|
34
|
+
name: key,
|
35
|
+
**options.slice(:method, :serializer, :serialize_if),
|
36
|
+
})
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def data
|
41
|
+
if self.class.collection? @resource
|
42
|
+
data_for_collection
|
43
|
+
else
|
44
|
+
data_for_one_record
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def meta
|
49
|
+
@meta&.deep_transform_keys do |k|
|
50
|
+
run_key_transform!(k)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def serializable_hash!
|
55
|
+
if root
|
56
|
+
use_root
|
57
|
+
else
|
58
|
+
data
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def data_for_collection
|
65
|
+
@resource.map { |el| build_record(el) }
|
66
|
+
end
|
67
|
+
|
68
|
+
def data_for_one_record
|
69
|
+
build_record(@resource) if @resource
|
70
|
+
end
|
71
|
+
|
72
|
+
def build_record(record)
|
73
|
+
self.class.attributes_to_serialize.each_with_object({}) do |attr, hash|
|
74
|
+
name = attr[:name]
|
75
|
+
serializer = attr[:serializer]
|
76
|
+
method = attr[:method]
|
77
|
+
serialize_if = attr[:serialize_if]
|
78
|
+
|
79
|
+
next if serialize_if && !serialize_if.call(context)
|
80
|
+
|
81
|
+
hash[run_key_transform!(name)] = if serializer.present?
|
82
|
+
attr[:serializer].new(record.send(name), root: false, context: context)
|
83
|
+
.to_h
|
84
|
+
elsif method.present?
|
85
|
+
send(method, record)
|
86
|
+
else
|
87
|
+
record.send(name)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def use_root
|
93
|
+
result = {}
|
94
|
+
|
95
|
+
result[run_key_transform!(:data)] = data
|
96
|
+
result[run_key_transform!(:meta)] = meta if @meta.present?
|
97
|
+
result[run_key_transform!(:message)] = message if @message.present?
|
98
|
+
|
99
|
+
result
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'pi_resources/serializer/base'
|
5
|
+
|
6
|
+
module PiResources
|
7
|
+
module Serializer
|
8
|
+
class ResourceErrorsSerializer
|
9
|
+
include Base
|
10
|
+
|
11
|
+
def initialize(errors)
|
12
|
+
@resource = errors
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def errors
|
18
|
+
@resource.full_messages
|
19
|
+
end
|
20
|
+
|
21
|
+
def details
|
22
|
+
@resource.details.each_with_object({}) do |error, result|
|
23
|
+
field = error[0]
|
24
|
+
errors = error[1]
|
25
|
+
result[run_key_transform!(field)] = errors.map do |e|
|
26
|
+
e.each_with_object({}) do |obj, result2|
|
27
|
+
if obj[0] == :error
|
28
|
+
result2[:error] = run_key_transform!(obj[1])
|
29
|
+
else
|
30
|
+
result2[run_key_transform!(obj[0])] = obj[1]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def serializable_hash!
|
38
|
+
result = {}
|
39
|
+
|
40
|
+
result[run_key_transform!(:errors)] = errors
|
41
|
+
result[run_key_transform!(:details)] = details
|
42
|
+
|
43
|
+
result
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'pi_resources/serializer/money_serializer'
|
5
|
+
|
6
|
+
module PiResources
|
7
|
+
module Serializer
|
8
|
+
class SmartHashSerializer < HashSerializer
|
9
|
+
def initialize(resource, meta: nil, message: nil, context: nil, root: true)
|
10
|
+
super(
|
11
|
+
check_type(resource),
|
12
|
+
meta: meta,
|
13
|
+
message: message,
|
14
|
+
context: context,
|
15
|
+
root: root,
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def check_type(stuff)
|
22
|
+
case stuff
|
23
|
+
when Array
|
24
|
+
stuff.map { |s| check_type(s) }
|
25
|
+
when Hash
|
26
|
+
stuff.transform_values { |v| check_type(v) }
|
27
|
+
when Money
|
28
|
+
MoneySerializer.new(stuff, root: false).data
|
29
|
+
else
|
30
|
+
stuff
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
module PiResources
|
6
|
+
module Serializer
|
7
|
+
class ValidationResponse
|
8
|
+
include ActiveModel::Model
|
9
|
+
include ActiveModel::Attributes
|
10
|
+
include ActiveModel::Serialization
|
11
|
+
|
12
|
+
attribute :id, type: :string
|
13
|
+
attribute :status, type: :integer
|
14
|
+
attribute :message, type: :string
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'pi_resources/serializer/base'
|
5
|
+
|
6
|
+
module PiResources
|
7
|
+
module Serializer
|
8
|
+
class ValidationResponseSerializer
|
9
|
+
include Base
|
10
|
+
|
11
|
+
attr_reader :message
|
12
|
+
|
13
|
+
def initialize(resource, message = nil)
|
14
|
+
@resource = resource
|
15
|
+
@message = message
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def validation_response
|
21
|
+
@resource.attributes.symbolize_keys.deep_transform_keys do |k|
|
22
|
+
run_key_transform!(k)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def serializable_hash!
|
27
|
+
result = {}
|
28
|
+
|
29
|
+
result[run_key_transform!(:validation_response)] = validation_response
|
30
|
+
result[run_key_transform!(:message)] = message if message
|
31
|
+
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/pi_resources.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/all'
|
4
|
+
require 'active_model'
|
5
|
+
require 'json'
|
6
|
+
require 'money'
|
7
|
+
|
8
|
+
require 'pi_resources/version'
|
9
|
+
require 'pi_resources/railtie' if defined? Rails
|
10
|
+
require 'pi_resources/serializer'
|
11
|
+
require 'pi_resources/serializer/base'
|
12
|
+
require 'pi_resources/serializer/exception_serializer'
|
13
|
+
require 'pi_resources/serializer/hash_serializer'
|
14
|
+
require 'pi_resources/serializer/money_serializer'
|
15
|
+
require 'pi_resources/serializer/object_serializer'
|
16
|
+
require 'pi_resources/serializer/resource_errors_serializer'
|
17
|
+
require 'pi_resources/serializer/smart_hash_serializer'
|
18
|
+
require 'pi_resources/serializer/validation_response_serializer'
|
19
|
+
require 'pi_resources/serializer/validation_response'
|
20
|
+
require 'pi_resources/method_not_overridden_error'
|
21
|
+
|
22
|
+
require 'pi_resources/renderers' if defined? Rails
|
23
|
+
require 'pi_resources/resources_controller' if defined? Rails
|
24
|
+
require 'pi_resources/params_helpers' if defined? Rails
|
25
|
+
|
26
|
+
module PiResources
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pi-resources-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Umberto Peserico
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: money
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '7.0'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 7.0.0
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '7.0'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 7.0.0
|
47
|
+
description: Description of PiResource
|
48
|
+
email:
|
49
|
+
- umberto.peserico@pandev.it
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- MIT-LICENSE
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- lib/pi_resources.rb
|
58
|
+
- lib/pi_resources/method_not_overridden_error.rb
|
59
|
+
- lib/pi_resources/params_helpers.rb
|
60
|
+
- lib/pi_resources/railtie.rb
|
61
|
+
- lib/pi_resources/renderers.rb
|
62
|
+
- lib/pi_resources/resources_controller.rb
|
63
|
+
- lib/pi_resources/serializer.rb
|
64
|
+
- lib/pi_resources/serializer/base.rb
|
65
|
+
- lib/pi_resources/serializer/exception_serializer.rb
|
66
|
+
- lib/pi_resources/serializer/hash_serializer.rb
|
67
|
+
- lib/pi_resources/serializer/money_serializer.rb
|
68
|
+
- lib/pi_resources/serializer/object_serializer.rb
|
69
|
+
- lib/pi_resources/serializer/resource_errors_serializer.rb
|
70
|
+
- lib/pi_resources/serializer/smart_hash_serializer.rb
|
71
|
+
- lib/pi_resources/serializer/validation_response.rb
|
72
|
+
- lib/pi_resources/serializer/validation_response_serializer.rb
|
73
|
+
- lib/pi_resources/version.rb
|
74
|
+
- lib/tasks/pi_resource_tasks.rake
|
75
|
+
homepage: https://bitbucket.org/pandev-srl/pi-resources-rails
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata:
|
79
|
+
rubygems_mfa_required: 'true'
|
80
|
+
homepage_uri: https://bitbucket.org/pandev-srl/pi-resources-rails
|
81
|
+
source_code_uri: https://bitbucket.org/pandev-srl/pi-resources-rails
|
82
|
+
changelog_uri: https://bitbucket.org/pandev-srl/pi-resources-rails
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 3.1.0
|
92
|
+
- - "<"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '3.3'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubygems_version: 3.4.19
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Summary of PiResource
|
105
|
+
test_files: []
|