Class: Securial::Middleware::TransformResponseKeys
- Inherits:
-
Object
- Object
- Securial::Middleware::TransformResponseKeys
- Defined in:
- lib/securial/middleware/transform_response_keys.rb
Overview
Rack middleware that transforms JSON response keys to configured format.
This middleware enables Rails applications to output responses in the naming convention expected by client applications (JavaScript, mobile apps, etc.) while maintaining Ruby’s snake_case internally.
Instance Method Summary collapse
-
#call(env) ⇒ Array
Processes the response and transforms JSON keys if applicable.
-
#extract_body(response) ⇒ String
private
private
Extracts the complete response body from the response object.
-
#initialize(app) ⇒ TransformResponseKeys
constructor
Initializes the middleware with the Rack application and optional format.
-
#json_response?(headers) ⇒ Boolean
private
private
Checks if the response contains JSON content.
Constructor Details
#initialize(app) ⇒ TransformResponseKeys
Initializes the middleware with the Rack application and optional format.
The format parameter is deprecated in favor of the global configuration setting ‘Securial.configuration.response_keys_format`.
47 48 49 |
# File 'lib/securial/middleware/transform_response_keys.rb', line 47 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Array
Processes the response and transforms JSON keys if applicable.
Intercepts JSON responses, parses the body, transforms all keys to the configured format using the KeyTransformer helper, and replaces the response body with the transformed JSON. Non-JSON responses pass through unchanged.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/securial/middleware/transform_response_keys.rb', line 71 def call(env) status, headers, response = @app.call(env) if json_response?(headers) body = extract_body(response) if body.present? format = Securial.configuration.response_keys_format transformed = Securial::Helpers::KeyTransformer.deep_transform_keys(JSON.parse(body)) do |key| Securial::Helpers::KeyTransformer.camelize(key, format) end new_body = [JSON.generate(transformed)] headers["Content-Length"] = new_body.first.bytesize.to_s return [status, headers, new_body] end end [status, headers, response] end |
#extract_body(response) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extracts the complete response body from the response object.
Iterates through the response body parts and concatenates them into a single string for JSON parsing and transformation.
117 118 119 120 121 |
# File 'lib/securial/middleware/transform_response_keys.rb', line 117 def extract_body(response) response_body = "" response.each { |part| response_body << part } response_body end |
#json_response?(headers) ⇒ Boolean (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks if the response contains JSON content.
100 101 102 |
# File 'lib/securial/middleware/transform_response_keys.rb', line 100 def json_response?(headers) headers["Content-Type"]&.include?("application/json") end |