sinatra 0.1.7 → 0.2.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.

Files changed (95) hide show
  1. data/CHANGELOG +1 -8
  2. data/Manifest +42 -49
  3. data/README.rdoc +430 -0
  4. data/Rakefile +22 -28
  5. data/images/404.png +0 -0
  6. data/images/500.png +0 -0
  7. data/index.html +9 -0
  8. data/lib/sinatra.rb +1144 -46
  9. data/lib/sinatra/test/methods.rb +56 -0
  10. data/lib/sinatra/test/spec.rb +10 -0
  11. data/lib/sinatra/test/unit.rb +13 -0
  12. data/sinatra.gemspec +44 -40
  13. data/test/app_test.rb +150 -0
  14. data/test/application_test.rb +175 -0
  15. data/test/builder_test.rb +101 -0
  16. data/test/custom_error_test.rb +67 -0
  17. data/test/diddy_test.rb +41 -0
  18. data/test/erb_test.rb +116 -0
  19. data/test/event_context_test.rb +15 -0
  20. data/test/events_test.rb +50 -0
  21. data/test/haml_test.rb +181 -0
  22. data/test/helper.rb +3 -16
  23. data/test/mapped_error_test.rb +61 -0
  24. data/test/public/foo.xml +1 -0
  25. data/test/rest_test.rb +16 -0
  26. data/test/sass_test.rb +57 -0
  27. data/test/sessions_test.rb +40 -0
  28. data/test/streaming_test.rb +112 -0
  29. data/test/sym_params_test.rb +19 -0
  30. data/test/template_test.rb +30 -0
  31. data/test/use_in_file_templates_test.rb +48 -0
  32. data/test/views/foo.builder +1 -0
  33. data/test/views/foo.erb +1 -0
  34. data/test/views/foo.haml +1 -0
  35. data/test/views/foo.sass +2 -0
  36. data/test/views/foo_layout.erb +2 -0
  37. data/test/views/foo_layout.haml +2 -0
  38. data/test/views/layout_test/foo.builder +1 -0
  39. data/test/views/layout_test/foo.erb +1 -0
  40. data/test/views/layout_test/foo.haml +1 -0
  41. data/test/views/layout_test/foo.sass +2 -0
  42. data/test/views/layout_test/layout.builder +3 -0
  43. data/test/views/layout_test/layout.erb +1 -0
  44. data/test/views/layout_test/layout.haml +1 -0
  45. data/test/views/layout_test/layout.sass +2 -0
  46. data/test/views/no_layout/no_layout.builder +1 -0
  47. data/test/views/no_layout/no_layout.haml +1 -0
  48. metadata +122 -98
  49. data/LICENSE +0 -22
  50. data/README +0 -100
  51. data/RakeFile +0 -35
  52. data/examples/hello/hello.rb +0 -28
  53. data/examples/hello/views/hello.erb +0 -1
  54. data/examples/todo/todo.rb +0 -38
  55. data/files/default_index.erb +0 -42
  56. data/files/error.erb +0 -9
  57. data/files/logo.png +0 -0
  58. data/files/not_found.erb +0 -52
  59. data/lib/sinatra/context.rb +0 -88
  60. data/lib/sinatra/context/renderer.rb +0 -75
  61. data/lib/sinatra/core_ext/array.rb +0 -5
  62. data/lib/sinatra/core_ext/class.rb +0 -49
  63. data/lib/sinatra/core_ext/hash.rb +0 -7
  64. data/lib/sinatra/core_ext/kernel.rb +0 -16
  65. data/lib/sinatra/core_ext/metaid.rb +0 -18
  66. data/lib/sinatra/core_ext/module.rb +0 -11
  67. data/lib/sinatra/core_ext/symbol.rb +0 -5
  68. data/lib/sinatra/dispatcher.rb +0 -27
  69. data/lib/sinatra/dsl.rb +0 -176
  70. data/lib/sinatra/environment.rb +0 -15
  71. data/lib/sinatra/event.rb +0 -238
  72. data/lib/sinatra/irb.rb +0 -56
  73. data/lib/sinatra/loader.rb +0 -31
  74. data/lib/sinatra/logger.rb +0 -22
  75. data/lib/sinatra/options.rb +0 -49
  76. data/lib/sinatra/rack_ext/request.rb +0 -15
  77. data/lib/sinatra/route.rb +0 -65
  78. data/lib/sinatra/server.rb +0 -57
  79. data/lib/sinatra/sessions.rb +0 -21
  80. data/lib/sinatra/test_methods.rb +0 -55
  81. data/site/index.htm +0 -104
  82. data/site/index.html +0 -104
  83. data/site/logo.png +0 -0
  84. data/test/sinatra/dispatcher_test.rb +0 -91
  85. data/test/sinatra/event_test.rb +0 -46
  86. data/test/sinatra/renderer_test.rb +0 -47
  87. data/test/sinatra/request_test.rb +0 -21
  88. data/test/sinatra/route_test.rb +0 -21
  89. data/test/sinatra/static_files/foo.txt +0 -1
  90. data/test/sinatra/static_files_test.rb +0 -48
  91. data/test/sinatra/url_test.rb +0 -18
  92. data/vendor/erb/init.rb +0 -3
  93. data/vendor/erb/lib/erb.rb +0 -41
  94. data/vendor/haml/init.rb +0 -3
  95. data/vendor/haml/lib/haml.rb +0 -41
