bmizerany-sinatra 0.8.10 → 0.9.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/lib/sinatra/base.rb CHANGED
@@ -4,7 +4,7 @@ require 'rack'
4
4
  require 'rack/builder'
5
5
 
6
6
  module Sinatra
7
- VERSION = '0.8.10'
7
+ VERSION = '0.9.0'
8
8
 
9
9
  class Request < Rack::Request
10
10
  def user_agent
@@ -14,13 +14,6 @@ module Sinatra
14
14
  def accept
15
15
  @env['HTTP_ACCEPT'].split(',').map { |a| a.strip }
16
16
  end
17
-
18
- # Override Rack 0.9.x's #params implementation (see #72 in lighthouse)
19
- def params
20
- self.GET.update(self.POST)
21
- rescue EOFError => boom
22
- self.GET
23
- end
24
17
  end
25
18
 
26
19
  class Response < Rack::Response
@@ -77,7 +70,7 @@ module Sinatra
77
70
  def redirect(uri, *args)
78
71
  status 302
79
72
  response['Location'] = uri
80
- halt(*args)
73
+ halt *args
81
74
  end
82
75
 
83
76
  # Halt processing and return the error status provided.
@@ -142,7 +135,7 @@ module Sinatra
142
135
  class StaticFile < ::File #:nodoc:
143
136
  alias_method :to_path, :path
144
137
  def each
145
- while buf = read(8192)
138
+ while buf = read(8196)
146
139
  yield buf
147
140
  end
148
141
  end
@@ -217,7 +210,6 @@ module Sinatra
217
210
 
218
211
  def lookup_layout(engine, options)
219
212
  return if options[:layout] == false
220
- options.delete(:layout) if options[:layout] == true
221
213
  template = options[:layout] || :layout
222
214
  data = lookup_template(engine, template, options)
223
215
  [template, data]
@@ -306,10 +298,10 @@ module Sinatra
306
298
  attr_accessor :env, :request, :response, :params
307
299
 
308
300
  def call!(env)
309
- @env = env
310
- @request = Request.new(env)
301
+ @env = env
302
+ @request = Request.new(env)
311
303
  @response = Response.new
312
- @params = nil
304
+ @params = nil
313
305
  error_detection { dispatch! }
314
306
  @response.finish
315
307
  end
@@ -331,9 +323,10 @@ module Sinatra
331
323
  self.class.filters.each {|block| instance_eval(&block)}
332
324
  if routes = self.class.routes[@request.request_method]
333
325
  path = @request.path_info
334
- original_params = nested_params(@request.params)
326
+ original_params = Hash.new{ |hash,k| hash[k.to_s] if Symbol === k }
327
+ original_params.merge! @request.params
335
328
 
336
- routes.each do |pattern, keys, conditions, method_name|
329
+ routes.each do |pattern, keys, conditions, block|
337
330
  if pattern =~ path
338
331
  values = $~.captures.map{|val| val && unescape(val) }
339
332
  params =
@@ -351,12 +344,13 @@ module Sinatra
351
344
  else
352
345
  {}
353
346
  end
354
- @params = original_params.merge(params)
347
+ @params = original_params.dup
348
+ @params.merge!(params)
355
349
 
356
350
  catch(:pass) {
357
351
  conditions.each { |cond|
358
352
  throw :pass if instance_eval(&cond) == false }
359
- return invoke(method_name)
353
+ return invoke(block)
360
354
  }
361
355
  end
362
356
  end
@@ -364,30 +358,8 @@ module Sinatra
364
358
  raise NotFound
365
359
  end
366
360
 
