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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d59454435cfcca8978b29560a38ecd2457cdfd1ac6486be5a71fa9c9b24ea443
4
- data.tar.gz: 9c15d8ccb50eb9a2acc1a70c525deede37594200aca4b2bc1f08f9ab857a36ec
3
+ metadata.gz: 79618d6dca3b9246b8023943d2863bc985e3d9674fd56e51aec84ad0c10ebf62
4
+ data.tar.gz: 07043a67f3c6011aacc34097476444e45c7749ed8134bd71b48bb29ffe47cdff
5
5
  SHA512:
6
- metadata.gz: a85f24d238c05674ae36a97c30f36aa2d2913c77d53a2907c39a99203c2c1d77e93f86533c8cbbcbad7abbf0b56835e468906e04548075d609a0716ac42132bd
7
- data.tar.gz: bded888809fdda525cf230a50a9bad7fce9e705716d325257f931f2882524a10e6338d46ec820e1cb67e5c9f674ec89011cfde4ea19283edce0795fba943448e
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
- # Build route pattern from request path, removing query parameters
97
- route_path = request.path_info
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: request.request_method.upcase,
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
@@ -1,5 +1,5 @@
1
1
  module Routes
2
2
  module Analyzer
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routes-analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregorio Galante