sinatra 0.3.3 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- data/AUTHORS +40 -0
- data/CHANGES +189 -0
- data/README.rdoc +146 -117
- data/Rakefile +33 -10
- data/{test → compat}/app_test.rb +11 -10
- data/{test → compat}/application_test.rb +10 -5
- data/compat/builder_test.rb +101 -0
- data/{test → compat}/custom_error_test.rb +0 -0
- data/compat/erb_test.rb +136 -0
- data/{test → compat}/events_test.rb +16 -3
- data/compat/filter_test.rb +30 -0
- data/compat/haml_test.rb +233 -0
- data/compat/helper.rb +30 -0
- data/compat/mapped_error_test.rb +72 -0
- data/{test → compat}/pipeline_test.rb +9 -4
- data/{test → compat}/public/foo.xml +0 -0
- data/compat/sass_test.rb +57 -0
- data/{test → compat}/sessions_test.rb +0 -0
- data/{test → compat}/streaming_test.rb +4 -1
- data/{test → compat}/sym_params_test.rb +0 -0
- data/{test → compat}/template_test.rb +0 -0
- data/{test → compat}/use_in_file_templates_test.rb +0 -0
- data/{test → compat}/views/foo.builder +0 -0
- data/{test → compat}/views/foo.erb +0 -0
- data/{test → compat}/views/foo.haml +0 -0
- data/{test → compat}/views/foo.sass +0 -0
- data/{test → compat}/views/foo_layout.erb +0 -0
- data/{test → compat}/views/foo_layout.haml +0 -0
- data/{test → compat}/views/layout_test/foo.builder +0 -0
- data/{test → compat}/views/layout_test/foo.erb +0 -0
- data/{test → compat}/views/layout_test/foo.haml +0 -0
- data/{test → compat}/views/layout_test/foo.sass +0 -0
- data/{test → compat}/views/layout_test/layout.builder +0 -0
- data/{test → compat}/views/layout_test/layout.erb +0 -0
- data/{test → compat}/views/layout_test/layout.haml +0 -0
- data/{test → compat}/views/layout_test/layout.sass +0 -0
- data/{test → compat}/views/no_layout/no_layout.builder +0 -0
- data/{test → compat}/views/no_layout/no_layout.haml +0 -0
- data/lib/sinatra.rb +6 -1484
- data/lib/sinatra/base.rb +838 -0
- data/lib/sinatra/compat.rb +239 -0
- data/{images → lib/sinatra/images}/404.png +0 -0
- data/{images → lib/sinatra/images}/500.png +0 -0
- data/lib/sinatra/main.rb +48 -0
- data/lib/sinatra/test.rb +114 -0
- data/lib/sinatra/test/bacon.rb +17 -0
- data/lib/sinatra/test/rspec.rb +7 -8
- data/lib/sinatra/test/spec.rb +3 -4
- data/lib/sinatra/test/unit.rb +3 -5
- data/sinatra.gemspec +68 -35
- data/test/base_test.rb +68 -0
- data/test/builder_test.rb +50 -87
- data/test/data/reload_app_file.rb +3 -0
- data/test/erb_test.rb +38 -124
- data/test/filter_test.rb +27 -22
- data/test/haml_test.rb +51 -216
- data/test/helper.rb +22 -6
- data/test/helpers_test.rb +361 -0
- data/test/mapped_error_test.rb +137 -49
- data/test/middleware_test.rb +58 -0
- data/test/options_test.rb +97 -0
- data/test/reload_test.rb +61 -0
- data/test/request_test.rb +18 -0
- data/test/result_test.rb +88 -0
- data/test/routing_test.rb +391 -0
- data/test/sass_test.rb +27 -48
- data/test/sinatra_test.rb +13 -0
- data/test/static_test.rb +57 -0
- data/test/templates_test.rb +88 -0
- data/test/views/hello.builder +1 -0
- data/test/views/hello.erb +1 -0
- data/test/views/hello.haml +1 -0
- data/test/views/hello.sass +2 -0
- data/test/views/hello.test +1 -0
- data/test/views/layout2.builder +3 -0
- data/test/views/layout2.erb +2 -0
- data/test/views/layout2.haml +2 -0
- data/test/views/layout2.test +1 -0
- metadata +80 -48
- data/ChangeLog +0 -96
- data/lib/sinatra/test/methods.rb +0 -76
- data/test/event_context_test.rb +0 -15
@@ -0,0 +1,239 @@
|
|
1
|
+
# Sinatra 0.3.x compatibility module.
|
2
|
+
#
|
3
|
+
# The following code makes Sinatra 0.9.x compatible with Sinatra 0.3.x to
|
4
|
+
# ease the transition to the final 1.0 release. Everything defined in this
|
5
|
+
# file will be removed for the 1.0 release.
|
6
|
+
|
7
|
+
require 'ostruct'
|
8
|
+
require 'sinatra/base'
|
9
|
+
require 'sinatra/main'
|
10
|
+
|
11
|
+
# Like Kernel#warn but outputs the location that triggered the warning.
|
12
|
+
def sinatra_warn(*message)
|
13
|
+
line = caller.
|
14
|
+
detect { |line| line !~ /(?:lib\/sinatra\/|__DELEGATE__)/ }.
|
15
|
+
sub(/:in .*/, '')
|
16
|
+
warn "#{line}: warning: #{message.join(' ')}"
|
17
|
+
end
|
18
|
+
|
19
|
+
# Rack now supports evented and swiftiplied mongrels through separate
|
20
|
+
# handler.
|
21
|
+
if ENV['SWIFT']
|
22
|
+
sinatra_warn 'the SWIFT environment variable is deprecated;',
|
23
|
+
'use Rack::Handler::SwiftipliedMongrel instead.'
|
24
|
+
require 'swiftcore/swiftiplied_mongrel'
|
25
|
+
puts "Using Swiftiplied Mongrel"
|
26
|
+
elsif ENV['EVENT']
|
27
|
+
sinatra_warn 'the EVENT environment variable is deprecated;',
|
28
|
+
'use Rack::Handler::EventedMongrel instead.'
|
29
|
+
require 'swiftcore/evented_mongrel'
|
30
|
+
puts "Using Evented Mongrel"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Make Rack 0.9.0 backward compatibile with 0.4.0 mime types. This isn't
|
34
|
+
# technically a Sinatra issue but many Sinatra apps access the old
|
35
|
+
# MIME_TYPES constants due to Sinatra example code.
|
36
|
+
require 'rack/file'
|
37
|
+
class Rack::File
|
38
|
+
def self.const_missing(const_name)
|
39
|
+
if const_name == :MIME_TYPES
|
40
|
+
hash = Hash.new { |hash,key| Rack::Mime::MIME_TYPES[".#{key}"] }
|
41
|
+
const_set :MIME_TYPES, hash
|
42
|
+
sinatra_warn 'Rack::File::MIME_TYPES is deprecated; use Rack::Mime instead.'
|
43
|
+
hash
|
44
|
+
else
|
45
|
+
super
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
module Sinatra
|
51
|
+
module Compat
|
52
|
+
end
|
53
|
+
|
54
|
+
# The ServerError exception is deprecated. Any exception is considered an
|
55
|
+
# internal server error.
|
56
|
+
class ServerError < RuntimeError
|
57
|
+
def initialize(*args, &block)
|
58
|
+
sinatra_warn 'Sinatra::ServerError is deprecated;',
|
59
|
+
'use another exception, error, or Kernel#fail instead.'
|
60
|
+
end
|
61
|
+
def code ; 500 ; end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Default < Base
|
65
|
+
def self.const_missing(const_name)
|
66
|
+
if const_name == :FORWARD_METHODS
|
67
|
+
sinatra_warn 'Sinatra::Application::FORWARD_METHODS is deprecated;',
|
68
|
+
'use Sinatra::Delegator::METHODS instead.'
|
69
|
+
const_set :FORWARD_METHODS, Sinatra::Delegator::METHODS
|
70
|
+
Sinatra::Delegator::METHODS
|
71
|
+
else
|
72
|
+
super
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Deprecated. Use: response['Header-Name']
|
77
|
+
def headers(header=nil)
|
78
|
+
sinatra_warn "The 'headers' method is deprecated; use 'response' instead."
|
79
|
+
response.headers.merge!(header) if header
|
80
|
+
response.headers
|
81
|
+
end
|
82
|
+
alias :header :headers
|
83
|
+
|
84
|
+
# Deprecated. Use: halt
|
85
|
+
def stop(*args, &block)
|
86
|
+
sinatra_warn "The 'stop' method is deprecated; use 'halt' instead."
|
87
|
+
halt(*args, &block)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Deprecated. Use: etag
|
91
|
+
def entity_tag(*args, &block)
|
92
|
+
sinatra_warn "The 'entity_tag' method is deprecated; use 'etag' instead."
|
93
|
+
etag(*args, &block)
|
94
|
+
end
|
95
|
+
|
96
|
+
# The :disposition option is deprecated; use: #attachment. This method
|
97
|
+
# setting the Content-Transfer-Encoding header is deprecated.
|
98
|
+
#--
|
99
|
+
# TODO deprecation warning for :disposition argument.
|
100
|
+
def send_file(path, opts={})
|
101
|
+
opts[:disposition] = 'attachment' if !opts.key?(:disposition)
|
102
|
+
attachment opts[:filename] || path if opts[:filename] || opts[:disposition]
|
103
|
+
response['Content-Transfer-Encoding'] = 'binary' if opts[:disposition]
|
104
|
+
super(path, opts)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Throwing halt with a Symbol and the to_result convention are
|
108
|
+
# deprecated. Override the invoke method to detect those types of return
|
109
|
+
# values.
|
110
|
+
def invoke(handler)
|
111
|
+
res = super
|
112
|
+
case
|
113
|
+
when res.kind_of?(Symbol)
|
114
|
+
sinatra_warn "Invoking the :#{res} helper by returning a Symbol is deprecated;",
|
115
|
+
"call the helper directly instead."
|
116
|
+
@response.body = __send__(res)
|
117
|
+
when res.respond_to?(:to_result)
|
118
|
+
sinatra_warn "The to_result convention is deprecated."
|
119
|
+
@response.body = res.to_result(self)
|
120
|
+
end
|
121
|
+
res
|
122
|
+
end
|
123
|
+
|
124
|
+
def options
|
125
|
+
Options.new(self.class)
|
126
|
+
end
|
127
|
+
|
128
|
+
class Options < Struct.new(:target) #:nodoc:
|
129
|
+
def method_missing(name, *args, &block)
|
130
|
+
if target.respond_to?(name)
|
131
|
+
target.__send__(name, *args, &block)
|
132
|
+
elsif args.empty? && name.to_s !~ /=$/
|
133
|
+
sinatra_warn 'accessing undefined options will raise a NameError in Sinatra 1.0'
|
134
|
+
nil
|
135
|
+
else
|
136
|
+
super
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class << self
|
142
|
+
# Deprecated. Options are stored directly on the class object.
|
143
|
+
def options
|
144
|
+
sinatra_warn "The 'options' class method is deprecated; use 'self' instead."
|
145
|
+
Options.new(self)
|
146
|
+
end
|
147
|
+
|
148
|
+
# Deprecated. Use: configure
|
149
|
+
def configures(*args, &block)
|
150
|
+
sinatra_warn "The 'configures' method is deprecated; use 'configure' instead."
|
151
|
+
configure(*args, &block)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Deprecated. Use: set
|
155
|
+
def default_options
|
156
|
+
sinatra_warn "Sinatra::Application.default_options is deprecated; use 'set' instead."
|
157
|
+
fake = lambda { |options| set(options) }
|
158
|
+
def fake.merge!(options) ; call(options) ; end
|
159
|
+
fake
|
160
|
+
end
|
161
|
+
|
162
|
+
# Deprecated. Use: set
|
163
|
+
def set_option(*args, &block)
|
164
|
+
sinatra_warn "The 'set_option' method is deprecated; use 'set' instead."
|
165
|
+
set(*args, &block)
|
166
|
+
end
|
167
|
+
|
168
|
+
def set_options(*args, &block)
|
169
|
+
sinatra_warn "The 'set_options' method is deprecated; use 'set' instead."
|
170
|
+
set(*args, &block)
|
171
|
+
end
|
172
|
+
|
173
|
+
# Deprecated. Use: set :environment, ENV
|
174
|
+
def env=(value)
|
175
|
+
sinatra_warn "The :env option is deprecated; use :environment instead."
|
176
|
+
set :environment, value
|
177
|
+
end
|
178
|
+
|
179
|
+
# Deprecated. Use: options.environment
|
180
|
+
def env
|
181
|
+
sinatra_warn "The :env option is deprecated; use :environment instead."
|
182
|
+
environment
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
# Deprecated. Missing messages are no longer delegated to @response.
|
187
|
+
def method_missing(name, *args, &b)
|
188
|
+
if @response.respond_to?(name)
|
189
|
+
sinatra_warn "The '#{name}' method is deprecated; use 'response.#{name}' instead."
|
190
|
+
@response.send(name, *args, &b)
|
191
|
+
else
|
192
|
+
super
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
class << self
|
198
|
+
# Deprecated. Use: Sinatra::Application
|
199
|
+
def application
|
200
|
+
sinatra_warn "Sinatra.application is deprecated; use Sinatra::Application instead."
|
201
|
+
Sinatra::Application
|
202
|
+
end
|
203
|
+
|
204
|
+
# Deprecated. Use: Sinatra::Application.reset!
|
205
|
+
def application=(value)
|
206
|
+
raise ArgumentError unless value.nil?
|
207
|
+
sinatra_warn "Setting Sinatra.application to nil is deprecated; create a new instance instead."
|
208
|
+
Sinatra.class_eval do
|
209
|
+
remove_const :Application
|
210
|
+
const_set :Application, Class.new(Sinatra::Default)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def build_application
|
215
|
+
sinatra_warn "Sinatra.build_application is deprecated; use Sinatra::Application instead."
|
216
|
+
Sinatra::Application
|
217
|
+
end
|
218
|
+
|
219
|
+
def options
|
220
|
+
sinatra_warn "Sinatra.options is deprecated; use Sinatra::Application.option_name instead."
|
221
|
+
Sinatra::Application.options
|
222
|
+
end
|
223
|
+
|
224
|
+
def port
|
225
|
+
sinatra_warn "Sinatra.port is deprecated; use Sinatra::Application.port instead."
|
226
|
+
options.port
|
227
|
+
end
|
228
|
+
|
229
|
+
def host
|
230
|
+
sinatra_warn "Sinatra.host is deprecated; use Sinatra::Application.host instead."
|
231
|
+
options.host
|
232
|
+
end
|
233
|
+
|
234
|
+
def env
|
235
|
+
sinatra_warn "Sinatra.env is deprecated; use Sinatra::Application.environment instead."
|
236
|
+
options.environment
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
File without changes
|
File without changes
|
data/lib/sinatra/main.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
class Default < Base
|
5
|
+
set :app_file, lambda {
|
6
|
+
ignore = [
|
7
|
+
/lib\/sinatra.*\.rb$/, # all sinatra code
|
8
|
+
/\(.*\)/, # generated code
|
9
|
+
/custom_require\.rb$/ # rubygems require hacks
|
10
|
+
]
|
11
|
+
path =
|
12
|
+
caller.map{ |line| line.split(':', 2).first }.find do |file|
|
13
|
+
next if ignore.any? { |pattern| file =~ pattern }
|
14
|
+
file
|
15
|
+
end
|
16
|
+
path || $0
|
17
|
+
}.call
|
18
|
+
|
19
|
+
set :run, Proc.new { $0 == app_file }
|
20
|
+
set :reload, Proc.new{ app_file? && development? }
|
21
|
+
|
22
|
+
if run? && ARGV.any?
|
23
|
+
require 'optparse'
|
24
|
+
OptionParser.new { |op|
|
25
|
+
op.on('-x') { set :mutex, true }
|
26
|
+
op.on('-e env') { |val| set :environment, val.to_sym }
|
27
|
+
op.on('-s server') { |val| set :server, val }
|
28
|
+
op.on('-p port') { |val| set :port, val.to_i }
|
29
|
+
}.parse!(ARGV.dup)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
include Sinatra::Delegator
|
35
|
+
|
36
|
+
def helpers(&block)
|
37
|
+
Sinatra::Application.send :class_eval, &block
|
38
|
+
end
|
39
|
+
|
40
|
+
def mime(ext, type)
|
41
|
+
ext = ".#{ext}" unless ext.to_s[0] == ?.
|
42
|
+
Rack::Mime::MIME_TYPES[ext.to_s] = type
|
43
|
+
end
|
44
|
+
|
45
|
+
at_exit do
|
46
|
+
raise $! if $!
|
47
|
+
Sinatra::Application.run! if Sinatra::Application.run?
|
48
|
+
end
|
data/lib/sinatra/test.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
|
5
|
+
module Test
|
6
|
+
include Rack::Utils
|
7
|
+
|
8
|
+
attr_reader :app, :request, :response
|
9
|
+
|
10
|
+
def test_request(verb, path, *args)
|
11
|
+
@app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application)
|
12
|
+
fail "@app not set - cannot make request" if @app.nil?
|
13
|
+
@request = Rack::MockRequest.new(@app)
|
14
|
+
opts, input =
|
15
|
+
case args.size
|
16
|
+
when 2 # input, env
|
17
|
+
input, env = args
|
18
|
+
if input.kind_of?(Hash) # params, env
|
19
|
+
[env, param_string(input)]
|
20
|
+
else
|
21
|
+
[env, input]
|
22
|
+
end
|
23
|
+
when 1 # params
|
24
|
+
if (data = args.first).kind_of?(Hash)
|
25
|
+
env = (data.delete(:env) || {})
|
26
|
+
[env, param_string(data)]
|
27
|
+
else
|
28
|
+
[{}, data]
|
29
|
+
end
|
30
|
+
when 0
|
31
|
+
[{}, '']
|
32
|
+
else
|
33
|
+
raise ArgumentError, "zero, one, or two arguments expected"
|
34
|
+
end
|
35
|
+
opts = rack_opts(opts)
|
36
|
+
opts[:input] ||= input
|
37
|
+
yield @request if block_given?
|
38
|
+
@response = @request.request(verb, path, opts)
|
39
|
+
end
|
40
|
+
|
41
|
+
def get(path, *args, &b) ; test_request('GET', path, *args, &b) ; end
|
42
|
+
def head(path, *args, &b) ; test_request('HEAD', path, *args, &b) ; end
|
43
|
+
def post(path, *args, &b) ; test_request('POST', path, *args, &b) ; end
|
44
|
+
def put(path, *args, &b) ; test_request('PUT', path, *args, &b) ; end
|
45
|
+
def delete(path, *args, &b) ; test_request('DELETE', path, *args, &b) ; end
|
46
|
+
|
47
|
+
def follow!
|
48
|
+
test_request 'GET', @response.location
|
49
|
+
end
|
50
|
+
|
51
|
+
def body ; @response.body ; end
|
52
|
+
def status ; @response.status ; end
|
53
|
+
|
54
|
+
# Delegate other missing methods to @response.
|
55
|
+
def method_missing(name, *args, &block)
|
56
|
+
if @response && @response.respond_to?(name)
|
57
|
+
@response.send(name, *args, &block)
|
58
|
+
else
|
59
|
+
super
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Also check @response since we delegate there.
|
64
|
+
def respond_to?(symbol, include_private=false)
|
65
|
+
super || (@response && @response.respond_to?(symbol, include_private))
|
66
|
+
end
|
67
|
+
|
68
|
+
RACK_OPT_NAMES = {
|
69
|
+
:accept => "HTTP_ACCEPT",
|
70
|
+
:agent => "HTTP_USER_AGENT",
|
71
|
+
:host => "HTTP_HOST",
|
72
|
+
:session => "HTTP_COOKIE",
|
73
|
+
:cookies => "HTTP_COOKIE",
|
74
|
+
:content_type => "CONTENT_TYPE"
|
75
|
+
}
|
76
|
+
|
77
|
+
def rack_opts(opts)
|
78
|
+
opts.inject({}) do |hash,(key,val)|
|
79
|
+
key = RACK_OPT_NAMES[key] || key
|
80
|
+
hash[key] = val
|
81
|
+
hash
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def env_for(opts={})
|
86
|
+
opts = rack_opts(opts)
|
87
|
+
Rack::MockRequest.env_for(opts)
|
88
|
+
end
|
89
|
+
|
90
|
+
def param_string(hash)
|
91
|
+
hash.map { |pair| pair.map{|v|escape(v)}.join('=') }.join('&')
|
92
|
+
end
|
93
|
+
|
94
|
+
if defined? Sinatra::Compat
|
95
|
+
# Deprecated. Use: "get" instead of "get_it".
|
96
|
+
%w(get head post put delete).each do |verb|
|
97
|
+
eval <<-RUBY, binding, __FILE__, __LINE__
|
98
|
+
def #{verb}_it(*args, &block)
|
99
|
+
sinatra_warn "The #{verb}_it method is deprecated; use #{verb} instead."
|
100
|
+
test_request('#{verb.upcase}', *args, &block)
|
101
|
+
end
|
102
|
+
RUBY
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class TestHarness
|
108
|
+
include Test
|
109
|
+
|
110
|
+
def initialize(app=nil)
|
111
|
+
@app = app || Sinatra::Application
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'bacon'
|
2
|
+
require 'sinatra/test'
|
3
|
+
|
4
|
+
Sinatra::Default.set(
|
5
|
+
:env => :test,
|
6
|
+
:run => false,
|
7
|
+
:raise_errors => true,
|
8
|
+
:logging => false
|
9
|
+
)
|
10
|
+
|
11
|
+
module Sinatra::Test
|
12
|
+
def should
|
13
|
+
@response.should
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Bacon::Context.send(:include, Sinatra::Test)
|
data/lib/sinatra/test/rspec.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require 'sinatra/test'
|
2
2
|
require 'spec/interop/test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
4
|
+
Sinatra::Default.set(
|
5
|
+
:env => :test,
|
6
|
+
:run => false,
|
7
|
+
:raise_errors => true,
|
8
|
+
:logging => false
|
9
|
+
)
|
data/lib/sinatra/test/spec.rb
CHANGED