367
- def nested_params(params)
368
- return indifferent_hash.merge(params) if !params.keys.join.include?('[')
369
- params.inject indifferent_hash do |res, (key,val)|
370
- if key =~ /\[.*\]/
371
- splat = key.scan(/(^[^\[]+)|\[([^\]]+)\]/).flatten.compact
372
- head, last = splat[0..-2], splat[-1]
373
- head.inject(res){ |s,v| s[v] ||= indifferent_hash }[last] = val
374
- end
375
- res
376
- end
377
- end
378
-
379
- def indifferent_hash
380
- Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
381
- end
382
-
383
- def invoke(handler)
384
- res = catch(:halt) {
385
- if handler.respond_to?(:call)
386
- instance_eval(&handler)
387
- else
388
- send(handler)
389
- end
390
- }
361
+ def invoke(block)
362
+ res = catch(:halt) { instance_eval(&block) }
391
363
  case
392
364
  when res.respond_to?(:to_str)
393
365
  @response.body = [res]
@@ -407,12 +379,16 @@ module Sinatra
407
379
  else
408
380
  @response.body = res
409
381
  end
382
+ when res.kind_of?(Symbol) # TODO: deprecate this.
383
+ @response.body = __send__(res)
410
384
  when res.respond_to?(:each)
411
385
  @response.body = res
412
386
  when (100...599) === res
413
387
  @response.status = res
414
388
  when res.nil?
415
389
  @response.body = []
390
+ else
391
+ raise TypeError, "#{res.inspect} not supported"
416
392
  end
417
393
  res
418
394
  end
@@ -428,12 +404,6 @@ module Sinatra
428
404
  invoke handler unless handler.nil?
429
405
  rescue ::Exception => boom
430
406
  @env['sinatra.error'] = boom
431
-
432
- if options.dump_errors?
433
- msg = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n ")
434
- @env['rack.errors'] << msg
435
- end
436
-
437
407
  raise boom if options.raise_errors?
438
408
  @response.status = 500
439
409
  invoke errmap[boom.class] || errmap[Exception]
@@ -486,10 +456,6 @@ module Sinatra
486
456
  end
487
457
  end
488
458
 
489
- def not_found(&block)
490
- error 404, &block
491
- end
492
-
493
459
  def template(name, &block)
494
460
  templates[name] = block
495
461
  end
@@ -499,16 +465,11 @@ module Sinatra
499
465
  end
500
466
 
501
467
  def use_in_file_templates!
502
- line = caller.detect do |s|
503
- [
504
- /lib\/sinatra.*\.rb/,
505
- /\(.*\)/,
506
- /rubygems\/custom_require\.rb/
507
- ].all? { |x| s !~ x }
508
- end
468
+ line = caller.detect { |s| s !~ /lib\/sinatra.*\.rb/ &&
469
+ s !~ /\(.*\)/ }
509
470
  file = line.sub(/:\d+.*$/, '')
510
471
  if data = ::IO.read(file).split('__END__')[1]
511
- data.gsub!(/\r\n/, "\n")
472
+ data.gsub! /\r\n/, "\n"
512
473
  template = nil
513
474
  data.each_line do |line|
514
475
  if line =~ /^@@\s*(.*)/
@@ -567,10 +528,10 @@ module Sinatra
567
528
 
568
529
  def get(path, opts={}, &block)
569
530
  conditions = @conditions.dup
570
- _, _, _, method_name = route('GET', path, opts, &block)
531
+ route 'GET', path, opts, &block
571
532
 
572
533
  @conditions = conditions
573
- head(path, opts) { invoke(method_name) ; [] }
534
+ head(path, opts) { invoke(block) ; [] }
574
535
  end
575
536
 
576
537
  def put(path, opts={}, &bk); route 'PUT', path, opts, &bk; end
@@ -586,14 +547,8 @@ module Sinatra
586
547
 
587
548
  pattern, keys = compile(path)
588
549
  conditions, @conditions = @conditions, []
