routes-analyzer 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/README.md +43 -3
 - data/lib/routes/analyzer/middleware.rb +29 -0
 - data/lib/routes/analyzer/version.rb +1 -1
 - metadata +1 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: d59454435cfcca8978b29560a38ecd2457cdfd1ac6486be5a71fa9c9b24ea443
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 9c15d8ccb50eb9a2acc1a70c525deede37594200aca4b2bc1f08f9ab857a36ec
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: a85f24d238c05674ae36a97c30f36aa2d2913c77d53a2907c39a99203c2c1d77e93f86533c8cbbcbad7abbf0b56835e468906e04548075d609a0716ac42132bd
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: bded888809fdda525cf230a50a9bad7fce9e705716d325257f931f2882524a10e6338d46ec820e1cb67e5c9f674ec89011cfde4ea19283edce0795fba943448e
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,5 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # Routes::Analyzer
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
      
 3 
     | 
    
         
            +
            [](https://badge.fury.io/rb/routes-analyzer)
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
       3 
5 
     | 
    
         
             
            A Ruby on Rails plugin that tracks and analyzes route usage in your application. It uses Redis to store route access patterns and provides insights into which routes are being used and which are not.
         
     | 
| 
       4 
6 
     | 
    
         | 
| 
       5 
7 
     | 
    
         
             
            ## Features
         
     | 
| 
         @@ -109,12 +111,26 @@ Displays current configuration and tests Redis connectivity. 
     | 
|
| 
       109 
111 
     | 
    
         
             
            ## How It Works
         
     | 
| 
       110 
112 
     | 
    
         | 
| 
       111 
113 
     | 
    
         
             
            1. **Middleware Integration**: The plugin automatically adds middleware to your Rails application
         
     | 
| 
       112 
     | 
    
         
            -
            2. ** 
     | 
| 
       113 
     | 
    
         
            -
             
     | 
| 
      
 114 
     | 
    
         
            +
            2. **Route Filtering**: Only routes explicitly defined in `routes.rb` are tracked. This ensures that:
         
     | 
| 
      
 115 
     | 
    
         
            +
               - Undefined routes (404 errors) are not tracked
         
     | 
| 
      
 116 
     | 
    
         
            +
               - Catch-all routes that handle unknown paths don't pollute the data
         
     | 
| 
      
 117 
     | 
    
         
            +
               - Only legitimate application routes are analyzed
         
     | 
| 
      
 118 
     | 
    
         
            +
            3. **Request Tracking**: Each valid HTTP request is analyzed to extract route information
         
     | 
| 
      
 119 
     | 
    
         
            +
            4. **Redis Storage**: Usage data is stored in Redis with the following structure:
         
     | 
| 
       114 
120 
     | 
    
         
             
               - Route path and HTTP method
         
     | 
| 
       115 
121 
     | 
    
         
             
               - Access count within the timeframe
         
     | 
| 
       116 
122 
     | 
    
         
             
               - Last access timestamp
         
     | 
| 
       117 
     | 
    
         
            -
             
     | 
| 
      
 123 
     | 
    
         
            +
            5. **Data Expiration**: Redis keys automatically expire after the configured timeframe plus a buffer period
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
            ## Route Detection
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
            The middleware uses Rails' routing system to determine if a route is valid:
         
     | 
| 
      
 128 
     | 
    
         
            +
             
     | 
| 
      
 129 
     | 
    
         
            +
            - **Path Parameters Check**: Verifies that Rails recognized the route and set path parameters
         
     | 
| 
      
 130 
     | 
    
         
            +
            - **Route Recognition**: Uses `Rails.application.routes.recognize_path` to confirm the route exists in `routes.rb`
         
     | 
| 
      
 131 
     | 
    
         
            +
            - **Error Handling**: Gracefully handles routing errors and invalid requests without tracking them
         
     | 
| 
      
 132 
     | 
    
         
            +
             
     | 
| 
      
 133 
     | 
    
         
            +
            This approach ensures that only routes you've intentionally defined are included in the usage analysis.
         
     | 
| 
       118 
134 
     | 
    
         | 
| 
       119 
135 
     | 
    
         
             
            ## Data Structure
         
     | 
| 
       120 
136 
     | 
    
         | 
| 
         @@ -142,6 +158,30 @@ For each tracked route, the following data is stored in Redis: 
     | 
|
| 
       142 
158 
     | 
    
         
             
            4. Push to the branch (`git push origin my-new-feature`)
         
     | 
| 
       143 
159 
     | 
    
         
             
            5. Create a Pull Request
         
     | 
| 
       144 
160 
     | 
    
         | 
| 
      
 161 
     | 
    
         
            +
            ## Testing
         
     | 
| 
      
 162 
     | 
    
         
            +
             
     | 
| 
      
 163 
     | 
    
         
            +
            Run the test suite to ensure everything is working correctly:
         
     | 
| 
      
 164 
     | 
    
         
            +
             
     | 
| 
      
 165 
     | 
    
         
            +
            ```bash
         
     | 
| 
      
 166 
     | 
    
         
            +
            ruby bin/test
         
     | 
| 
      
 167 
     | 
    
         
            +
            ```
         
     | 
| 
      
 168 
     | 
    
         
            +
             
     | 
| 
      
 169 
     | 
    
         
            +
            ## Rubocop check
         
     | 
| 
      
 170 
     | 
    
         
            +
             
     | 
| 
      
 171 
     | 
    
         
            +
            Run Rubocop to check code style:
         
     | 
| 
      
 172 
     | 
    
         
            +
             
     | 
| 
      
 173 
     | 
    
         
            +
            ```bash
         
     | 
| 
      
 174 
     | 
    
         
            +
            ruby bin/rubocop
         
     | 
| 
      
 175 
     | 
    
         
            +
            ```
         
     | 
| 
      
 176 
     | 
    
         
            +
             
     | 
| 
      
 177 
     | 
    
         
            +
            ## Publishing
         
     | 
| 
      
 178 
     | 
    
         
            +
             
     | 
| 
      
 179 
     | 
    
         
            +
            To publish a new version of the gem, update the version number in `lib/routes/analyzer/version.rb` and run:
         
     | 
| 
      
 180 
     | 
    
         
            +
             
     | 
| 
      
 181 
     | 
    
         
            +
            ```bash
         
     | 
| 
      
 182 
     | 
    
         
            +
            ruby bin/publish
         
     | 
| 
      
 183 
     | 
    
         
            +
            ```
         
     | 
| 
      
 184 
     | 
    
         
            +
             
     | 
| 
       145 
185 
     | 
    
         
             
            ## License
         
     | 
| 
       146 
186 
     | 
    
         | 
| 
       147 
187 
     | 
    
         
             
            The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
         
     | 
| 
         @@ -23,6 +23,9 @@ module Routes 
     | 
|
| 
       23 
23 
     | 
    
         
             
                    return false unless Routes::Analyzer.track_usage?
         
     | 
| 
       24 
24 
     | 
    
         
             
                    return false unless env["action_controller.instance"]
         
     | 
| 
       25 
25 
     | 
    
         | 
| 
      
 26 
     | 
    
         
            +
                    # Check if the route is actually defined in routes.rb
         
     | 
| 
      
 27 
     | 
    
         
            +
                    return false unless route_defined_in_rails?(env)
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
       26 
29 
     | 
    
         
             
                    true
         
     | 
| 
       27 
30 
     | 
    
         
             
                  rescue => e
         
     | 
| 
       28 
31 
     | 
    
         
             
                    Rails.logger.warn "Routes::Analyzer: Failed to check tracking conditions: #{e.message}"
         
     | 
| 
         @@ -57,6 +60,32 @@ module Routes 
     | 
|
| 
       57 
60 
     | 
    
         
             
                    Rails.logger.warn "Routes::Analyzer: Failed to track route usage: #{e.message}"
         
     | 
| 
       58 
61 
     | 
    
         
             
                  end
         
     | 
| 
       59 
62 
     | 
    
         | 
| 
      
 63 
     | 
    
         
            +
                  def route_defined_in_rails?(env)
         
     | 
| 
      
 64 
     | 
    
         
            +
                    request = Rack::Request.new(env)
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                    # First check: if Rails didn't set path_parameters, it means the route wasn't recognized
         
     | 
| 
      
 67 
     | 
    
         
            +
                    # This happens when Rails can't match the request to any defined route
         
     | 
| 
      
 68 
     | 
    
         
            +
                    path_params = env["action_dispatch.request.path_parameters"]
         
     | 
| 
      
 69 
     | 
    
         
            +
                    return false unless path_params
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                    # Second check: verify that Rails can recognize this path/method combination
         
     | 
| 
      
 72 
     | 
    
         
            +
                    # This ensures the route is actually defined in routes.rb and not just handled by a catch-all
         
     | 
| 
      
 73 
     | 
    
         
            +
                    route_exists = Rails.application.routes.recognize_path(
         
     | 
| 
      
 74 
     | 
    
         
            +
                      request.path_info,
         
     | 
| 
      
 75 
     | 
    
         
            +
                      method: request.request_method.downcase.to_sym
         
     | 
| 
      
 76 
     | 
    
         
            +
                    )
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
                    return true if route_exists
         
     | 
| 
      
 79 
     | 
    
         
            +
                    false
         
     | 
| 
      
 80 
     | 
    
         
            +
                  rescue ActionController::RoutingError, NoMethodError
         
     | 
| 
      
 81 
     | 
    
         
            +
                    # If recognize_path raises RoutingError, the route is not defined in routes.rb
         
     | 
| 
      
 82 
     | 
    
         
            +
                    # NoMethodError can occur if the controller/action doesn't exist
         
     | 
| 
      
 83 
     | 
    
         
            +
                    false
         
     | 
| 
      
 84 
     | 
    
         
            +
                  rescue => e
         
     | 
| 
      
 85 
     | 
    
         
            +
                    Rails.logger.warn "Routes::Analyzer: Error checking route definition: #{e.message}"
         
     | 
| 
      
 86 
     | 
    
         
            +
                    false
         
     | 
| 
      
 87 
     | 
    
         
            +
                  end
         
     | 
| 
      
 88 
     | 
    
         
            +
             
     | 
| 
       60 
89 
     | 
    
         
             
                  def extract_route_info(env, request)
         
     | 
| 
       61 
90 
     | 
    
         
             
                    # Get the matched route from Rails
         
     | 
| 
       62 
91 
     | 
    
         
             
                    if env["action_controller.instance"]
         
     |