routes-analyzer 0.1.2 → 0.1.4
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 +22 -0
 - data/lib/routes/analyzer/middleware.rb +24 -3
 - 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: 79618d6dca3b9246b8023943d2863bc985e3d9674fd56e51aec84ad0c10ebf62
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 07043a67f3c6011aacc34097476444e45c7749ed8134bd71b48bb29ffe47cdff
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 86f3904c67a101f44776d2d583c1f818340b5f79218a9ac0297a3eca2045faaff4142218cdb8e8a1bc4ec68ac122918aa237845dd2ad8a068a4ee2a45804058b
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 43bf9d60f739f769efe4bd35a628a30537298226da1b5b8457f98e726168718361f292663eaa47a1d811baac744d489278a0a969bcb0e13f3792c791be5a9b78
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -7,6 +7,7 @@ A Ruby on Rails plugin that tracks and analyzes route usage in your application. 
     | 
|
| 
       7 
7 
     | 
    
         
             
            ## Features
         
     | 
| 
       8 
8 
     | 
    
         | 
| 
       9 
9 
     | 
    
         
             
            - **Route Usage Tracking**: Automatically tracks which routes are accessed and how often
         
     | 
| 
      
 10 
     | 
    
         
            +
            - **Smart Parameter Handling**: Routes with parameters (like `/users/:id`) are properly grouped under a single pattern instead of creating separate entries for each parameter value
         
     | 
| 
       10 
11 
     | 
    
         
             
            - **Redis Storage**: Uses Redis to store usage statistics efficiently  
         
     | 
| 
       11 
12 
     | 
    
         
             
            - **Configurable Timeframe**: Set custom analysis periods (default 30 days)
         
     | 
| 
       12 
13 
     | 
    
         
             
            - **Comprehensive Reporting**: Shows both used and unused routes
         
     | 
| 
         @@ -132,6 +133,27 @@ The middleware uses Rails' routing system to determine if a route is valid: 
     | 
|
| 
       132 
133 
     | 
    
         | 
| 
       133 
134 
     | 
    
         
             
            This approach ensures that only routes you've intentionally defined are included in the usage analysis.
         
     | 
| 
       134 
135 
     | 
    
         | 
| 
      
 136 
     | 
    
         
            +
            ## Parameterized Route Handling
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
            The gem intelligently handles routes with parameters by tracking them under their route pattern rather than individual parameter values:
         
     | 
| 
      
 139 
     | 
    
         
            +
             
     | 
| 
      
 140 
     | 
    
         
            +
            - **Route Definition**: `/users/:id` in `routes.rb`
         
     | 
| 
      
 141 
     | 
    
         
            +
            - **Actual Requests**: `/users/123`, `/users/456`, `/users/789`
         
     | 
| 
      
 142 
     | 
    
         
            +
            - **Tracked As**: Single entry for `/users/:id` with combined statistics
         
     | 
| 
      
 143 
     | 
    
         
            +
             
     | 
| 
      
 144 
     | 
    
         
            +
            This means that accessing `/users/123` three times and `/users/456` two times will show up as 5 total accesses to the `/users/:id` route pattern, not as separate routes.
         
     | 
| 
      
 145 
     | 
    
         
            +
             
     | 
| 
      
 146 
     | 
    
         
            +
            **Example Output:**
         
     | 
| 
      
 147 
     | 
    
         
            +
            ```
         
     | 
| 
      
 148 
     | 
    
         
            +
            COUNT    ROUTE                   METHOD    CONTROLLER#ACTION
         
     | 
| 
      
 149 
     | 
    
         
            +
            --------------------------------------------------------------
         
     | 
| 
      
 150 
     | 
    
         
            +
            15       /users/:id             GET       users#show
         
     | 
| 
      
 151 
     | 
    
         
            +
            8        /users/:id/profile     GET       users#profile
         
     | 
| 
      
 152 
     | 
    
         
            +
            23       /posts/:id             GET       posts#show
         
     | 
| 
      
 153 
     | 
    
         
            +
            ```
         
     | 
| 
      
 154 
     | 
    
         
            +
             
     | 
| 
      
 155 
     | 
    
         
            +
            This grouping provides much more meaningful insights into which route patterns are being used in your application.
         
     | 