589
- method_name = "route { #{method} #{path} }"
590
- nmethods = instance_methods.grep(rx = /#{Regexp.escape(method_name)}/).size
591
- method_name += " [#{nmethods}]"
592
-
593
- define_method(method_name, &block)
594
-
595
550
  (routes[method] ||= []).
596
- push([pattern, keys, conditions, method_name]).last
551
+ push [pattern, keys, conditions, block]
597
552
  end
598
553
 
599
554
  def compile(path)
@@ -692,7 +647,6 @@ module Sinatra
692
647
  end
693
648
 
694
649
  set :raise_errors, true
695
- set :dump_errors, false
696
650
  set :sessions, false
697
651
  set :logging, false
698
652
  set :methodoverride, false
@@ -786,7 +740,6 @@ module Sinatra
786
740
 
787
741
  class Default < Base
788
742
  set :raise_errors, false
789
- set :dump_errors, true
790
743
  set :sessions, false
791
744
  set :logging, true
792
745
  set :methodoverride, true
@@ -803,7 +756,6 @@ module Sinatra
803
756
  end
804
757
 
805
758
  def self.call(env)
806
- $LOADED_FEATURES.delete("sinatra.rb")
807
759
  reload! if reload?
808
760
  super
809
761
  end
@@ -1,102 +1,58 @@
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
1
  require 'ostruct'
8
2
  require 'sinatra/base'
9
3
  require 'sinatra/main'
10
4
 
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.
5
+ # Deprecated. Do we still need this?
21
6
  if ENV['SWIFT']
22
- sinatra_warn 'the SWIFT environment variable is deprecated;',
23
- 'use Rack::Handler::SwiftipliedMongrel instead.'
24
7
  require 'swiftcore/swiftiplied_mongrel'
25
8
  puts "Using Swiftiplied Mongrel"
26
9
  elsif ENV['EVENT']
27
- sinatra_warn 'the EVENT environment variable is deprecated;',
28
- 'use Rack::Handler::EventedMongrel instead.'
29
10
  require 'swiftcore/evented_mongrel'
30
11
  puts "Using Evented Mongrel"
31
12
  end
32
13
 
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.
14
+ # Deprecated. Make Rack 0.9.0 backward compatibile with 0.4.0
15
+ # mime types
36
16
  require 'rack/file'
37
17
  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
18
+ unless defined? MIME_TYPES
19
+ MIME_TYPES = Hash.new {|hash,key|
20
+ Rack::Mime::MIME_TYPES[".#{key}"] }
47
21
  end
48
22
  end
49
23
 
24
+ # Deprecated. Rack::Utils will not extend itself in the future. Sinatra::Base
25
+ # includes Rack::Utils, however.
26
+ module Rack::Utils ; extend self ; end
27
+
50
28
  module Sinatra
51
29
  module Compat
52
30
  end
53
31
 
54
- # The ServerError exception is deprecated. Any exception is considered an
55
- # internal server error.
32
+ # Deprecated. Use: error
56
33
  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
34
  def code ; 500 ; end
62
35
  end
63
36
 
64
37
  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
38
+ # Deprecated.
39
+ FORWARD_METHODS = Sinatra::Delegator::METHODS
75
40
 
76
41
  # Deprecated. Use: response['Header-Name']
77
42
  def headers(header=nil)
78
- sinatra_warn "The 'headers' method is deprecated; use 'response' instead."
79
43
  response.headers.merge!(header) if header
80
44
  response.headers
81
45
  end
82
46
  alias :header :headers
83
47
 
84
48
  # 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
49
+ alias :stop :halt
89
50
 
90
51
  # 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
52
+ alias :entity_tag :etag
95
53
 
96
54
  # The :disposition option is deprecated; use: #attachment. This method
97
55
  # setting the Content-Transfer-Encoding header is deprecated.
98
- #--
99
- # TODO deprecation warning for :disposition argument.
100
56
  def send_file(path, opts={})
101
57
  opts[:disposition] = 'attachment' if !opts.key?(:disposition)
102
58
  attachment opts[:filename] || path if opts[:filename] || opts[:disposition]
@@ -104,89 +60,48 @@ module Sinatra
104
60
  super(path, opts)
105
61
  end
106
62
 
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
63
+ def options ; self.class.options ; end
140
64
 
141
65
  class << self
142
66
  # 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)
67
+ def options ; Options.new(self) ; end
68
+
69
+ class Options < Struct.new(:target) #:nodoc:
70
+ def method_missing(name, *args, &block)
71
+ if target.respond_to?(name)
72
+ target.__send__(name, *args, &block)
73
+ elsif args.empty? && name.to_s !~ /=$/
74
+ nil
75
+ else
76
+ super
77
+ end
78
+ end
146
79
  end
147
80
 
148
81
  # 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
82
+ alias :configures :configure
153
83
 
154
84
  # Deprecated. Use: set
155
85
  def default_options
156
- sinatra_warn "Sinatra::Application.default_options is deprecated; use 'set' instead."
157
86
  fake = lambda { |options| set(options) }
158
87
  def fake.merge!(options) ; call(options) ; end
159
88
  fake
160
89
  end
161
90
 
162
91
  # 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
92
+ alias :set_option :set
93
+ alias :set_options :set
172
94
 
173
95
  # Deprecated. Use: set :environment, ENV
174
96
  def env=(value)
175
- sinatra_warn "The :env option is deprecated; use :environment instead."
176
97
  set :environment, value
177
98
  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
99
+ alias :env :environment
184
100
  end
185
101
 
186
102
  # Deprecated. Missing messages are no longer delegated to @response.
187
103
  def method_missing(name, *args, &b)
188
104
  if @response.respond_to?(name)
189
- sinatra_warn "The '#{name}' method is deprecated; use 'response.#{name}' instead."
190
105
  @response.send(name, *args, &b)
191
106
  else
192
107
  super
@@ -197,43 +112,30 @@ module Sinatra
197
112
  class << self
198
113
  # Deprecated. Use: Sinatra::Application
199
114
  def application
200
- sinatra_warn "Sinatra.application is deprecated; use Sinatra::Application instead."
201
115
  Sinatra::Application
202
116
  end
203
117
 
118
+ # Deprecated. Use: error 404
119
+ def not_found(&block)
120
+ error 404, &block
121
+ end
122
+
204
123
  # Deprecated. Use: Sinatra::Application.reset!
205
124
  def application=(value)
206
125
  raise ArgumentError unless value.nil?
207
- sinatra_warn "Setting Sinatra.application to nil is deprecated; create a new instance instead."
208
126
  Sinatra.class_eval do
209
127
  remove_const :Application
210
128
  const_set :Application, Class.new(Sinatra::Default)
211
129
  end
212
130
  end
213
131
 
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
132
+ # Deprecated. Use: Sinatra::Application
133
+ alias :build_application :application
233
134
 
234
- def env
235
- sinatra_warn "Sinatra.env is deprecated; use Sinatra::Application.environment instead."
236
- options.environment
237
- end
135
+ # Deprecated.
136
+ def options ; Sinatra::Application.options ; end
137
+ def port ; options.port ; end
138
+ def host ; options.host ; end
139
+ def env ; options.environment ; end
238
140
  end
239
141
  end
@@ -1,9 +1,2 @@
1
1
  require 'sinatra/test'
2
2
  require 'spec/interop/test'
3
-
4
- Sinatra::Default.set(
5
- :env => :test,
6
- :run => false,
7
- :raise_errors => true,
8
- :logging => false
9
- )
@@ -1,9 +1,2 @@
1
1
  require 'test/spec'
2
2
  require 'sinatra/test'
3
- require 'sinatra/test/unit'
4
-
5
- module Sinatra::Test
6
- def should
7
- @response.should
8
- end
9
- end
@@ -1,5 +1,5 @@
1
- require 'sinatra/test'
2
1
  require 'test/unit'
2
+ require 'sinatra/test'
3
3
 
4
4
  Test::Unit::TestCase.send :include, Sinatra::Test
5
5