rtomayko-sinatra 0.9.0 → 0.9.0.2

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.
@@ -1,58 +1,102 @@
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
+
1
7
  require 'ostruct'
2
8
  require 'sinatra/base'
3
9
  require 'sinatra/main'
4
10
 
5
- # Deprecated. Do we still need this?
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.
6
21
  if ENV['SWIFT']
22
+ sinatra_warn 'the SWIFT environment variable is deprecated;',
23
+ 'use Rack::Handler::SwiftipliedMongrel instead.'
7
24
  require 'swiftcore/swiftiplied_mongrel'
8
25
  puts "Using Swiftiplied Mongrel"
9
26
  elsif ENV['EVENT']
27
+ sinatra_warn 'the EVENT environment variable is deprecated;',
28
+ 'use Rack::Handler::EventedMongrel instead.'
10
29
  require 'swiftcore/evented_mongrel'
11
30
  puts "Using Evented Mongrel"
12
31
  end
13
32
 
14
- # Deprecated. Make Rack 0.9.0 backward compatibile with 0.4.0
15
- # mime types
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.
16
36
  require 'rack/file'
17
37
  class Rack::File
18
- unless defined? MIME_TYPES
19
- MIME_TYPES = Hash.new {|hash,key|
20
- Rack::Mime::MIME_TYPES[".#{key}"] }
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
21
47
  end
22
48
  end
23
49
 
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
-
28
50
  module Sinatra
29
51
  module Compat
30
52
  end
31
53
 
32
- # Deprecated. Use: error
54
+ # The ServerError exception is deprecated. Any exception is considered an
55
+ # internal server error.
33
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
34
61
  def code ; 500 ; end
35
62
  end
36
63
 
37
64
  class Default < Base
38
- # Deprecated.
39
- FORWARD_METHODS = Sinatra::Delegator::METHODS
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
40
75
 
41
76
  # Deprecated. Use: response['Header-Name']
42
77
  def headers(header=nil)
78
+ sinatra_warn "The 'headers' method is deprecated; use 'response' instead."
43
79
  response.headers.merge!(header) if header
44
80
  response.headers
45
81
  end
46
82
  alias :header :headers
47
83
 
48
84
  # Deprecated. Use: halt
49
- alias :stop :halt
85
+ def stop(*args, &block)
86
+ sinatra_warn "The 'stop' method is deprecated; use 'halt' instead."
87
+ halt(*args, &block)
88
+ end
50
89
 
51
90
  # Deprecated. Use: etag
52
- alias :entity_tag :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
53
95
 
54
96
  # The :disposition option is deprecated; use: #attachment. This method
55
97
  # setting the Content-Transfer-Encoding header is deprecated.
98
+ #--
99
+ # TODO deprecation warning for :disposition argument.
56
100
  def send_file(path, opts={})
57
101
  opts[:disposition] = 'attachment' if !opts.key?(:disposition)
58
102
  attachment opts[:filename] || path if opts[:filename] || opts[:disposition]
@@ -60,48 +104,89 @@ module Sinatra
60
104
  super(path, opts)
61
105
  end
62
106
 
63
- def options ; self.class.options ; end
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
64
140
 
65
141
  class << self
66
142
  # Deprecated. Options are stored directly on the class object.
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
143
+ def options
144
+ sinatra_warn "The 'options' class method is deprecated; use 'self' instead."
145
+ Options.new(self)
79
146
  end
80
147
 
81
148
  # Deprecated. Use: configure
82
- alias :configures :configure
149
+ def configures(*args, &block)
150
+ sinatra_warn "The 'configures' method is deprecated; use 'configure' instead."
151
+ configure(*args, &block)
152
+ end
83
153
 
84
154
  # Deprecated. Use: set
85
155
  def default_options
156
+ sinatra_warn "Sinatra::Application.default_options is deprecated; use 'set' instead."
86
157
  fake = lambda { |options| set(options) }
87
158
  def fake.merge!(options) ; call(options) ; end
88
159
  fake
89
160
  end
90
161
 
91
162
  # Deprecated. Use: set
92
- alias :set_option :set
93
- alias :set_options :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
94
172
 
95
173
  # Deprecated. Use: set :environment, ENV
96
174
  def env=(value)
175
+ sinatra_warn "The :env option is deprecated; use :environment instead."
97
176
  set :environment, value
98
177
  end
99
- alias :env :environment
178
+
179
+ # Deprecated. Use: options.environment
180
+ def env
181
+ sinatra_warn "The :env option is deprecated; use :environment instead."
182
+ environment
183
+ end
100
184
  end
101
185
 
102
186
  # Deprecated. Missing messages are no longer delegated to @response.
103
187
  def method_missing(name, *args, &b)
104
188
  if @response.respond_to?(name)
189
+ sinatra_warn "The '#{name}' method is deprecated; use 'response.#{name}' instead."
105
190
  @response.send(name, *args, &b)
106
191
  else
107
192
  super
@@ -112,30 +197,43 @@ module Sinatra
112
197
  class << self
113
198
  # Deprecated. Use: Sinatra::Application
114
199
  def application
200
+ sinatra_warn "Sinatra.application is deprecated; use Sinatra::Application instead."
115
201
  Sinatra::Application
116
202
  end
117
203
 
118
- # Deprecated. Use: error 404
119
- def not_found(&block)
120
- error 404, &block
121
- end
122
-
123
204
  # Deprecated. Use: Sinatra::Application.reset!
124
205
  def application=(value)
125
206
  raise ArgumentError unless value.nil?
207
+ sinatra_warn "Setting Sinatra.application to nil is deprecated; create a new instance instead."
126
208
  Sinatra.class_eval do
