mini_api 0.1.1 → 0.1.3

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: 14a8f16f154f4f5c49499da422027fefd65c8c89b19ef9b59aef90dcf2f13f8c
4
- data.tar.gz: 061a3a10595c82455343eab9c59f4bcf2a734ec5a2a04d69bd8ac5575b682096
3
+ metadata.gz: d964fdc02d20eb94726fd7ce97316402f97369aa0639f6ae8f4c4761d1359ed4
4
+ data.tar.gz: 76c2ac644e655cec1b23199c843c56008b23730e671d40499dac66c305f34f6f
5
5
  SHA512:
6
- metadata.gz: f295a7cf808f32dc15f06766259116e95f66e4ca7143777a4d32b20c6f11cb2e33a71355e474ac29d8c46e529cf1e4dc481fe9a1a1027f72ab4e0488116bcdaf
7
- data.tar.gz: 85dc53237ece21101a3dd3ffd346e42e52327dabd3f06cc0592452cc69437fe95b5941f77913642c1ce61c33a359161edb08445a4d065bb929b401bb2cdf33c3
6
+ metadata.gz: a84f16e06ce682471041e1a6c03cc9d991f482db377854cd0c6a533b570867654bf77cea9e7074a8a5ee2f7f219ae3fb22d192dc8ad3d8ac4bdc0b4b0cc57fe2
7
+ data.tar.gz: 85d339e62fa8195be1d02289e6fb00759b1a401cc428029a2cae6deb39403d24cf34f487bc21f493eed3d67c8f904f1ffbeb139f8191946490c7065ae53821f7
data/README.md CHANGED
@@ -3,12 +3,13 @@
3
3
  [![Test Coverage](https://api.codeclimate.com/v1/badges/ec2939be693459b7ce4d/test_coverage)](https://codeclimate.com/github/leoncruz/api-responder/test_coverage)
4
4
 
5
5
  # Mini Api
6
- A gem to standardize json responses in Rails applications, highly inspired on [Responders](https:github.com/heartcombo/responders)
6
+ A gem to standardize json responses in Rails applications, highly inspired on [Responders](https://github.com/heartcombo/responders)
7
7
 
8
8
  ## Table of Contents
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: @resource,
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniApi
4
+ class CaseTransformOptionInvalid < StandardError; end
5
+ 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
@@ -23,9 +24,15 @@ module MiniApi
23
24
  if resource_has_errors?
24
25
  { errors: @resource.errors.messages }.merge(body)
25
26
  else
26
- { data: serialiable_body(@resource) }.merge(body)
27
+ { data: serialiable_body(@resource).as_json }.merge(body)
27
28
  end
28
29
 
30
+ # This is for an problem with ActiveModelSerializer that adds an error
31
+ # attribute when resource is an ActiveModel instance
32
+ body[:data] = body[:data].except('errors') if body[:data]&.key?('errors')
33
+
34
+ body = CaseTransform.response_keys(body)
35
+
29
36
  @controller.render json: body, status: status_code
30
37
  end
31
38
 
@@ -40,9 +47,9 @@ module MiniApi
40
47
 
41
48
  return :unprocessable_entity if resource_has_errors?
42
49
 
43
- return :created if @resource.previously_new_record?
50
+ return :created if previously_new_record?
44
51
 
45
- return :no_content unless @resource.persisted?
52
+ return :no_content if destroyed?
46
53
 
47
54
  :ok
48
55
  end
@@ -57,5 +64,17 @@ module MiniApi
57
64
  default: ''
58
65
  )
59
66
  end
67
+
68
+ def previously_new_record?
69
+ return true if @resource.is_a?(ActiveRecord::Base) && @resource.previously_new_record?
70
+
71
+ false
72
+ end
73
+
74
+ def destroyed?
75
+ return true if @resource.is_a?(ActiveRecord::Base) && !@resource.persisted?
76
+
77
+ false
78
+ end
60
79
  end
61
80
  end
@@ -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'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniApi
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.3'
5
5
  end
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leon Cruz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-17 00:00:00.000000000 Z
11
+ date: 2023-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -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