| 
      
 156 
     | 
    
         
            +
             
     | 
| 
       135 
157 
     | 
    
         
             
            ## Data Structure
         
     | 
| 
       136 
158 
     | 
    
         | 
| 
       137 
159 
     | 
    
         
             
            For each tracked route, the following data is stored in Redis:
         
     | 
| 
         @@ -92,13 +92,17 @@ module Routes 
     | 
|
| 
       92 
92 
     | 
    
         
             
                      controller = env["action_controller.instance"]
         
     | 
| 
       93 
93 
     | 
    
         
             
                      action = controller.action_name
         
     | 
| 
       94 
94 
     | 
    
         
             
                      controller_name = controller.controller_name
         
     | 
| 
      
 95 
     | 
    
         
            +
                      method = request.request_method.upcase
         
     | 
| 
       95 
96 
     | 
    
         | 
| 
       96 
     | 
    
         
            -
                      #  
     | 
| 
       97 
     | 
    
         
            -
                      route_path =  
     | 
| 
      
 97 
     | 
    
         
            +
                      # Find the route pattern instead of using the actual path
         
     | 
| 
      
 98 
     | 
    
         
            +
                      route_path = find_route_pattern(controller_name, action, method)
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
                      # Fallback to actual path if pattern not found
         
     | 
| 
      
 101 
     | 
    
         
            +
                      route_path ||= request.path_info
         
     | 
| 
       98 
102 
     | 
    
         | 
| 
       99 
103 
     | 
    
         
             
                      {
         
     | 
| 
       100 
104 
     | 
    
         
             
                        route: route_path,
         
     | 
| 
       101 
     | 
    
         
            -
                        method:  
     | 
| 
      
 105 
     | 
    
         
            +
                        method: method,
         
     | 
| 
       102 
106 
     | 
    
         
             
                        controller: controller_name,
         
     | 
| 
       103 
107 
     | 
    
         
             
                        action: action
         
     | 
| 
       104 
108 
     | 
    
         
             
                      }
         
     | 
| 
         @@ -108,6 +112,23 @@ module Routes 
     | 
|
| 
       108 
112 
     | 
    
         
             
                    nil
         
     | 
| 
       109 
113 
     | 
    
         
             
                  end
         
     | 
| 
       110 
114 
     | 
    
         | 
| 
      
 115 
     | 
    
         
            +
                  def find_route_pattern(controller_name, action, method)
         
     | 
| 
      
 116 
     | 
    
         
            +
                    return nil unless defined?(Rails) && Rails.application
         
     | 
| 
      
 117 
     | 
    
         
            +
             
     | 
| 
      
 118 
     | 
    
         
            +
                    Rails.application.routes.routes.each do |route|
         
     | 
| 
      
 119 
     | 
    
         
            +
                      if route.defaults[:controller] == controller_name &&
         
     | 
| 
      
 120 
     | 
    
         
            +
                         route.defaults[:action] == action &&
         
     | 
| 
      
 121 
     | 
    
         
            +
                         route.verb == method
         
     | 
| 
      
 122 
     | 
    
         
            +
                        return route.path.spec.to_s.gsub(/\(\.:format\)$/, "")
         
     | 
| 
      
 123 
     | 
    
         
            +
                      end
         
     | 
| 
      
 124 
     | 
    
         
            +
                    end
         
     | 
| 
      
 125 
     | 
    
         
            +
             
     | 
| 
      
 126 
     | 
    
         
            +
                    nil
         
     | 
| 
      
 127 
     | 
    
         
            +
                  rescue => e
         
     | 
| 
      
 128 
     | 
    
         
            +
                    Rails.logger.warn "Routes::Analyzer: Failed to find route pattern: #{e.message}"
         
     | 
| 
      
 129 
     | 
    
         
            +
                    nil
         
     | 
| 
      
 130 
     | 
    
         
            +
                  end
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
       111 
132 
     | 
    
         
             
                  def build_redis_key(route, method)
         
     | 
| 
       112 
133 
     | 
    
         
             
                    "#{configuration.redis_key_prefix}:#{method}:#{route.gsub('/', ':')}"
         
     | 
| 
       113 
134 
     | 
    
         
             
                  end
         
     |