Class: Securial::Middleware::TransformResponseKeys

Inherits:
Object
  • Object
show all
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

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`.

Examples:

middleware = TransformResponseKeys.new(app)

Parameters:

  • app (#call)

    The Rack application to wrap

  • format (Symbol)

    Deprecated: The key format to use (defaults to :lowerCamelCase)



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.

Examples:

JSON transformation

# Original response: { "first_name": "John", "last_name": "Doe" }
# Transformed response: { "firstName": "John", "lastName": "Doe" }

Non-JSON responses

# HTML, XML, and other content types pass through unchanged

Empty or malformed JSON handling

# Empty responses and invalid JSON are left unchanged

Parameters:

  • env (Hash)

    The Rack environment hash

Returns:

  • (Array)

    The Rack response array [status, headers, body] with transformed keys



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.

Examples:

body = extract_body(response)
# => '{"user_name":"john","email":"[email protected]"}'

Parameters:

  • response (#each)

    The response body object (responds to #each)

Returns:

  • (String)

    The complete response body as a string



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.

Parameters:

  • headers (Hash)

    The response headers hash

Returns:

  • (Boolean)

    true if the response has JSON content type



100
101
102
# File 'lib/securial/middleware/transform_response_keys.rb', line 100

def json_response?(headers)
  headers["Content-Type"]&.include?("application/json")
end