sinatra-contrib 1.4.7 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,127 +0,0 @@
1
- require 'sinatra/base'
2
- require 'backports'
3
- require 'uri'
4
-
5
- module Sinatra
6
-
7
- # = Sinatra::Decompile
8
- #
9
- # <tt>Sinatra::Decompile</tt> is an extension that provides a method,
10
- # conveniently called +decompile+, that will generate a String pattern for a
11
- # given route.
12
- #
13
- # == Usage
14
- #
15
- # === Classic Application
16
- #
17
- # To use the extension in a classic application all you need to do is require
18
- # it:
19
- #
20
- # require "sinatra"
21
- # require "sinatra/decompile"
22
- #
23
- # # Your classic application code goes here...
24
- #
25
- # This will add the +decompile+ method to the application/class scope, but
26
- # you can also call it as <tt>Sinatra::Decompile.decompile</tt>.
27
- #
28
- # === Modular Application
29
- #
30
- # To use the extension in a modular application you need to require it, and
31
- # then, tell the application you will use it:
32
- #
33
- # require "sinatra/base"
34
- # require "sinatra/decompile"
35
- #
36
- # class MyApp < Sinatra::Base
37
- # register Sinatra::Decompile
38
- #
39
- # # The rest of your modular application code goes here...
40
- # end
41
- #
42
- # This will add the +decompile+ method to the application/class scope. You
43
- # can choose not to register the extension, but instead of calling
44
- # +decompile+, you will need to call <tt>Sinatra::Decompile.decompile</tt>.
45
- #
46
- module Decompile
47
- extend self
48
-
49
- ##
50
- # Regenerates a string pattern for a given route
51
- #
52
- # Example:
53
- #
54
- # class Sinatra::Application
55
- # routes.each do |verb, list|
56
- # puts "#{verb}:"
57
- # list.each do |data|
58
- # puts "\t" << decompile(data)
59
- # end
60
- # end
61
- # end
62
- #
63
- # Will return the internal Regexp if it's unable to reconstruct the pattern,
64
- # which likely indicates that a Regexp was used in the first place.
65
- #
66
- # You can also use this to check whether you could actually use a string
67
- # pattern instead of your regexp:
68
- #
69
- # decompile /^/foo$/ # => '/foo'
70
- def decompile(pattern, keys = nil, *)
71
- # Everything in here is basically just the reverse of
72
- # Sinatra::Base#compile
73
- #
74
- # Sinatra 2.0 will come with a mechanism for this, making this obsolete.
75
- pattern, keys = pattern if pattern.respond_to? :to_ary
76
- keys, str = keys.try(:dup), pattern.inspect
77
- return pattern unless str.start_with? '/' and str.end_with? '/'
78
- str.gsub! /^\/(\^|\\A)?|(\$|\\z)?\/$/, ''
79
- str.gsub! encoded(' '), ' '
80
- return pattern if str =~ /^[\.\+]/
81
- str.gsub! '((?:[^\.\/?#%]|(?:%[^2].|%[2][^Ee]))+)', '([^\/?#]+)'
82
- str.gsub! '((?:[^\/?#%]|(?:%[^2].|%[2][^Ee]))+)', '([^\/?#]+)'
83
- str.gsub! /\([^\(\)]*\)|\([^\(\)]*\([^\(\)]*\)[^\(\)]*\)/ do |part|
84
- case part
85
- when '(.*?)'
86
- return pattern if keys.shift != 'splat'
87
- '*'
88
- when /^\(\?\:(\\*.)\|%[\w\[\]]+\)$/
89
- $1
90
- when /^\(\?\:(%\d+)\|([^\)]+|\([^\)]+\))\)$/
91
- URI.unescape($1)
92
- when '([^\/?#]+)'
93
- return pattern if keys.empty?
94
- ":" << keys.shift
95
- when /^\(\?\:\\?(.)\|/
96
- char = $1
97
- return pattern unless encoded(char) == part
98
- Regexp.escape(char)
99
- else
100
- return pattern
101
- end
102
- end
103
- str.gsub /(.)([\.\+\(\)\/])/ do
104
- return pattern if $1 != "\\"
105
- $2
106
- end
107
- end
108
-
109
- private
110
-
111
- def encoded(char)
112
- return super if defined? super
113
- enc = uri_parser.escape(char)
114
- enc = "(?:#{escaped(char, enc).join('|')})" if enc == char
115
- enc = "(?:#{enc}|#{encoded('+')})" if char == " "
116
- enc
117
- end
118
-
119
- def uri_parser
120
- #TODO: Remove check after dropping support for 1.8.7
121
- @_uri_parser ||= defined?(URI::Parser) ? URI::Parser.new : URI
122
- end
123
-
124
- end
125
-
126
- register Decompile
127
- end
@@ -1,43 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec::Matchers.define :decompile do |path|
4
- match do |app|
5
- @compiled, @keys = app.send :compile, path
6
- @decompiled = app.decompile(@compiled, @keys)
7
- @decompiled.should == path
8
- end
9
-
10
- failure_message_for_should do |app|
11
- values = [app, @compiled, @keys, path, @decompiled].map(&:inspect)
12
- "expected %s to decompile %s with %s to %s, but was %s" % values
13
- end
14
- end
15
-
16
- describe Sinatra::Decompile do
17
- subject { Sinatra::Application }
18
- it { should decompile("") }
19
- it { should decompile("/") }
20
- it { should decompile("/?") }
21
- it { should decompile("/foo") }
22
- it { should decompile("/:name") }
23
- it { should decompile("/:name?") }
24
- it { should decompile("/:foo/:bar") }
25
- it { should decompile("/page/:id/edit") }
26
- it { should decompile("/hello/*") }
27
- it { should decompile("/*/foo/*") }
28
- it { should decompile("*") }
29
- it { should decompile(":name.:format") }
30
- it { should decompile("a b") }
31
- it { should decompile("a+b") }
32
- it { should decompile(/./) }
33
- it { should decompile(/f(oo)/) }
34
- it { should decompile(/ba+r/) }
35
-
36
- it 'just returns strings' do
37
- subject.decompile('/foo').should == '/foo'
38
- end
39
-
40
- it 'just decompile simple regexps without keys' do
41
- subject.decompile(%r{/foo}).should == '/foo'
42
- end
43
- end