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.
- data/CHANGELOG +1 -8
- data/Manifest +42 -49
- data/README.rdoc +430 -0
- data/Rakefile +22 -28
- data/images/404.png +0 -0
- data/images/500.png +0 -0
- data/index.html +9 -0
- data/lib/sinatra.rb +1144 -46
- data/lib/sinatra/test/methods.rb +56 -0
- data/lib/sinatra/test/spec.rb +10 -0
- data/lib/sinatra/test/unit.rb +13 -0
- data/sinatra.gemspec +44 -40
- data/test/app_test.rb +150 -0
- data/test/application_test.rb +175 -0
- data/test/builder_test.rb +101 -0
- data/test/custom_error_test.rb +67 -0
- data/test/diddy_test.rb +41 -0
- data/test/erb_test.rb +116 -0
- data/test/event_context_test.rb +15 -0
- data/test/events_test.rb +50 -0
- data/test/haml_test.rb +181 -0
- data/test/helper.rb +3 -16
- data/test/mapped_error_test.rb +61 -0
- data/test/public/foo.xml +1 -0
- data/test/rest_test.rb +16 -0
- data/test/sass_test.rb +57 -0
- data/test/sessions_test.rb +40 -0
- data/test/streaming_test.rb +112 -0
- data/test/sym_params_test.rb +19 -0
- data/test/template_test.rb +30 -0
- data/test/use_in_file_templates_test.rb +48 -0
- data/test/views/foo.builder +1 -0
- data/test/views/foo.erb +1 -0
- data/test/views/foo.haml +1 -0
- data/test/views/foo.sass +2 -0
- data/test/views/foo_layout.erb +2 -0
- data/test/views/foo_layout.haml +2 -0
- data/test/views/layout_test/foo.builder +1 -0
- data/test/views/layout_test/foo.erb +1 -0
- data/test/views/layout_test/foo.haml +1 -0
- data/test/views/layout_test/foo.sass +2 -0
- data/test/views/layout_test/layout.builder +3 -0
- data/test/views/layout_test/layout.erb +1 -0
- data/test/views/layout_test/layout.haml +1 -0
- data/test/views/layout_test/layout.sass +2 -0
- data/test/views/no_layout/no_layout.builder +1 -0
- data/test/views/no_layout/no_layout.haml +1 -0
- metadata +122 -98
- data/LICENSE +0 -22
- data/README +0 -100
- data/RakeFile +0 -35
- data/examples/hello/hello.rb +0 -28
- data/examples/hello/views/hello.erb +0 -1
- data/examples/todo/todo.rb +0 -38
- data/files/default_index.erb +0 -42
- data/files/error.erb +0 -9
- data/files/logo.png +0 -0
- data/files/not_found.erb +0 -52
- data/lib/sinatra/context.rb +0 -88
- data/lib/sinatra/context/renderer.rb +0 -75
- data/lib/sinatra/core_ext/array.rb +0 -5
- data/lib/sinatra/core_ext/class.rb +0 -49
- data/lib/sinatra/core_ext/hash.rb +0 -7
- data/lib/sinatra/core_ext/kernel.rb +0 -16
- data/lib/sinatra/core_ext/metaid.rb +0 -18
- data/lib/sinatra/core_ext/module.rb +0 -11
- data/lib/sinatra/core_ext/symbol.rb +0 -5
- data/lib/sinatra/dispatcher.rb +0 -27
- data/lib/sinatra/dsl.rb +0 -176
- data/lib/sinatra/environment.rb +0 -15
- data/lib/sinatra/event.rb +0 -238
- data/lib/sinatra/irb.rb +0 -56
- data/lib/sinatra/loader.rb +0 -31
- data/lib/sinatra/logger.rb +0 -22
- data/lib/sinatra/options.rb +0 -49
- data/lib/sinatra/rack_ext/request.rb +0 -15
- data/lib/sinatra/route.rb +0 -65
- data/lib/sinatra/server.rb +0 -57
- data/lib/sinatra/sessions.rb +0 -21
- data/lib/sinatra/test_methods.rb +0 -55
- data/site/index.htm +0 -104
- data/site/index.html +0 -104
- data/site/logo.png +0 -0
- data/test/sinatra/dispatcher_test.rb +0 -91
- data/test/sinatra/event_test.rb +0 -46
- data/test/sinatra/renderer_test.rb +0 -47
- data/test/sinatra/request_test.rb +0 -21
- data/test/sinatra/route_test.rb +0 -21
- data/test/sinatra/static_files/foo.txt +0 -1
- data/test/sinatra/static_files_test.rb +0 -48
- data/test/sinatra/url_test.rb +0 -18
- data/vendor/erb/init.rb +0 -3
- data/vendor/erb/lib/erb.rb +0 -41
- data/vendor/haml/init.rb +0 -3
- data/vendor/haml/lib/haml.rb +0 -41
@@ -1 +0,0 @@
|
|
1
|
-
Hello <%= params[:name].capitalize || 'World' %>!
|
data/examples/todo/todo.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
|
2
|
-
require 'sinatra'
|
3
|
-
|
4
|
-
get '/' do
|
5
|
-
@items = session[:items] || []
|
6
|
-
haml <<-haml
|
7
|
-
%script window.document.getElementById('new_item').focus();
|
8
|
-
%h1 Sinatra's todo list
|
9
|
-
%ul
|
10
|
-
- @items.each_with_index do |item, index|
|
11
|
-
%li.item
|
12
|
-
%div
|
13
|
-
= item
|
14
|
-
%form{:action => "/" + index.to_s, :method => 'POST'}
|
15
|
-
%input{:type => 'hidden', :name => '_method', :value => 'DELETE'}
|
16
|
-
%input{:type => 'submit', :value => 'delete'}
|
17
|
-
%form{:action => '/clear', :method => 'POST'}
|
18
|
-
%input{:value => 'clear', :type => :submit}
|
19
|
-
%form{:action => '/', :method => 'POST'}
|
20
|
-
%input{:type => 'textbox', :name => :new_item, :id => 'new_item'}
|
21
|
-
%input{:type => 'submit'}
|
22
|
-
haml
|
23
|
-
end
|
24
|
-
|
25
|
-
post '/' do
|
26
|
-
(session[:items] ||= []) << params[:new_item] unless params[:new_item].to_s.strip.empty?
|
27
|
-
redirect '/'
|
28
|
-
end
|
29
|
-
|
30
|
-
post '/clear' do
|
31
|
-
session[:items].clear
|
32
|
-
redirect '/'
|
33
|
-
end
|
34
|
-
|
35
|
-
delete '/:id' do
|
36
|
-
session[:items].delete_at(params[:id].to_i)
|
37
|
-
redirect '/'
|
38
|
-
end
|
data/files/default_index.erb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
-
|
4
|
-
<html>
|
5
|
-
<head>
|
6
|
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
7
|
-
<title>Sinatra has taken the stage!</title>
|
8
|
-
</head>
|
9
|
-
|
10
|
-
<body>
|
11
|
-
<style type="text/css" media="screen">
|
12
|
-
body {
|
13
|
-
color: #333;
|
14
|
-
font-size: 1.5em;
|
15
|
-
}
|
16
|
-
|
17
|
-
#content {
|
18
|
-
margin: 0 auto;
|
19
|
-
text-align: center;
|
20
|
-
width: 500px;
|
21
|
-
}
|
22
|
-
|
23
|
-
#content pre {
|
24
|
-
text-align: left;
|
25
|
-
background-color: #D8D8D8;
|
26
|
-
padding: 10px;
|
27
|
-
}
|
28
|
-
</style>
|
29
|
-
<div id="content">
|
30
|
-
<div id="banner">
|
31
|
-
<img src="http://sinatra.rubyforge.org/logo.png">
|
32
|
-
</div>
|
33
|
-
<div id="content">
|
34
|
-
<h3>Sing this diddy to move on:</h3>
|
35
|
-
<pre>
|
36
|
-
get "/" do
|
37
|
-
... your code here ..
|
38
|
-
end</pre>
|
39
|
-
</div>
|
40
|
-
</div>
|
41
|
-
</body>
|
42
|
-
</html>
|
data/files/error.erb
DELETED
data/files/logo.png
DELETED
Binary file
|
data/files/not_found.erb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
-
|
4
|
-
<html>
|
5
|
-
|
6
|
-
<head>
|
7
|
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
8
|
-
<title>Not Found :: Sinatra</title>
|
9
|
-
</head>
|
10
|
-
|
11
|
-
<body id="not_found">
|
12
|
-
|
13
|
-
<style type="text/css" media="screen">
|
14
|
-
body {
|
15
|
-
background-color: #fff;
|
16
|
-
}
|
17
|
-
|
18
|
-
.message {
|
19
|
-
padding: 10px;
|
20
|
-
|
21
|
-
font-size: 20px;
|
22
|
-
font-color: #333;
|
23
|
-
}
|
24
|
-
|
25
|
-
#content {
|
26
|
-
}
|
27
|
-
|
28
|
-
.message pre {
|
29
|
-
font-size: 15px;
|
30
|
-
text-align: left;
|
31
|
-
|
32
|
-
background-color: #ccc;
|
33
|
-
|
34
|
-
padding: 10px;
|
35
|
-
}
|
36
|
-
</style>
|
37
|
-
|
38
|
-
<div id="container">
|
39
|
-
<div id="content">
|
40
|
-
<div class='message'>
|
41
|
-
<h3>Sinatra doesn't know this diddy, but he's a quick study.</h3>
|
42
|
-
<p>Sing this one:</p>
|
43
|
-
<pre><%= request.request_method.downcase %> '<%= request.path_info %>' do
|
44
|
-
html "Replace this with your code."
|
45
|
-
end</pre>
|
46
|
-
</div>
|
47
|
-
</div>
|
48
|
-
</div>
|
49
|
-
|
50
|
-
</body>
|
51
|
-
|
52
|
-
</html>
|
data/lib/sinatra/context.rb
DELETED
@@ -1,88 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/context/renderer'
|
2
|
-
|
3
|
-
module Sinatra
|
4
|
-
|
5
|
-
class EventContext
|
6
|
-
|
7
|
-
cattr_accessor :logger
|
8
|
-
attr_reader :request
|
9
|
-
|
10
|
-
include Sinatra::Renderer
|
11
|
-
|
12
|
-
def initialize(request) #:nodoc:
|
13
|
-
@request = request
|
14
|
-
@headers = {}
|
15
|
-
end
|
16
|
-
|
17
|
-
# Sets or returns the status
|
18
|
-
def status(value = nil)
|
19
|
-
@status = value if value
|
20
|
-
@status || 200
|
21
|
-
end
|
22
|
-
|
23
|
-
# Sets or returns the body
|
24
|
-
# *Usage*
|
25
|
-
# body 'test'
|
26
|
-
# or
|
27
|
-
# body do
|
28
|
-
# 'test'
|
29
|
-
# end
|
30
|
-
# both are the same
|
31
|
-
#
|
32
|
-
def body(value = nil, &block)
|
33
|
-
@body = value if value
|
34
|
-
@body = block.call if block
|
35
|
-
@body
|
36
|
-
end
|
37
|
-
|
38
|
-
# Renders an exception to +body+ and sets status to 500
|
39
|
-
def error(value = nil)
|
40
|
-
if value
|
41
|
-
status 500
|
42
|
-
@error = value
|
43
|
-
erb :error, :views_directory => SINATRA_ROOT + '/files/'
|
44
|
-
end
|
45
|
-
@error
|
46
|
-
end
|
47
|
-
|
48
|
-
# Sets or returns response headers
|
49
|
-
#
|
50
|
-
# *Usage*
|
51
|
-
# header 'Content-Type' => 'text/html'
|
52
|
-
# header 'Foo' => 'Bar'
|
53
|
-
# or
|
54
|
-
# headers 'Content-Type' => 'text/html',
|
55
|
-
# 'Foo' => 'Bar'
|
56
|
-
#
|
57
|
-
# Whatever blows your hair back
|
58
|
-
def headers(value = nil)
|
59
|
-
@headers.merge!(value) if value
|
60
|
-
@headers
|
61
|
-
end
|
62
|
-
alias :header :headers
|
63
|
-
|
64
|
-
# Returns a Hash of session data. Keys are symbolized
|
65
|
-
def session
|
66
|
-
request.env['rack.session']
|
67
|
-
end
|
68
|
-
|
69
|
-
# Returns a Hash of params. Keys are symbolized
|
70
|
-
def params
|
71
|
-
@params ||= @request.params.symbolize_keys
|
72
|
-
end
|
73
|
-
|
74
|
-
# Redirect to a url
|
75
|
-
def redirect(path)
|
76
|
-
logger.info "Redirecting to: #{path}"
|
77
|
-
status 302
|
78
|
-
header 'Location' => path
|
79
|
-
end
|
80
|
-
|
81
|
-
def log_event #:nodoc:
|
82
|
-
logger.info "#{request.request_method} #{request.path_info} | Status: #{status} | Params: #{params.inspect}"
|
83
|
-
logger.exception(error) if error
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
Layouts = Hash.new # :nodoc:
|
2
|
-
|
3
|
-
module Sinatra
|
4
|
-
|
5
|
-
# The magic or rendering happens here. This is included in Sinatra::EventContext on load.
|
6
|
-
#
|
7
|
-
# These methods are the foundation for Sinatra::Erb and Sinatra::Haml and allow you to quickly
|
8
|
-
# create custom wrappers for your favorite rendering engines outside of erb and haml.
|
9
|
-
|
10
|
-
module Renderer
|
11
|
-
|
12
|
-
DEFAULT_OPTIONS = {
|
13
|
-
:views_directory => 'views',
|
14
|
-
:layout => :layout
|
15
|
-
}
|
16
|
-
|
17
|
-
|
18
|
-
# Renders templates from a string or file and handles their layouts:
|
19
|
-
#
|
20
|
-
# Example:
|
21
|
-
# module MyRenderer
|
22
|
-
# def my(template, options, &layout)
|
23
|
-
# render(template, :my, options, &layout)
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# def render_my(template)
|
27
|
-
# template.capitalize # It capitalizes templates!!!!! WOW!
|
28
|
-
# end
|
29
|
-
# end
|
30
|
-
# Sinatra::EventContext.send :include, MyRenderer
|
31
|
-
#
|
32
|
-
# get '/' do
|
33
|
-
# my "something"
|
34
|
-
# end
|
35
|
-
#
|
36
|
-
# get_it '/' # => 'Something'
|
37
|
-
#
|
38
|
-
# The second method is named render_extname. render will call this dynamicly
|
39
|
-
#
|
40
|
-
# paramaters:
|
41
|
-
# * +template+ If String, renders the string. If Symbol, reads from file with the basename of the Symbol; uses +renderer+ for extension.
|
42
|
-
# * +renderer+ A symbol defining the render_ method to call and the extension append to +template+ when looking in the +views_directory+
|
43
|
-
# * +options+ An optional Hash of options (see next section)
|
44
|
-
#
|
45
|
-
# options:
|
46
|
-
# * +:views_directory+ Allows you to override the default 'views' directory an look for the template in another
|
47
|
-
# * +:layout+ Which layout to use (see Sinatra::Dsl). false to force a render with no layout. Defaults to :default layout
|
48
|
-
#
|
49
|
-
def render(template, renderer, options = {})
|
50
|
-
options = DEFAULT_OPTIONS.merge(options)
|
51
|
-
|
52
|
-
layout = block_given? ? yield : Layouts[options[:layout]]
|
53
|
-
|
54
|
-
result_method = 'render_%s' % renderer
|
55
|
-
|
56
|
-
if layout
|
57
|
-
send(result_method, layout) { send(result_method, determine_template(template, renderer, options)) }
|
58
|
-
else
|
59
|
-
send(result_method, determine_template(template, renderer, options))
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
protected
|
64
|
-
|
65
|
-
def determine_template(template, ext, options)
|
66
|
-
if template.is_a?(Symbol)
|
67
|
-
File.read("%s/%s.%s" % [options[:views_directory], template, ext])
|
68
|
-
else
|
69
|
-
template
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# Extends the class object with class and instance accessors for class attributes,
|
2
|
-
# just like the native attr* accessors for instance attributes.
|
3
|
-
class Class # :nodoc:
|
4
|
-
def cattr_reader(*syms)
|
5
|
-
syms.flatten.each do |sym|
|
6
|
-
next if sym.is_a?(Hash)
|
7
|
-
class_eval(<<-EOS, __FILE__, __LINE__)
|
8
|
-
unless defined? @@#{sym}
|
9
|
-
@@#{sym} = nil
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.#{sym}
|
13
|
-
@@#{sym}
|
14
|
-
end
|
15
|
-
|
16
|
-
def #{sym}
|
17
|
-
@@#{sym}
|
18
|
-
end
|
19
|
-
EOS
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def cattr_writer(*syms)
|
24
|
-
options = syms.last.is_a?(Hash) ? syms.pop : {}
|
25
|
-
syms.flatten.each do |sym|
|
26
|
-
class_eval(<<-EOS, __FILE__, __LINE__)
|
27
|
-
unless defined? @@#{sym}
|
28
|
-
@@#{sym} = nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.#{sym}=(obj)
|
32
|
-
@@#{sym} = obj
|
33
|
-
end
|
34
|
-
|
35
|
-
#{"
|
36
|
-
def #{sym}=(obj)
|
37
|
-
@@#{sym} = obj
|
38
|
-
end
|
39
|
-
" unless options[:instance_writer] == false }
|
40
|
-
EOS
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def cattr_accessor(*syms)
|
45
|
-
cattr_reader(*syms)
|
46
|
-
cattr_writer(*syms)
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module Kernel
|
2
|
-
# (Taken from ActiveSupport)
|
3
|
-
# Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
|
4
|
-
#
|
5
|
-
# silence_warnings do
|
6
|
-
# value = noisy_call # no warning voiced
|
7
|
-
# end
|
8
|
-
#
|
9
|
-
# noisy_call # warning voiced
|
10
|
-
def silence_warnings
|
11
|
-
old_verbose, $VERBOSE = $VERBOSE, nil
|
12
|
-
yield
|
13
|
-
ensure
|
14
|
-
$VERBOSE = old_verbose
|
15
|
-
end
|
16
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
# Compliments to why for this:
|
2
|
-
# http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
|
3
|
-
|
4
|
-
class Object
|
5
|
-
# The hidden singleton lurks behind everyone
|
6
|
-
def metaclass; class << self; self; end; end
|
7
|
-
def meta_eval &blk; metaclass.instance_eval &blk; end
|
8
|
-
|
9
|
-
# Adds methods to a metaclass
|
10
|
-
def meta_def name, &blk
|
11
|
-
meta_eval { define_method name, &blk }
|
12
|
-
end
|
13
|
-
|
14
|
-
# Defines an instance method within a class
|
15
|
-
def class_def name, &blk
|
16
|
-
class_eval { define_method name, &blk }
|
17
|
-
end
|
18
|
-
end
|
data/lib/sinatra/dispatcher.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Sinatra
|
2
|
-
|
3
|
-
class Dispatcher
|
4
|
-
|
5
|
-
cattr_accessor :logger
|
6
|
-
|
7
|
-
def default_headers
|
8
|
-
{ 'Content-Type' => 'text/html' }
|
9
|
-
end
|
10
|
-
|
11
|
-
def call(env)
|
12
|
-
Loader.reload! if Options.environment == :development
|
13
|
-
|
14
|
-
@request = Rack::Request.new(env)
|
15
|
-
|
16
|
-
event = EventManager.determine_event(
|
17
|
-
@request.request_method.downcase.intern,
|
18
|
-
@request.path_info
|
19
|
-
)
|
20
|
-
|
21
|
-
result = event.attend(@request)
|
22
|
-
[result.status, default_headers.merge(result.headers), result.body]
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|