@@ -1,176 +0,0 @@
1
-
2
- module Sinatra
3
-
4
- module Dsl
5
-
6
- # Define an Event that responds to a +path+ on GET method
7
- #
8
- # The +path+ can be a template (i.e. '/:foo/bar/:baz'). When recognized, it will add <tt>:foo</tt> and <tt>:baz</tt> to +params+ with their values.
9
- #
10
- # Example:
11
- # # Going RESTful
12
- #
13
- # get '/' do
14
- # .. show stuff ..
15
- # end
16
- #
17
- # post '/' do
18
- # .. add stuff ..
19
- # redirect '/'
20
- # end
21
- #
22
- # put '/:id' do
23
- # .. update params[:id] ..
24
- # redirect '/'
25
- # end
26
- #
27
- # delete '/:id' do
28
- # .. delete params[:id] ..
29
- # redirect '/'
30
- # end
31
- #
32
- # BIG NOTE: PUT and DELETE are trigged when POSTing to their paths with a <tt>_method</tt> param whose value is PUT or DELETE
33
- #
34
- def get(path, &block)
35
- Sinatra::Event.new(:get, path, &block)
36
- end
37
-
38
- # Same as get but responds to POST
39
- def post(path, &block)
40
- Sinatra::Event.new(:post, path, &block)
41
- end
42
-
43
- # Same as get but responds to PUT
44
- #
45
- # BIG NOTE: PUT and DELETE are trigged when POSTing to their paths with a <tt>_method</tt> param whose value is PUT or DELETE
46
- def put(path, &block)
47
- Sinatra::Event.new(:put, path, &block)
48
- end
49
-
50
- # Same as get but responds to DELETE
51
- #
52
- # BIG NOTE: PUT and DELETE are trigged when POSTing to their paths with a <tt>_method</tt> param whose value is PUT or DELETE
53
- def delete(path, &block)
54
- Sinatra::Event.new(:delete, path, &block)
55
- end
56
-
57
- # Run given block after each Event's execution
58
- # Usage:
59
- # before_attend do
60
- # logger.debug "After event attend!"
61
- # end
62
- # or
63
- # before_attend :authorize # authorize is a helper method defined using helpers
64
- #
65
- # Stop execution using - throw :halt
66
- # before_attend do
67
- # throw :halt, 401 unless has_access?
68
- # end
69
- # Throw a Symbol to execute a helper method
70
- # Throw a String to render it as the content
71
- # Throw a Fixnum to set the status
72
- #
73
- def before_attend(filter_name = nil, &block)
74
- Sinatra::Event.before_attend(filter_name, &block)
75
- end
76
-
77
- # Run given block after each Event's execution
78
- # Example:
79
- # after_attend do
80
- # logger.debug "After event attend!"
81
- # end
82
- # or
83
- # after_attend :clean_up # clean_up is a helper method defined using helpers
84
- #
85
- def after_attend(filter_name = nil, &block)
86
- Sinatra::Event.after_attend(filter_name, &block)
87
- end
88
-
89
- # Add methods to each event for use during execution
90
- #
91
- # Example:
92
- # helpers do
93
- # def foo
94
- # 'foo!'
95
- # end
96
- # end
97
- #
98
- # get '/bar' do
99
- # foo
100
- # end
101
- #
102
- # get_it '/bar' # => 'foo!'
103
- #
104
- def helpers(&block)
105
- Sinatra::EventContext.class_eval(&block)
106
- end
107
-
108
- # Maps a path to a physical directory containing static files
109
- #
110
- # Example:
111
- # static '/p', 'public'
112
- #
113
- def static(path, root)
114
- Sinatra::StaticEvent.new(path, root)
115
- end
116
-
117
- # Execute block if in environment is equal to env (Used for configuration)
118
- def config_for(env = :development)
119
- yield if Sinatra::Options.environment == env.to_sym
120
- end
121
-
122
- # Define named layouts (default name is <tt>:layout</tt>)
123
- #
124
- # Examples:
125
- # # Default layout in Erb
126
- # layout do
127
- # '-- <%= yield %> --'
128
- # end
129
- #
130
- # # Named layout in Haml
131
- # layout :for_haml do
132
- # '== XXXX #{yield} XXXX'
133
- # end
134
- #
135
- # # Loads layout named <tt>:"foo.erb"</tt> from file (default behaviour if block is omitted)
136
- # layout 'foo.erb' # looks for foo.erb. This is odd an is being re-thought
137
- #
138
- # def layout(name = :layout, options = {})
139
- # Layouts[name] = unless block_given?
140
- # File.read("%s/%s" % [options[:views_directory] || 'views', name])
141
- # else
142
- # yield
143
- # end
144
- # end
145
- #
146
- # Cool trick:
147
- #
148
- # # Send a one-time layout to renderer method
149
- # get '/cooltrick' do
150
- # erb 'wicked' do
151
- # 'Cool <%= yield %> Trick'
152
- # end
153
- # end
154
- #
155
- # get_it '/cooltrick' # => 'Cool wicked Trick'
156
- #
157
- def layout(name = :layout, options = {})
158
- Layouts[name] = unless block_given?
159
- File.read("%s/%s" % [options[:views_directory] || 'views', name])
160
- else
161
- yield
162
- end
163
- end
164
-
165
- # Turn sessions <tt>:on</tt> or <tt>:off</tt>
166
- #
167
- # NOTE: There is currently no way to turn it on or off per Event... patches anyone?)
168
- def sessions(on_off)
169
- Sinatra::Session::Cookie.use = on_off
170
- end
171
-
172
- end
173
-
174
- end
175
-
176
- include Sinatra::Dsl
@@ -1,15 +0,0 @@
1
- module Sinatra
2
- module Environment
3
- extend self
4
-
5
- def prepare
6
- Options.parse!(ARGV)
7
- end
8
-
9
- def prepare_loggers(logger = Logger.new(open(Options.log_file, 'w')))
10
- [Server, EventContext, Event, Dispatcher].each do |klass|
11
- klass.logger = logger
12
- end
13
- end
14
- end
15
- end
@@ -1,238 +0,0 @@
1
- require 'thread'
2
-
3
- module Sinatra
4
-
5
- module EventManager # :nodoc:
6
- extend self
7
-
8
- def reset!
9
- @events.clear if @events
10
- end
11
-
12
- def events
13
- @events || []
14
- end
15
-
16
- def register_event(event)
17
- (@events ||= []) << event
18
- end
19
-
20
- def determine_event(verb, path, if_nil = :present_error)
21
- event = events.find(method(if_nil)) do |e|
22
- e.verb == verb && e.recognize(path)
23
- end
24
- end
25
-
26
- def present_error
27
- determine_event(:get, '404', :not_found)
28
- end
29
-
30
- def not_found
31
- Event.new(:get, 'not_found', false) do
32
- status 404
33
-
34
- if request.path_info == '/' && request.request_method == 'GET'
35
- erb :default_index, :views_directory => SINATRA_ROOT + '/files'
36
- else
37
- erb :not_found, :views_directory => SINATRA_ROOT + '/files'
38
- end
39
- end
40
- end
41
-
42
- end
43
-
44
- class Event # :nodoc:
45
-
46
- cattr_accessor :logger
47
- cattr_accessor :after_filters
48
- cattr_accessor :before_filters
49
-
50
- @@mutex = Mutex.new
51
-
52
- self.before_filters = []
53
- self.after_filters = []
54
-
55
- def self.before_attend(method_name = nil, &block)
56
- setup_filter(:before_filters, method_name, &block)
57
- end
58
-
59
- def self.after_attend(method_name = nil, &block)
60
- setup_filter(:after_filters, method_name, &block)
61
- end
62
-
63
- def self.setup_filter(filter_set_name, method_name, &block)
64
- raise "Must specify method or block" if method_name.nil? and !block_given?
65
- send(filter_set_name) << if block_given?
66
- block
67
- else
68
- method_name
69
- end
70
- end
71
-
72
- after_attend :log_event
73
-
74
- attr_reader :path, :verb
75
-
76
- def initialize(verb, path, register = true, &block)
77
- @verb = verb
78
- @path = path
79
- @route = Route.new(path)
80
- @block = block
81
- EventManager.register_event(self) if register
82
- end
83
-
84
- def attend(request)
85
- request.params.merge!(@route.params)
86
- context = EventContext.new(request)
87
- run_safely do
88
- caught = catch(:halt) do
89
- call_filters(before_filters, context)
90
- end
91
- body = case caught
92
- when :filter_chain_completed
93
- begin
94
- context.instance_eval(&@block) if @block
95
- rescue => e
96
- context.error e
97
- end
98
- when Symbol
99
- context.send(caught)
100
- when String
101
- caught
102
- when Fixnum
103
- context.status caught
104
- end
105
- context.body context.body || body || ''
106
- call_filters(after_filters, context)
107
- end
108
- context
109
- end
110
- alias :call :attend
111
-
112
- def recognize(path)
113
- @route.recognize(path)
114
- end
115
-
116
- private
117
-
118
- def run_safely
119
- if Options.use_mutex?
120
- @@mutex.synchronize do
121
- yield
122
- end
123
- else
124
- yield
125
- end
126
- end
127
-
128
- # Adapted from Merb
129
- # calls a filter chain according to rules.
130
- def call_filters(filter_set, context)
131
- filter_set.each do |filter|
132
- case filter
133
- when Symbol, String
134
- context.send(filter)
135
- when Proc
136
- context.instance_eval(&filter)
137
- end
138
- end
139
- :filter_chain_completed
140
- end
141
-
142
- end
143
-
144
- class StaticEvent < Event # :nodoc:
145
-
146
- def initialize(path, root, register = true)
147
- @root = root
148
- super(:get, path, register)
149
- end
150
-
151
- def recognize(path)
152
- filename = physical_path_for(path)
153
- File.exists?(filename) && File.file?(filename)
154
- end
155
-
156
- def physical_path_for(path)
157
- path.gsub(/^#{@path}/, @root)
158
- end
159
-
160
- def attend(request)
161
- @filename = physical_path_for(request.path_info)
162
- context = EventContext.new(request)
163
- context.body self
164
- context.header 'Content-Type' => MIME_TYPES[File.extname(@filename)[1..-1]]
165
- context.header 'Content-Length' => File.size(@filename).to_s
166
- context
167
- end
168
-
169
- def each
170
- File.open(@filename, "rb") do |file|
171
- while part = file.read(8192)
172
- yield part
173
- end
174
- end
175
- end
176
-
177
- # :stopdoc:
178
- # From WEBrick.
179
- MIME_TYPES = {
180
- "ai" => "application/postscript",
181
- "asc" => "text/plain",
182
- "avi" => "video/x-msvideo",
183
- "bin" => "application/octet-stream",
184
- "bmp" => "image/bmp",
185
- "class" => "application/octet-stream",
186
- "cer" => "application/pkix-cert",
187
- "crl" => "application/pkix-crl",
188
- "crt" => "application/x-x509-ca-cert",
189
- #"crl" => "application/x-pkcs7-crl",
190
- "css" => "text/css",
191
- "dms" => "application/octet-stream",
192
- "doc" => "application/msword",
193
- "dvi" => "application/x-dvi",
194
- "eps" => "application/postscript",
195
- "etx" => "text/x-setext",
196
- "exe" => "application/octet-stream",
197
- "gif" => "image/gif",
198
- "htm" => "text/html",
199
- "html" => "text/html",
200
- "jpe" => "image/jpeg",
201
- "jpeg" => "image/jpeg",
202
- "jpg" => "image/jpeg",
203
- "lha" => "application/octet-stream",
204
- "lzh" => "application/octet-stream",
205
- "mov" => "video/quicktime",
206
- "mpe" => "video/mpeg",
207
- "mpeg" => "video/mpeg",
208
- "mpg" => "video/mpeg",
209
- "pbm" => "image/x-portable-bitmap",
210
- "pdf" => "application/pdf",
211
- "pgm" => "image/x-portable-graymap",
212
- "png" => "image/png",
213
- "pnm" => "image/x-portable-anymap",
214
- "ppm" => "image/x-portable-pixmap",
215
- "ppt" => "application/vnd.ms-powerpoint",
216
- "ps" => "application/postscript",
217
- "qt" => "video/quicktime",
218
- "ras" => "image/x-cmu-raster",
219
- "rb" => "text/plain",
220
- "rd" => "text/plain",
221
- "rtf" => "application/rtf",
222
- "sgm" => "text/sgml",
223
- "sgml" => "text/sgml",
224
- "tif" => "image/tiff",
225
- "tiff" => "image/tiff",
226
- "txt" => "text/plain",
227
- "xbm" => "image/x-xbitmap",
228
- "xls" => "application/vnd.ms-excel",
229
- "xml" => "text/xml",
230
- "xpm" => "image/x-xpixmap",
231
- "xwd" => "image/x-xwindowdump",
232
- "zip" => "application/zip",
233
- }
234
- # :startdoc:
235
-
236
- end
237
-
238
- end
@@ -1,56 +0,0 @@
1
- module Sinatra
2
-
3
- # Sinatra Irb is entered via <tt>ruby myapp.rb -c</tt> (replace myapp.rb with your app filename)
4
- #
5
- # Be sure to also check out Sinatra::TestMethods for more cool stuff when your in Irb
6
- #
7
- module Irb
8
- extend self
9
-
10
- # taken from merb
11
- def start! #:nodoc:
12
-
13
- Object.send(:include, TestMethods) # added to allow post_to in console
14
-
15
- Object.class_eval do
16
- # Reload all Sinatra and App specific files
17
- def reload!
18
- Loader.reload!
19
- end
20
-
21
- # Show the +body+ with result info in your text editor!!! Great Job!
22
- def show!(editor = nil)
23
- editor = editor || ENV['EDITOR']
24
- IO.popen(editor, 'w') do |f|
25
- f.puts "<!--"
26
- f.puts result_info
27
- f.puts "-->"
28
- f.puts
29
- f.puts body
30
- end
31
- nil
32
- end
33
- alias :mate :show!
34
-
35
- def result_info #:nodoc:
36
- info = <<-end_info
37
- # Status: #{status}
38
- # Headers: #{headers.inspect}
39
- end_info
40
- end
41
- end
42
-
43
- ARGV.clear # Avoid passing args to IRB
44
- require 'irb'
45
- require 'irb/completion'
46
- def exit
47
- exit!
48
- end
49
- if File.exists? ".irbrc"
50
- ENV['IRBRC'] = ".irbrc"
51
- end
52
- IRB.start
53
- exit!
54
- end
55
- end
56
- end