mini_api 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/mini_api/case_transform.rb +33 -0
- data/lib/mini_api/config.rb +11 -0
- data/lib/mini_api/default_responder.rb +21 -1
- data/lib/mini_api/exceptions/case_transform_option_invalid.rb +5 -0
- data/lib/mini_api/model_responder.rb +3 -0
- data/lib/mini_api/relation_responder.rb +9 -0
- data/lib/mini_api/version.rb +1 -1
- data/lib/mini_api.rb +14 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d964fdc02d20eb94726fd7ce97316402f97369aa0639f6ae8f4c4761d1359ed4
|
4
|
+
data.tar.gz: 76c2ac644e655cec1b23199c843c56008b23730e671d40499dac66c305f34f6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a84f16e06ce682471041e1a6c03cc9d991f482db377854cd0c6a533b570867654bf77cea9e7074a8a5ee2f7f219ae3fb22d192dc8ad3d8ac4bdc0b4b0cc57fe2
|
7
|
+
data.tar.gz: 85d339e62fa8195be1d02289e6fb00759b1a401cc428029a2cae6deb39403d24cf34f487bc21f493eed3d67c8f904f1ffbeb139f8191946490c7065ae53821f7
|
data/README.md
CHANGED
@@ -9,6 +9,7 @@ A gem to standardize json responses in Rails applications, highly inspired on [R
|
|
9
9
|
- [Usage](#usage)
|
10
10
|
- [Respondering json](#respondering-json)
|
11
11
|
- [Success and failure actions](#success-and-failure-actions)
|
12
|
+
- [Transform keys](#transform-keys)
|
12
13
|
- [Overriding response](#overriding-response)
|
13
14
|
- [Pagination](#pagination)
|
14
15
|
- [Contributing](#contributing)
|
@@ -141,6 +142,20 @@ The `message` key is different based on actions on informed model: create, updat
|
|
141
142
|
|
142
143
|
You can respond any type of data, but ActiveRecord/ActiveModel::Model and ActiveRecord::Relation has a special treatment as shown above
|
143
144
|
|
145
|
+
### Transform keys
|
146
|
+
|
147
|
+
It is possible to transform the keys of request and response. By default, will transform to `snake_case`, but the possible values are `snake_case`, `camel_lower` and `camel_case`
|
148
|
+
|
149
|
+
To change the transform operation, simply adds a initializer on initilizations folder with content:
|
150
|
+
```ruby
|
151
|
+
MiniApi::Config.configure do |config|
|
152
|
+
config.transform_params_keys_to = :snake_case
|
153
|
+
config.transform_response_keys_to = :camel_lower
|
154
|
+
end
|
155
|
+
```
|
156
|
+
The option `transform_params_keys_to` will transform request params.
|
157
|
+
The option `transform_response_keys_to` will transform responses.
|
158
|
+
|
144
159
|
## Overriding response
|
145
160
|
|
146
161
|
You can override the `status`, `message` and `sucess` keys simply informing values to `render_json`. Example:
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mini_api/config'
|
4
|
+
require 'mini_api/exceptions/case_transform_option_invalid'
|
5
|
+
|
6
|
+
module MiniApi
|
7
|
+
module CaseTransform
|
8
|
+
module_function
|
9
|
+
|
10
|
+
def transform(object, transform_to = :snake_case)
|
11
|
+
object.deep_transform_keys do |key|
|
12
|
+
case transform_to
|
13
|
+
when :camel_case
|
14
|
+
key.to_s.camelize
|
15
|
+
when :camel_lower
|
16
|
+
key.to_s.camelize(:lower)
|
17
|
+
when :snake_case
|
18
|
+
key.to_s.underscore
|
19
|
+
else
|
20
|
+
raise CaseTransformOptionInvalid, "option #{transform_to} is not supported."
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def request_params_keys(params)
|
26
|
+
transform(params, Config.transform_params_keys_to)
|
27
|
+
end
|
28
|
+
|
29
|
+
def response_keys(response)
|
30
|
+
transform(response, Config.transform_response_keys_to)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MiniApi
|
4
|
+
class Config
|
5
|
+
include ActiveSupport::Configurable
|
6
|
+
|
7
|
+
# permitted values are: [:camel_case, :camel_lower, snake_case]
|
8
|
+
config_accessor :transform_params_keys_to, instance_accessor: false, default: :snake_case
|
9
|
+
config_accessor :transform_response_keys_to, instance_accessor: false, default: :snake_case
|
10
|
+
end
|
11
|
+
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'mini_api/case_transform'
|
4
|
+
|
3
5
|
module MiniApi
|
4
6
|
class DefaultResponder
|
5
7
|
def initialize(controller, resource, options = {})
|
@@ -11,14 +13,32 @@ module MiniApi
|
|
11
13
|
def respond
|
12
14
|
success = @options[:success] != false
|
13
15
|
|
16
|
+
data = transform_keys
|
17
|
+
|
14
18
|
@controller.render(
|
15
19
|
json: {
|
16
20
|
success: success,
|
17
|
-
data:
|
21
|
+
data: data,
|
18
22
|
message: @options[:message] || nil
|
19
23
|
},
|
20
24
|
status: @options[:status] || :ok
|
21
25
|
)
|
22
26
|
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def transform_keys
|
31
|
+
return CaseTransform.response_keys(@resource) if @resource.is_a?(Hash)
|
32
|
+
|
33
|
+
return @resource unless @resource.is_a?(Array)
|
34
|
+
|
35
|
+
@resource.map do |item|
|
36
|
+
if item.is_a?(Hash)
|
37
|
+
CaseTransform.response_keys(item)
|
38
|
+
else
|
39
|
+
item
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
23
43
|
end
|
24
44
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'mini_api/serialization'
|
4
|
+
require 'mini_api/case_transform'
|
4
5
|
|
5
6
|
module MiniApi
|
6
7
|
# class to handle json render of ActiveRecord::Base instances and ActiveModel::Model's
|
@@ -30,6 +31,8 @@ module MiniApi
|
|
30
31
|
# attribute when resource is an ActiveModel instance
|
31
32
|
body[:data] = body[:data].except('errors') if body[:data]&.key?('errors')
|
32
33
|
|
34
|
+
body = CaseTransform.response_keys(body)
|
35
|
+
|
33
36
|
@controller.render json: body, status: status_code
|
34
37
|
end
|
35
38
|
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'mini_api/exceptions/kaminari_not_installed'
|
4
4
|
require 'mini_api/serialization'
|
5
|
+
require 'mini_api/case_transform'
|
5
6
|
|
6
7
|
module MiniApi
|
7
8
|
class RelationResponder
|
@@ -16,6 +17,10 @@ module MiniApi
|
|
16
17
|
def respond
|
17
18
|
meta, collection = extract_meta_and_collection
|
18
19
|
|
20
|
+
meta = CaseTransform.response_keys(meta)
|
21
|
+
|
22
|
+
collection = transform_case(collection.as_json)
|
23
|
+
|
19
24
|
@controller.render json: {
|
20
25
|
success: @options[:success] || true,
|
21
26
|
data: collection,
|
@@ -40,6 +45,10 @@ module MiniApi
|
|
40
45
|
]
|
41
46
|
end
|
42
47
|
|
48
|
+
def transform_case(collection)
|
49
|
+
collection.map { |item| CaseTransform.response_keys(item) }
|
50
|
+
end
|
51
|
+
|
43
52
|
def transform_resource_to_collection
|
44
53
|
unless defined?(Kaminari)
|
45
54
|
raise KaminariNotInstalled, 'The Kaminari gem is not installed. Install to perform pagination operations'
|
data/lib/mini_api/version.rb
CHANGED
data/lib/mini_api.rb
CHANGED
@@ -2,9 +2,23 @@
|
|
2
2
|
|
3
3
|
require 'mini_api/railtie'
|
4
4
|
require 'mini_api/responder'
|
5
|
+
require 'mini_api/case_transform'
|
5
6
|
|
6
7
|
# Entrypoint module
|
7
8
|
module MiniApi
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
include CaseTransform
|
13
|
+
|
14
|
+
before_action :transform_params
|
15
|
+
|
16
|
+
def transform_params
|
17
|
+
self.params =
|
18
|
+
ActionController::Parameters.new(CaseTransform.request_params_keys(request.parameters))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
8
22
|
def render_json(resource, options = {})
|
9
23
|
responder = Responder.new(self, resource, options)
|
10
24
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leon Cruz
|
@@ -35,7 +35,10 @@ files:
|
|
35
35
|
- README.md
|
36
36
|
- Rakefile
|
37
37
|
- lib/mini_api.rb
|
38
|
+
- lib/mini_api/case_transform.rb
|
39
|
+
- lib/mini_api/config.rb
|
38
40
|
- lib/mini_api/default_responder.rb
|
41
|
+
- lib/mini_api/exceptions/case_transform_option_invalid.rb
|
39
42
|
- lib/mini_api/exceptions/kaminari_not_installed.rb
|
40
43
|
- lib/mini_api/locales/en.yml
|
41
44
|
- lib/mini_api/model_responder.rb
|