Class: Securial::Middleware::TransformRequestKeys
- Inherits:
-
Object
- Object
- Securial::Middleware::TransformRequestKeys
- Defined in:
- lib/securial/middleware/transform_request_keys.rb
Overview
Rack middleware that transforms JSON request body keys to snake_case.
This middleware enables Rails applications to accept camelCase JSON from frontend applications while automatically converting keys to Ruby’s snake_case convention before they reach the application.
Instance Method Summary collapse
-
#call(env) ⇒ Array
Processes the request and transforms JSON body keys if applicable.
-
#initialize(app) ⇒ TransformRequestKeys
constructor
Initializes the middleware with the Rack application.
-
#json_request?(env) ⇒ Boolean
private
private
Checks if the request contains JSON content.
-
#read_request_body(req) ⇒ String
private
private
Reads and rewinds the request body.
-
#replace_request_body(env, transformed_data) ⇒ void
private
private
Replaces the request body with transformed JSON.
-
#request_has_body?(req) ⇒ Boolean
private
private
Checks if the request has a body to process.
-
#transform_json_body(env) ⇒ void
private
private
Transforms JSON request body keys to snake_case.
-
#transform_keys_to_snake_case(json_data) ⇒ Object
private
private
Transforms all keys in the JSON structure to snake_case.
Constructor Details
#initialize(app) ⇒ TransformRequestKeys
Initializes the middleware with the Rack application.
40 41 42 |
# File 'lib/securial/middleware/transform_request_keys.rb', line 40 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Array
Processes the request and transforms JSON body keys if applicable.
Intercepts JSON requests, parses the body, transforms all keys to snake_case using the KeyTransformer helper, and replaces the request body with the transformed JSON. Non-JSON requests pass through unchanged.
64 65 66 67 68 69 70 |
# File 'lib/securial/middleware/transform_request_keys.rb', line 64 def call(env) if json_request?(env) transform_json_body(env) end @app.call(env) end |
#json_request?(env) ⇒ 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 request contains JSON content.
80 81 82 |
# File 'lib/securial/middleware/transform_request_keys.rb', line 80 def json_request?(env) env["CONTENT_TYPE"]&.include?("application/json") end |
#read_request_body(req) ⇒ 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.
Reads and rewinds the request body.
126 127 128 129 130 |
# File 'lib/securial/middleware/transform_request_keys.rb', line 126 def read_request_body(req) raw = req.body.read req.body.rewind raw end |
#replace_request_body(env, transformed_data) ⇒ void (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.
This method returns an undefined value.
Replaces the request body with transformed JSON.
151 152 153 154 155 |
# File 'lib/securial/middleware/transform_request_keys.rb', line 151 def replace_request_body(env, transformed_data) new_body = JSON.dump(transformed_data) env["rack.input"] = StringIO.new(new_body) env["rack.input"].rewind end |
#request_has_body?(req) ⇒ 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 request has a body to process.
116 117 118 |
# File 'lib/securial/middleware/transform_request_keys.rb', line 116 def request_has_body?(req) (req.body&.size || 0) > 0 end |
#transform_json_body(env) ⇒ void (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.
This method returns an undefined value.
Transforms JSON request body keys to snake_case.
Reads the request body, parses it as JSON, transforms all keys to snake_case, and replaces the original body with the transformed JSON. Gracefully handles empty bodies and malformed JSON.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/securial/middleware/transform_request_keys.rb', line 94 def transform_json_body(env) req = Rack::Request.new(env) return unless request_has_body?(req) raw_body = read_request_body(req) begin parsed_json = JSON.parse(raw_body) transformed_json = transform_keys_to_snake_case(parsed_json) replace_request_body(env, transformed_json) rescue JSON::ParserError # Malformed JSON - leave unchanged for application to handle end end |
#transform_keys_to_snake_case(json_data) ⇒ Object (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.
Transforms all keys in the JSON structure to snake_case.
138 139 140 141 142 |
# File 'lib/securial/middleware/transform_request_keys.rb', line 138 def transform_keys_to_snake_case(json_data) Securial::Helpers::KeyTransformer.deep_transform_keys(json_data) do |key| Securial::Helpers::KeyTransformer.underscore(key) end end |