127
209
  remove_const :Application
128
210
  const_set :Application, Class.new(Sinatra::Default)
129
211
  end
130
212
  end
131
213
 
132
- # Deprecated. Use: Sinatra::Application
133
- alias :build_application :application
214
+ def build_application
215
+ sinatra_warn "Sinatra.build_application is deprecated; use Sinatra::Application instead."
216
+ Sinatra::Application
217
+ end
134
218
 
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
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
140
238
  end
141
239
  end
data/lib/sinatra/test.rb CHANGED
@@ -1,104 +1,57 @@
1
1
  require 'sinatra/base'
2
- require 'test/unit'
3
2
 
4
- module Sinatra::Test
5
- include Rack::Utils
6
-
7
- attr_reader :app, :request, :response
8
-
9
- def mock_app(base=Sinatra::Base, &block)
10
- @app = Sinatra.new(base, &block)
11
- end
12
-
13
- def request(verb, path, *args)
14
- fail "@app not set - cannot make request" if @app.nil?
15
- @request = Rack::MockRequest.new(@app)
16
- opts, input =
17
- case args.size
18
- when 2 # input, env
19
- input, env = args
20
- if input.kind_of?(Hash) # params, env
21
- [env, param_string(input)]
22
- else
23
- [env, input]
24
- end
25
- when 1 # params
26
- if (data = args.first).kind_of?(Hash)
27
- env = (data.delete(:env) || {})
28
- [env, param_string(data)]
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, build_query(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, build_query(data)]
27
+ else
28
+ [{}, data]
29
+ end
30
+ when 0
31
+ [{}, '']
29
32
  else
30
- [{}, data]
33
+ raise ArgumentError, "zero, one, or two arguments expected"
31
34
  end
32
- when 0
33
- [{}, '']
34
- else
35
- raise ArgumentError, "zero, one, or two arguments expected"
36
- end
37
- opts = rack_opts(opts)
38
- opts[:input] ||= input
39
- yield @request if block_given?
40
- @response = @request.request(verb, path, opts)
41
- end
42
-
43
- def get(path, *args, &b) ; request('GET', path, *args, &b) ; end
44
- def head(path, *args, &b) ; request('HEAD', path, *args, &b) ; end
45
- def post(path, *args, &b) ; request('POST', path, *args, &b) ; end
46
- def put(path, *args, &b) ; request('PUT', path, *args, &b) ; end
47
- def delete(path, *args, &b) ; request('DELETE', path, *args, &b) ; end
48
-
49
- def follow!
50
- request 'GET', @response.location
51
- end
52
-
53
- def should
54
- @response.should
55
- end
56
-
57
- def body
58
- @response.body
59
- end
60
-
61
- def status
62
- @response.status
63
- end
64
-
65
- RACK_OPT_NAMES = {
66
- :accept => "HTTP_ACCEPT",
67
- :agent => "HTTP_USER_AGENT",
68
- :host => "HTTP_HOST",
69
- :session => "HTTP_COOKIE",
70
- :cookies => "HTTP_COOKIE",
71
- :content_type => "CONTENT_TYPE"
72
- }
73
-
74
- def rack_opts(opts)
75
- opts.inject({}) do |hash,(key,val)|
76
- key = RACK_OPT_NAMES[key] || key
77
- hash[key] = val
78
- hash
35
+ opts = rack_opts(opts)
36
+ opts[:input] ||= input
37
+ yield @request if block_given?
38
+ @response = @request.request(verb, path, opts)
79
39
  end
80
- end
81
40
 
82
- def env_for(opts={})
83
- opts = rack_opts(opts)
84
- Rack::MockRequest.env_for(opts)
85
- end
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
86
46
 
87
- def param_string(hash)
88
- hash.map { |pair| pair.map{|v|escape(v)}.join('=') }.join('&')
89
- end
90
-
91
- if defined? Sinatra::Compat
92
- # Deprecated. Use: "get" instead of "get_it".
93
- %w(get head post put delete).each do |verb|
94
- alias_method "#{verb}_it", verb
95
- remove_method verb
47
+ def follow!
48
+ test_request 'GET', @response.location
96
49
  end
97
50
 
98
- include Sinatra::Delegator
51
+ def body ; @response.body ; end
52
+ def status ; @response.status ; end
99
53
 
100
- # Deprecated. Tests no longer delegate missing methods to the
101
- # mock response. Use: @response
54
+ # Delegate other missing methods to @response.
102
55
  def method_missing(name, *args, &block)
103
56
  if @response && @response.respond_to?(name)
104
57
  @response.send(name, *args, &block)
@@ -106,5 +59,53 @@ module Sinatra::Test
106
59
  super
107
60
  end
108
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
+ if defined? Sinatra::Compat
86
+ # Deprecated. Use: "get" instead of "get_it".
87
+ %w(get head post put delete).each do |verb|
88
+ eval <<-RUBY, binding, __FILE__, __LINE__
89
+ def #{verb}_it(*args, &block)
90
+ sinatra_warn "The #{verb}_it method is deprecated; use #{verb} instead."
91
+ test_request('#{verb.upcase}', *args, &block)
92
+ end
93
+ RUBY
94
+ end
95
+
96
+ # Deprecated. Use: build_query instead.
97
+ def param_string(hash)
98
+ sinatra_warn "The param_string method is deprecated; use build_query instead."
99
+ build_query(hash)
100
+ end
101
+ end
102
+ end
103
+
104
+ class TestHarness
105
+ include Test
106
+
107
+ def initialize(app=nil)
108
+ @app = app || Sinatra::Application
109
+ end
109
110
  end
110
111
  end