routes-analyzer 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0df5dfa530f0ff7771563a035f35bd225a1b7d20e01c87aa626c87fa60a94a4
4
- data.tar.gz: 1683d8d3a06202ce56e266bdd20b0d8805a08fc9eac68d9041be507fe78b367a
3
+ metadata.gz: d59454435cfcca8978b29560a38ecd2457cdfd1ac6486be5a71fa9c9b24ea443
4
+ data.tar.gz: 9c15d8ccb50eb9a2acc1a70c525deede37594200aca4b2bc1f08f9ab857a36ec
5
5
  SHA512:
6
- metadata.gz: ddec876f42e606cca4f39b24ff3c2d442ae1d4ec635de8e6abba2434be1ca22629abe98a24750c048559ce67de9ec7c6042bb8481f8a87790508fcda5e28165d
7
- data.tar.gz: 867674e3c3c49f0ed810d78b6871092261b815e5f6fe696de450973cadd9de6dd8ecdf7e759af0867245a8a2e92bba5612657f0f623110744d4db72dd5ea3f0e
6
+ metadata.gz: a85f24d238c05674ae36a97c30f36aa2d2913c77d53a2907c39a99203c2c1d77e93f86533c8cbbcbad7abbf0b56835e468906e04548075d609a0716ac42132bd
7
+ data.tar.gz: bded888809fdda525cf230a50a9bad7fce9e705716d325257f931f2882524a10e6338d46ec820e1cb67e5c9f674ec89011cfde4ea19283edce0795fba943448e
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Routes::Analyzer
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/routes-analyzer.svg)](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. **Request Tracking**: Each HTTP request is analyzed to extract route information
113
- 3. **Redis Storage**: Usage data is stored in Redis with the following structure:
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
- 4. **Data Expiration**: Redis keys automatically expire after the configured timeframe plus a buffer period
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
 
@@ -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"]
@@ -1,5 +1,5 @@
1
1
  module Routes
2
2
  module Analyzer
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregorio Galante