sinatra-rake-routes 0.0.3 → 0.0.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 +1 -1
- data/lib/sinatra-rake-routes.rb +50 -19
- data/lib/sinatra-rake-routes/version.rb +1 -1
- data/spec/fixtures/sample_app_output.txt +2 -0
- data/spec/support/sample_app.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95a7c71875de28832eba243ffb2ed23f6ea5d1ea
|
4
|
+
data.tar.gz: dc1f9c24996341ef8eb3ac17a171ef7f9a019d71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94bf7e057f889ae53698ffe57556cf70e4e1c26411b290ad9d89a13686fdb0fe0d2cc8c39136b67335397cb78a5ec6adcd663562fccf8e161d43e47651ba538b
|
7
|
+
data.tar.gz: c1a5ab469ce3465f89782a1ae19302b6957b0e171664c6529584508d68f3bc7a991a812ecc3f6f911f51433b98f1b8f0b395496f9c718b42cb8ccbafc6ecaccf
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# sinatra-rake-routes [](https://circleci.com/gh/wealthsimple/sinatra-rake-routes)
|
1
|
+
# sinatra-rake-routes [](https://circleci.com/gh/wealthsimple/sinatra-rake-routes) 
|
2
2
|
|
3
3
|
`rake routes` command for Sinatra applications for printing out all defined routes.
|
4
4
|
|
data/lib/sinatra-rake-routes.rb
CHANGED
@@ -4,14 +4,6 @@ require 'sinatra-rake-routes/version'
|
|
4
4
|
class SinatraRakeRoutes
|
5
5
|
@@app_class = nil
|
6
6
|
|
7
|
-
# Sinatra compiles routes into regexes. See below link for how this works:
|
8
|
-
# https://github.com/sinatra/sinatra/blob/4c7d38eb1b2cc02ce51029f28e0c3c34ca12ccfd/lib/sinatra/base.rb#L1618
|
9
|
-
# The below regexes cover some of the common cases.
|
10
|
-
HYPHEN_REGEX = /(?:\-|%2[Dd])/
|
11
|
-
NAMED_PARAM_REGEX_SOURCE = "([^\/?#]+)"
|
12
|
-
PERIOD_REGEX = /(?:\.|%2[Ee])/
|
13
|
-
SPLAT_REGEX = /(.*?)/
|
14
|
-
|
15
7
|
def self.set_app_class(klass)
|
16
8
|
if klass.respond_to?(:routes) && klass.routes.is_a?(Hash)
|
17
9
|
@@app_class = klass
|
@@ -28,7 +20,7 @@ class SinatraRakeRoutes
|
|
28
20
|
str = []
|
29
21
|
self.class.app_class.routes.each do |http_method, routes|
|
30
22
|
str << http_method
|
31
|
-
routes = routes.map { |route|
|
23
|
+
routes = routes.map { |route| decompile(*route) }
|
32
24
|
str << routes.sort.join("\n") + "\n"
|
33
25
|
end
|
34
26
|
str.join("\n")
|
@@ -36,16 +28,55 @@ class SinatraRakeRoutes
|
|
36
28
|
|
37
29
|
private
|
38
30
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
31
|
+
# Below method taken from Sinatra::Contrib
|
32
|
+
# https://github.com/sinatra/sinatra-contrib/blob/66892d3789ba23f157b17d67570479e97d7eaa95/lib/sinatra/decompile.rb#L70
|
33
|
+
def decompile(pattern, keys = nil, *)
|
34
|
+
# Everything in here is basically just the reverse of
|
35
|
+
# Sinatra::Base#compile
|
36
|
+
#
|
37
|
+
# Sinatra 2.0 will come with a mechanism for this, making this obsolete.
|
38
|
+
pattern, keys = pattern if pattern.respond_to? :to_ary
|
39
|
+
str = pattern.inspect
|
40
|
+
return pattern unless str.start_with? '/' and str.end_with? '/'
|
41
|
+
str.gsub! /(?:\\-|%2[Dd])/, "-"
|
42
|
+
str.gsub! /^\/(\^|\\A)?|(\$|\\z)?\/$/, ''
|
43
|
+
str.gsub! encoded(' '), ' '
|
44
|
+
return pattern if str =~ /^[\.\+]/
|
45
|
+
str.gsub! '((?:[^\.\/?#%]|(?:%[^2].|%[2][^Ee]))+)', '([^\/?#]+)'
|
46
|
+
str.gsub! '((?:[^\/?#%]|(?:%[^2].|%[2][^Ee]))+)', '([^\/?#]+)'
|
47
|
+
str.gsub! /\([^\(\)]*\)|\([^\(\)]*\([^\(\)]*\)[^\(\)]*\)/ do |part|
|
48
|
+
case part
|
49
|
+
when '(.*?)'
|
50
|
+
return pattern if keys.shift != 'splat'
|
51
|
+
'*'
|
52
|
+
when /^\(\?\:(\\*.)\|%[\w\[\]]+\)$/
|
53
|
+
$1
|
54
|
+
when /^\(\?\:(%\d+)\|([^\)]+|\([^\)]+\))\)$/
|
55
|
+
URI.unescape($1)
|
56
|
+
when '([^\/?#]+)'
|
57
|
+
return pattern if keys.empty?
|
58
|
+
":" << keys.shift
|
59
|
+
when /^\(\?\:\\?(.)\|/
|
60
|
+
char = $1
|
61
|
+
return pattern unless encoded(char) == part
|
62
|
+
Regexp.escape(char)
|
63
|
+
else
|
64
|
+
return pattern
|
65
|
+
end
|
66
|
+
end
|
67
|
+
str.gsub /(.)([\.\+\(\)\/])/ do
|
68
|
+
return pattern if $1 != "\\"
|
69
|
+
$2
|
47
70
|
end
|
48
|
-
# Remove leading "\A" and trailing "\z"
|
49
|
-
source[2...-2]
|
50
71
|
end
|
72
|
+
|
73
|
+
def encoded(char)
|
74
|
+
@uri_parser ||= URI::Parser.new
|
75
|
+
enc = @uri_parser.escape(char)
|
76
|
+
escaped = [Regexp.escape(enc), @uri_parser.escape(char, /./)]
|
77
|
+
enc = "(?:#{escaped.join('|')})" if enc == char
|
78
|
+
enc = "(?:#{enc}|#{encoded('+')})" if char == " "
|
79
|
+
enc
|
80
|
+
end
|
81
|
+
|
51
82
|
end
|
@@ -7,12 +7,14 @@ POST
|
|
7
7
|
GET
|
8
8
|
/users.tar.gz
|
9
9
|
/users/:user_id
|
10
|
+
/users/:user_id.json
|
10
11
|
/users/:user_id/messages
|
11
12
|
/users/:user_id/messages/:message_id
|
12
13
|
|
13
14
|
HEAD
|
14
15
|
/users.tar.gz
|
15
16
|
/users/:user_id
|
17
|
+
/users/:user_id.json
|
16
18
|
/users/:user_id/messages
|
17
19
|
/users/:user_id/messages/:message_id
|
18
20
|
|
data/spec/support/sample_app.rb
CHANGED