grep_routes 0.0.5 → 0.1.0
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.
- data/Gemfile.lock +1 -1
- data/grep_routes.gemspec +1 -1
- data/lib/grep_routes.rb +39 -39
- data/lib/grep_routes_mock_rack_app.rb +7 -0
- data/spec/basic_routes_spec.rb +6 -0
- data/spec/fixtures/routes.rb +18 -0
- data/spec/fixtures/routes_with_rack_mounts.rb +12 -1
- data/spec/with_rack_mounts_spec.rb +16 -9
- metadata +9 -8
data/Gemfile.lock
CHANGED
data/grep_routes.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "grep_routes"
|
5
|
-
s.version = "0.0
|
5
|
+
s.version = "0.1.0"
|
6
6
|
s.authors = ["Tyler Montgomery"]
|
7
7
|
s.email = ["tyler.a.montgomery@gmail.com"]
|
8
8
|
s.homepage = "http://github.com/ubermajestix/grep_routes"
|
data/lib/grep_routes.rb
CHANGED
@@ -9,6 +9,7 @@ end
|
|
9
9
|
require 'active_support'
|
10
10
|
require 'active_support/core_ext/hash/reverse_merge'
|
11
11
|
require 'active_support/core_ext/enumerable'
|
12
|
+
require 'grep_routes_mock_rack_app'
|
12
13
|
|
13
14
|
# See if the user has the 3.2 or 3.1 version of actionpack for action_dispatch.
|
14
15
|
# If not, blow up.
|
@@ -37,7 +38,6 @@ class GrepRoutes
|
|
37
38
|
@route_file = File.read(path_to_routes_file)
|
38
39
|
@class_name = route_file.match(/(\w+)::Application\.routes\.draw/)[1]
|
39
40
|
self.init_rails_class
|
40
|
-
self.init_rack_apps
|
41
41
|
end
|
42
42
|
|
43
43
|
# To make this fast we don't load your rails app or any of your gems.
|
@@ -60,38 +60,48 @@ class GrepRoutes
|
|
60
60
|
Object.const_get(class_name,Module.new)
|
61
61
|
end
|
62
62
|
|
63
|
-
# If there are any mounted Rack apps in your routes, we'll have to grab their
|
64
|
-
# classes and define #call on them so they look like Rack apps to the router.
|
65
|
-
#
|
66
|
-
# Only the last class needs to have #call defined on it but all the objects
|
67
|
-
# "above" it need to be defined as Modules - basically we define the object
|
68
|
-
# heirarchy.
|
69
|
-
def init_rack_apps
|
70
|
-
rack_apps = route_file.scan(/mount (.+?\s+)/).flatten.map(&:strip)
|
71
|
-
rack_apps.each do |class_name|
|
72
|
-
objects = class_name.split("::")
|
73
|
-
rack_class = objects.pop
|
74
|
-
last_object = Object
|
75
|
-
objects.each do |obj|
|
76
|
-
last_object.const_set(obj, Module.new) unless last_object.const_defined?(obj)
|
77
|
-
last_object = last_object.const_get(obj)
|
78
|
-
end
|
79
|
-
make_rackapp(last_object, rack_class)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
63
|
def make_rackapp(mod, obj)
|
84
|
-
mod.const_set(obj,
|
85
|
-
mod.const_get(obj,Class.new).class_eval do
|
86
|
-
def self.call
|
87
|
-
end
|
88
|
-
end
|
64
|
+
mod.const_set(obj,GrepRoutesMockRackApp) unless mod.const_defined?(obj)
|
89
65
|
end
|
90
66
|
|
91
67
|
# This evals the routes file. After this method is called the RouteSet will
|
92
68
|
# have all of our routes inside it.
|
69
|
+
|
93
70
|
def eval_routes
|
94
|
-
|
71
|
+
# no_method_retries = 3
|
72
|
+
begin
|
73
|
+
eval(route_file)
|
74
|
+
# If a method is not defined on a class, we define it and try again
|
75
|
+
rescue NoMethodError => e
|
76
|
+
match = e.message.match(/undefined method `(.+)' for (.+):Class/)
|
77
|
+
undefined_method = match[1]
|
78
|
+
# TODO there has got to be a better way to do this!
|
79
|
+
obj = Object.const_get(match[2], Class.new).class_eval do
|
80
|
+
method_to_eval = <<-method_to_eval
|
81
|
+
def self.#{undefined_method}
|
82
|
+
GrepRoutesMockRackApp.new
|
83
|
+
end
|
84
|
+
method_to_eval
|
85
|
+
eval(method_to_eval)
|
86
|
+
end
|
87
|
+
retry
|
88
|
+
# If a class is not defined we define it and try again
|
89
|
+
rescue NameError => e
|
90
|
+
class_name = e.message.match(/uninitialized constant (.+)$/)[1].gsub("GrepRoutes::", '')
|
91
|
+
define_objects(class_name)
|
92
|
+
retry
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def define_objects(class_name)
|
97
|
+
objects = class_name.split("::")
|
98
|
+
rack_class = objects.pop
|
99
|
+
last_object = Object
|
100
|
+
objects.each do |obj|
|
101
|
+
last_object.const_set(obj, Module.new) unless last_object.const_defined?(obj)
|
102
|
+
last_object = last_object.const_get(obj)
|
103
|
+
end
|
104
|
+
make_rackapp(last_object, rack_class)
|
95
105
|
end
|
96
106
|
|
97
107
|
# A shortcut to the RouteSet we defined in init_rails_class.
|
@@ -100,26 +110,17 @@ class GrepRoutes
|
|
100
110
|
end
|
101
111
|
|
102
112
|
# Returns an Array of Hashes to make it easier to reference parts of the route.
|
103
|
-
# This is stolen from the Rail's
|
113
|
+
# This is stolen from the Rail's 3.2 RouteFormatter
|
104
114
|
def routes
|
105
115
|
return @routes if @routes
|
106
116
|
|
107
117
|
@routes = route_set.routes.collect do |route|
|
108
|
-
# reqs = route.requirements.dup
|
109
|
-
# reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
|
110
|
-
# reqs = reqs.empty? ? "" : reqs.inspect
|
111
|
-
# puts "="*45
|
112
|
-
# puts route.path.inspect
|
113
|
-
# puts route.path.spec.inspect
|
114
|
-
# puts "="*45
|
115
|
-
# {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path.to_s, :reqs => reqs}
|
116
|
-
|
117
118
|
route_reqs = route.requirements
|
118
119
|
|
119
|
-
|
120
120
|
controller = route_reqs[:controller] || ':controller'
|
121
121
|
action = route_reqs[:action] || ':action'
|
122
122
|
|
123
|
+
# TODO figure out how they're doing this engine/rackapp routeing stuff.
|
123
124
|
# rack_app = discover_rack_app(route.app)
|
124
125
|
# endpoint = rack_app ? rack_app.inspect : "#{controller}##{action}"
|
125
126
|
endpoint = "#{controller}##{action}"
|
@@ -164,7 +165,6 @@ class GrepRoutes
|
|
164
165
|
# This formats the route as an Array of Strings.
|
165
166
|
# This is stolen from the Rail's routes rake task.
|
166
167
|
def formatted_routes
|
167
|
-
puts routes.first.inspect
|
168
168
|
name_width = routes.map{ |r| r[:name].length }.max
|
169
169
|
verb_width = routes.map{ |r| r[:verb].length }.max
|
170
170
|
path_width = routes.map{ |r| r[:path].length }.max
|
data/spec/basic_routes_spec.rb
CHANGED
data/spec/fixtures/routes.rb
CHANGED
@@ -51,6 +51,24 @@ SomeRailsApp::Application.routes.draw do
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
+
# From https://github.com/francocatena/mawida_app/blob/4f8937b765f0c00dd23e3cafa93182b086f97305/config/routes.rb
|
55
|
+
[
|
56
|
+
'cost_analysis', 'create_cost_analysis',
|
57
|
+
'synthesis_report', 'create_synthesis_report',
|
58
|
+
'high_risk_weaknesses_report', 'create_high_risk_weaknesses_report',
|
59
|
+
'fixed_weaknesses_report', 'create_fixed_weaknesses_report',
|
60
|
+
'weaknesses_by_state', 'create_weaknesses_by_state',
|
61
|
+
'weaknesses_by_risk', 'create_weaknesses_by_risk',
|
62
|
+
'weaknesses_by_audit_type', 'create_weaknesses_by_audit_type'
|
63
|
+
].each do |action|
|
64
|
+
match "conclusion_committee_reports/#{action}",
|
65
|
+
:as => "#{action}_conclusion_committee_reports",
|
66
|
+
:to => "conclusion_committee_reports##{action}"
|
67
|
+
match "follow_up_committee/#{action}",
|
68
|
+
:as => "#{action}_follow_up_committee",
|
69
|
+
:to => "follow_up_committee##{action}"
|
70
|
+
end
|
71
|
+
|
54
72
|
match 'privacy' => 'welcome#privacy'
|
55
73
|
match 'terms' => 'welcome#terms'
|
56
74
|
|
@@ -15,5 +15,16 @@ YourApp::Application.routes.draw do
|
|
15
15
|
mount Preview::StoreMailer => 'mail_view/store_mailer'
|
16
16
|
mount Preview::BrandMailer => 'mail_view/brand_mailer'
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
|
+
# Some Rack apps in routes.rb files from projects github
|
20
|
+
|
21
|
+
mount Tolk::Engine => "/tolk"
|
22
|
+
mount Evergreen.rails, :at => '/evergreen'
|
23
|
+
mount WebsocketApp, :at => '/socket'
|
24
|
+
mount SinatraInternal => '/sinatra'
|
25
|
+
mount Gitstats::Shitometer => '/gitstats'
|
26
|
+
mount Server.new, at: "/server/new"
|
27
|
+
|
28
|
+
# And a made up Rack app
|
29
|
+
mount Nested::RackApp.app, at: '/nested/rackapp'
|
19
30
|
end
|
@@ -3,18 +3,25 @@ require 'spec_helper'
|
|
3
3
|
describe GrepRoutes do
|
4
4
|
|
5
5
|
subject do
|
6
|
-
GrepRoutes.new('spec/fixtures/routes_with_rack_mounts.rb')
|
6
|
+
gr = GrepRoutes.new('spec/fixtures/routes_with_rack_mounts.rb')
|
7
|
+
gr.eval_routes
|
8
|
+
gr
|
7
9
|
end
|
8
10
|
|
9
|
-
it "should
|
10
|
-
subject.
|
11
|
-
subject.eval_routes
|
12
|
-
subject.routes.length.must_equal 6
|
11
|
+
it "should have a route for Tolk" do
|
12
|
+
subject.routes.map{|r| r[:path]}.must_include "/tolk"
|
13
13
|
end
|
14
14
|
|
15
|
-
it "should
|
16
|
-
subject.
|
17
|
-
subject.eval_routes
|
18
|
-
subject.print
|
15
|
+
it "should have a route for Evergreen.rails" do
|
16
|
+
subject.routes.map{|r| r[:path]}.must_include "/evergreen"
|
19
17
|
end
|
18
|
+
|
19
|
+
it "should have a route for WebSocket" do
|
20
|
+
subject.routes.map{|r| r[:path]}.must_include "/socket"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should have a route for Server.new" do
|
24
|
+
subject.routes.map{|r| r[:path]}.must_include "/server/new"
|
25
|
+
end
|
26
|
+
|
20
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grep_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70270111676060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70270111676060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: actionpack
|
27
|
-
requirement: &
|
27
|
+
requirement: &70270111675140 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3.1'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70270111675140
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: minitest
|
38
|
-
requirement: &
|
38
|
+
requirement: &70270111674580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 2.11.2
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70270111674580
|
47
47
|
description: Greppin in ur routes
|
48
48
|
email:
|
49
49
|
- tyler.a.montgomery@gmail.com
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- bin/grep_routes
|
61
61
|
- grep_routes.gemspec
|
62
62
|
- lib/grep_routes.rb
|
63
|
+
- lib/grep_routes_mock_rack_app.rb
|
63
64
|
- spec/basic_routes_spec.rb
|
64
65
|
- spec/fixtures/routes.rb
|
65
66
|
- spec/fixtures/routes_with_rack_mounts.rb
|