sinatra-rake-routes 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 407b7955269b5c168134db5d9281869200d38d11
4
- data.tar.gz: d5e39656762c0dede87e9589aa7cfe0fb76164e1
3
+ metadata.gz: 95a7c71875de28832eba243ffb2ed23f6ea5d1ea
4
+ data.tar.gz: dc1f9c24996341ef8eb3ac17a171ef7f9a019d71
5
5
  SHA512:
6
- metadata.gz: a1a174a8a372d2bbae676ea191e2c5e3d74d8cd8a063c5c5552958eb893ebab0c5c6bbb85a32162da3c32659d4e4973915972370a5ecc197e447ff054738a512
7
- data.tar.gz: 3294f3f0b7be56e948258183e5ef34c7432d1621faca77493173adf02738e022677bd6d34a770ea694661f1c697d1c2d8dcd3882d4b8dcefc81b5e2c73591fe5
6
+ metadata.gz: 94bf7e057f889ae53698ffe57556cf70e4e1c26411b290ad9d89a13686fdb0fe0d2cc8c39136b67335397cb78a5ec6adcd663562fccf8e161d43e47651ba538b
7
+ data.tar.gz: c1a5ab469ce3465f89782a1ae19302b6957b0e171664c6529584508d68f3bc7a991a812ecc3f6f911f51433b98f1b8f0b395496f9c718b42cb8ccbafc6ecaccf
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # sinatra-rake-routes [![Circle CI](https://circleci.com/gh/wealthsimple/sinatra-rake-routes.svg?style=svg)](https://circleci.com/gh/wealthsimple/sinatra-rake-routes)
1
+ # sinatra-rake-routes [![Circle CI](https://circleci.com/gh/wealthsimple/sinatra-rake-routes.svg?style=svg)](https://circleci.com/gh/wealthsimple/sinatra-rake-routes) ![](https://img.shields.io/gem/v/sinatra-rake-routes.svg)
2
2
 
3
3
  `rake routes` command for Sinatra applications for printing out all defined routes.
4
4
 
@@ -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| decompile_route(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
- def decompile_route(route)
40
- source = route[0].source
41
- params = route[1]
42
- source.gsub!(HYPHEN_REGEX.source, "-")
43
- source.gsub!(PERIOD_REGEX.source, ".")
44
- source.gsub!(SPLAT_REGEX.source, "*")
45
- params.each do |param|
46
- source.sub!(NAMED_PARAM_REGEX_SOURCE, ":#{param}")
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
@@ -1,3 +1,3 @@
1
1
  class SinatraRakeRoutes
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  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
 
@@ -11,6 +11,8 @@ class SampleApp < Sinatra::Base
11
11
  end
12
12
  get '/users.tar.gz' do
13
13
  end
14
+ get '/users/:user_id.json' do
15
+ end
14
16
  get '/users/:user_id' do
15
17
  end
16
18
  get '/users/:user_id/messages' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-rake-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Graham