Capcode 0.9.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.rdoc +3 -0
  2. data/examples/rest.log +15 -0
  3. data/examples/static.rb +12 -0
  4. data/examples/static/static-index.html +1 -0
  5. data/lib/capcode.rb +14 -344
  6. data/lib/capcode/filters.rb +1 -0
  7. data/lib/capcode/helpers.rb +246 -0
  8. data/lib/capcode/http_error.rb +49 -0
  9. data/lib/capcode/static_files.rb +68 -0
  10. data/lib/capcode/version.rb +1 -1
  11. metadata +46 -61
  12. data/doc/rdoc/classes/Capcode.html +0 -1076
  13. data/doc/rdoc/classes/Capcode/Base.html +0 -136
  14. data/doc/rdoc/classes/Capcode/Configuration.html +0 -290
  15. data/doc/rdoc/classes/Capcode/HTTPError.html +0 -148
  16. data/doc/rdoc/classes/Capcode/Helpers.html +0 -674
  17. data/doc/rdoc/classes/Capcode/Helpers/Authorization.html +0 -219
  18. data/doc/rdoc/classes/Capcode/Resource.html +0 -111
  19. data/doc/rdoc/classes/Capcode/StaticFiles.html +0 -199
  20. data/doc/rdoc/classes/Capcode/Views.html +0 -112
  21. data/doc/rdoc/created.rid +0 -1
  22. data/doc/rdoc/files/AUTHORS.html +0 -107
  23. data/doc/rdoc/files/COPYING.html +0 -531
  24. data/doc/rdoc/files/README_rdoc.html +0 -887
  25. data/doc/rdoc/files/lib/capcode/base/db_rb.html +0 -101
  26. data/doc/rdoc/files/lib/capcode/configuration_rb.html +0 -101
  27. data/doc/rdoc/files/lib/capcode/filters_rb.html +0 -101
  28. data/doc/rdoc/files/lib/capcode/helpers/auth_rb.html +0 -101
  29. data/doc/rdoc/files/lib/capcode/render/text_rb.html +0 -101
  30. data/doc/rdoc/files/lib/capcode_rb.html +0 -144
  31. data/doc/rdoc/fr_class_index.html +0 -35
  32. data/doc/rdoc/fr_file_index.html +0 -35
  33. data/doc/rdoc/fr_method_index.html +0 -56
  34. data/doc/rdoc/index.html +0 -24
  35. data/doc/rdoc/rdoc-style.css +0 -208
  36. data/lib/capcode.rbSAVE +0 -881
@@ -0,0 +1,246 @@
1
+ module Capcode
2
+ # Helpers contains methods available in your controllers
3
+ module Helpers
4
+ def self.args
5
+ @args ||= nil
6
+ end
7
+ def self.args=(x)
8
+ @args = x
9
+ end
10
+
11
+ # Render a view
12
+ #
13
+ # render's parameter can be a Hash or a string. Passing a string is equivalent to do
14
+ # render( :text => string )
15
+ #
16
+ # If you want to use a specific renderer, use one of this options :
17
+ #
18
+ # * :markaby => :my_func : :my_func must be defined in Capcode::Views
19
+ # * :erb => :my_erb_file : this suppose that's my_erb_file.rhtml exist in erb_path
20
+ # * :haml => :my_haml_file : this suppose that's my_haml_file.haml exist in haml_path
21
+ # * :sass => :my_sass_file : this suppose that's my_sass_file.sass exist in sass_path
22
+ # * :text => "my text"
23
+ # * :json => MyObject : this suppose that's MyObject respond to .to_json
24
+ # * :static => "my_file.xxx" : this suppose that's my_file.xxx exist in the static directory
25
+ # * :xml => :my_func : :my_func must be defined in Capcode::Views
26
+ # * :webdav => /path/to/root
27
+ #
28
+ # Or you can use a "HTTP code" renderer :
29
+ #
30
+ # render 200 => "Ok", :server => "Capcode #{Capcode::CAPCOD_VERION}", ...
31
+ #
32
+ # If you want to use a specific layout, you can specify it with option
33
+ # :layout
34
+ #
35
+ # If you want to change the Content-Type, you can specify it with option
36
+ # :content_type
37
+ # Note that this will not work with the JSON renderer
38
+ #
39
+ # If you use the WebDav renderer, you can use the option
40
+ # :resource_class (see http://github.com/georgi/rack_dav for more informations)
41
+ def render( hash )
42
+ if hash.class == Hash
43
+ render_type = nil
44
+ possible_code_renderer = nil
45
+
46
+ hash.keys.each do |key|
47
+ begin
48
+ gem "capcode-render-#{key.to_s}"
49
+ require "capcode/render/#{key.to_s}"
50
+ rescue Gem::LoadError
51
+ nil
52
+ rescue LoadError
53
+ raise Capcode::RenderError, "Hum... The #{key} renderer is malformated! Please try to install a new version or use an other renderer!", caller
54
+ end
55
+
56
+ if self.respond_to?("render_#{key.to_s}")
57
+ unless render_type.nil?
58
+ raise Capcode::RenderError, "Can't use multiple renderer (`#{render_type}' and `#{key}') !", caller
59
+ end
60
+ render_type = key
61
+ end
62
+
63
+ if key.class == Fixnum
64
+ possible_code_renderer = key
65
+ end
66
+ end
67
+
68
+ if render_type.nil? and possible_code_renderer.nil?
69
+ raise Capcode::RenderError, "Renderer type not specified!", caller
70
+ end
71
+
72
+ unless self.respond_to?("render_#{render_type.to_s}")
73
+ if possible_code_renderer.nil?
74
+ raise Capcode::RenderError, "#{render_type} renderer not present ! please require 'capcode/render/#{render_type}'", caller
75
+ else
76
+ code = possible_code_renderer
77
+ body = hash.delete(possible_code_renderer)
78
+ header = {}
79
+ hash.each do |k, v|
80
+ k = k.to_s.split(/_/).map{|e| e.capitalize}.join("-")
81
+ header[k] = v
82
+ end
83
+
84
+ [code, header, body]
85
+ end
86
+ else
87
+ render_name = hash.delete(render_type)
88
+ content_type = hash.delete(:content_type)
89
+ unless content_type.nil?
90
+ @response['Content-Type'] = content_type
91
+ end
92
+
93
+ begin
94
+ self.send( "render_#{render_type.to_s}", render_name, hash )
95
+ rescue => e
96
+ raise Capcode::RenderError, "Error rendering `#{render_type.to_s}' : #{e.message}"#, caller
97
+ end
98
+ end
99
+ else
100
+ render( :text => hash )
101
+ end
102
+ end
103
+
104
+ # Send a redirect response
105
+ #
106
+ # module Capcode
107
+ # class Hello < Route '/hello/(.*)'
108
+ # def get( you )
109
+ # if you.nil?
110
+ # redirect( WhoAreYou )
111
+ # else
112
+ # ...
113
+ # end
114
+ # end
115
+ # end
116
+ # end
117
+ #
118
+ # The first parameter can be a controller class name
119
+ #
120
+ # redirect( MyController )
121
+ #
122
+ # it can be a string path
123
+ #
124
+ # redirect( "/path/to/my/resource" )
125
+ #
126
+ # it can be an http status code (by default <tt>redirect</tt> use the http status code 302)
127
+ #
128
+ # redirect( 304, MyController )
129
+ #
130
+ # For more informations about HTTP status, see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection
131
+ def redirect( klass, *a )
132
+ httpCode = 302
133
+
134
+ if( klass.class == Fixnum )
135
+ httpCode = klass
136
+ klass = a.shift
137
+ end
138
+
139
+ [httpCode, {'Location' => URL(klass, *a), 'Content-Type' => 'text/plain'}, '']
140
+ end
141
+
142
+ # Builds an URL route to a controller or a path
143
+ #
144
+ # if you declare the controller Hello :
145
+ #
146
+ # module Capcode
147
+ # class Hello < Route '/hello/(.*)'
148
+ # ...
149
+ # end
150
+ # end
151
+ #
152
+ # then
153
+ #
154
+ # URL( Capcode::Hello, "you" ) # => /hello/you
155
+ def URL( klass, *a )
156
+ path = nil
157
+ result = {}
158
+
159
+ a = a.delete_if{ |x| x.nil? }
160
+
161
+ if klass.class == Class
162
+ klass.__urls__[0].each do |cpath, data|
163
+ args = a.clone
164
+
165
+ n = Regexp.new( data[:regexp] ).number_of_captures
166
+ equart = (a.size - n).abs
167
+
168
+ rtable = data[:regexp].dup.gsub( /\\\(/, "" ).gsub( /\\\)/, "" ).split( /\([^\)]*\)/ )
169
+ rtable.each do |r|
170
+ if r == ""
171
+ cpath = cpath + "/#{args.shift}"
172
+ else
173
+ cpath = cpath + "/#{r}"
174
+ end
175
+ end
176
+
177
+ cpath = (cpath + "/" + args.join( "/" )) if args.size > 0
178
+ cpath = cpath.gsub( /(\/){2,}/, "/" )
179
+ result[equart] = cpath
180
+ end
181
+
182
+ path = result[result.keys.min]
183
+ else
184
+ path = klass
185
+ end
186
+
187
+ (ENV['RACK_BASE_URI']||'')+path
188
+ end
189
+
190
+ # Calling content_for stores a block of markup in an identifier.
191
+ #
192
+ # module Capcode
193
+ # class ContentFor < Route '/'
194
+ # def get
195
+ # render( :markaby => :page, :layout => :layout )
196
+ # end
197
+ # end
198
+ # end
199
+ #
200
+ # module Capcode::Views
201
+ # def layout
202
+ # html do
203
+ # head do
204
+ # yield :header
205
+ # end
206
+ # body do
207
+ # yield :content
208
+ # end
209
+ # end
210
+ # end
211
+ #
212
+ # def page
213
+ # content_for :header do
214
+ # title "This is the title!"
215
+ # end
216
+ #
217
+ # content_for :content do
218
+ # p "this is the content!"
219
+ # end
220
+ # end
221
+ # end
222
+ def content_for( x )
223
+ if Capcode::Helpers.args.map{|_| _.to_s }.include?(x.to_s)
224
+ yield
225
+ end
226
+ end
227
+
228
+ # Return information about the static directory
229
+ #
230
+ # * <tt>static[:uri]</tt> give the static URI
231
+ # * <tt>static[:path]</tt> give the path to the static directory on the server
232
+ def static
233
+ {
234
+ :uri => Capcode.static,
235
+ :path => File.expand_path( File.join(Capcode::Configuration.get(:root), Capcode::Configuration.get(:static) ) )
236
+ }
237
+ end
238
+
239
+ # Use the Rack logger
240
+ #
241
+ # log.write( "This is a log !" )
242
+ def log
243
+ Capcode.logger || env['rack.errors']
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,49 @@
1
+ module Capcode
2
+ # HTTPError help you to create your own 404, 500 and/or 501 response
3
+ #
4
+ # To create a custom 404 reponse, create a fonction HTTPError.r404 in
5
+ # your application :
6
+ #
7
+ # module Capcode
8
+ # class HTTPError
9
+ # def r404(f)
10
+ # "#{f} not found :("
11
+ # end
12
+ # end
13
+ # end
14
+ #
15
+ # the rXXX method can also receive a second optional parameter corresponding
16
+ # of the header's Hash :
17
+ #
18
+ # module Capcode
19
+ # class HTTPError
20
+ # def r404(f, h)
21
+ # h['Content-Type'] = 'text/plain'
22
+ # "You are here ---> X (#{f} point)"
23
+ # end
24
+ # end
25
+ # end
26
+ #
27
+ # Do the same (r500, r501, r403) to customize 500, 501, 403 errors
28
+ class HTTPError
29
+ def initialize(app) #:nodoc:
30
+ @app = app
31
+ end
32
+
33
+ def call(env) #:nodoc:
34
+ status, headers, body = @app.call(env)
35
+ if self.methods.include? "r#{status}"
36
+ headers.delete('Content-Type') if headers.keys.include?('Content-Type')
37
+ body = begin
38
+ self.send( "r#{status}", env['REQUEST_PATH'], headers )
39
+ rescue
40
+ self.send( "r#{status}", env['REQUEST_PATH'] )
41
+ end
42
+ headers['Content-Length'] = body.length.to_s
43
+ headers['Content-Type'] = "text/html" unless headers.keys.include?('Content-Type')
44
+ end
45
+
46
+ [status, headers, body]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,68 @@
1
+ require 'capcode/helpers'
2
+
3
+ module Capcode
4
+ # Static file loader
5
+ #
6
+ # You can add declare a filter (with before_filter) using :StaticFiles
7
+ #
8
+ # Use :
9
+ # set :static, "path/to/static"
10
+ class StaticFiles
11
+ def initialize(app)
12
+ @app = app
13
+ end
14
+
15
+ def session
16
+ env["rack.session"]
17
+ end
18
+
19
+ def env
20
+ @env
21
+ end
22
+
23
+ def log
24
+ env["rack.errors"]
25
+ end
26
+
27
+ def request
28
+ @request
29
+ end
30
+
31
+ def response
32
+ @response
33
+ end
34
+
35
+ def call(env)
36
+ @env = env
37
+ @response = Rack::Response.new
38
+ @request = Rack::Request.new(@env)
39
+
40
+ static = ::File.expand_path( ::File.join(Capcode::Configuration.get(:root), Capcode::Configuration.get(:static) ) )
41
+ file = ::File.join(static, request.path.gsub(/^[\/]?#{Capcode::Configuration.get(:static)}/, "").split("/") )
42
+ file = ::File.join(file, "index.html" ) if ::File.directory?(file)
43
+
44
+ if ::File.exist?(file)
45
+ filter_output = Capcode::Filter.execute( self )
46
+ if filter_output.nil?
47
+ body = [::File.read(file)]
48
+ header = {
49
+ "Last-Modified" => ::File.mtime(file).httpdate,
50
+ "Content-Type" => ::Rack::Mime.mime_type(::File.extname(file), 'text/plain'),
51
+ "Content-Length" => body.first.size.to_s,
52
+ "Cache-Control" => "no-cache, must-revalidate"
53
+ }
54
+
55
+ return [200, header, body]
56
+ else
57
+ return filter_output
58
+ end
59
+ else
60
+ return @app.call(env)
61
+ end
62
+
63
+ return @app.call(env)
64
+ end
65
+
66
+ include Capcode::Helpers
67
+ end
68
+ end
@@ -1,3 +1,3 @@
1
1
  module Capcode
2
- CAPCOD_VERION="0.9.9"
2
+ CAPCOD_VERION="1.0.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Capcode
3
3
  version: !ruby/object:Gem::Version
4
- hash: 41
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 9
9
- - 9
10
- version: 0.9.9
9
+ - 0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Gr\xC3\xA9goire Lejeune"
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-17 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-05-31 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: rack
@@ -57,6 +56,9 @@ extra_rdoc_files:
57
56
  - AUTHORS
58
57
  - COPYING
59
58
  - lib/capcode.rb
59
+ - lib/capcode/helpers.rb
60
+ - lib/capcode/http_error.rb
61
+ - lib/capcode/static_files.rb
60
62
  - lib/capcode/configuration.rb
61
63
  - lib/capcode/base/db.rb
62
64
  - lib/capcode/render/text.rb
@@ -65,40 +67,18 @@ files:
65
67
  - README.rdoc
66
68
  - AUTHORS
67
69
  - setup.rb
68
- - doc/rdoc/classes/Capcode/Base.html
69
- - doc/rdoc/classes/Capcode/Configuration.html
70
- - doc/rdoc/classes/Capcode/Helpers/Authorization.html
71
- - doc/rdoc/classes/Capcode/Helpers.html
72
- - doc/rdoc/classes/Capcode/HTTPError.html
73
- - doc/rdoc/classes/Capcode/Resource.html
74
- - doc/rdoc/classes/Capcode/StaticFiles.html
75
- - doc/rdoc/classes/Capcode/Views.html
76
- - doc/rdoc/classes/Capcode.html
77
- - doc/rdoc/created.rid
78
- - doc/rdoc/files/AUTHORS.html
79
- - doc/rdoc/files/COPYING.html
80
- - doc/rdoc/files/lib/capcode/base/db_rb.html
81
- - doc/rdoc/files/lib/capcode/configuration_rb.html
82
- - doc/rdoc/files/lib/capcode/filters_rb.html
83
- - doc/rdoc/files/lib/capcode/helpers/auth_rb.html
84
- - doc/rdoc/files/lib/capcode/render/text_rb.html
85
- - doc/rdoc/files/lib/capcode_rb.html
86
- - doc/rdoc/files/README_rdoc.html
87
- - doc/rdoc/fr_class_index.html
88
- - doc/rdoc/fr_file_index.html
89
- - doc/rdoc/fr_method_index.html
90
- - doc/rdoc/index.html
91
- - doc/rdoc/rdoc-style.css
92
70
  - lib/capcode/base/db.rb
93
71
  - lib/capcode/configuration.rb
94
72
  - lib/capcode/core_ext.rb
95
73
  - lib/capcode/ext/rack/urlmap.rb
96
74
  - lib/capcode/filters.rb
97
75
  - lib/capcode/helpers/auth.rb
76
+ - lib/capcode/helpers.rb
77
+ - lib/capcode/http_error.rb
98
78
  - lib/capcode/render/text.rb
79
+ - lib/capcode/static_files.rb
99
80
  - lib/capcode/version.rb
100
81
  - lib/capcode.rb
101
- - lib/capcode.rbSAVE
102
82
  - examples/auth-basic.rb
103
83
  - examples/auth-digest.rb
104
84
  - examples/erb/cf.rhtml
@@ -114,6 +94,7 @@ files:
114
94
  - examples/render-image.rb
115
95
  - examples/render-text.rb
116
96
  - examples/rest-run.rb
97
+ - examples/rest.log
117
98
  - examples/rest.rb
118
99
  - examples/rest.ru
119
100
  - examples/route.rb
@@ -130,38 +111,42 @@ files:
130
111
  - examples/static/static-index.html
131
112
  - examples/static.rb
132
113
  - examples/upload.rb
133
- has_rdoc: true
134
114
  homepage: http://algorithmique.net
135
115
  licenses: []
136
116
 
137
- post_install_message: "\n\
138
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\
139
- !! !!\n\
140
- !! 0.9.0 WAS A MAJOR ENHANCEMENT !!\n\
141
- !! ----------------------------------- !!\n\
142
- !! !!\n\
143
- !! YOU MUST UNINSTALL ALL PREVIOUS !!\n\
144
- !! VERSIONS !!! !!\n\
145
- !! !!\n\
146
- !! gem uninstall Capode --version '< 0.9.0' !!\n\
147
- !! !!\n\
148
- !! IF YOU DON'T DO IT, THIS ONE WILL NOT !!\n\
149
- !! WORK !!! !!\n\
150
- !! !!\n\
151
- !! Moreover : !!\n\
152
- !! !!\n\
153
- !! Renderers and database accessors have !!\n\
154
- !! been extracted and are now in the !!\n\
155
- !! plugins repository : !!\n\
156
- !! !!\n\
157
- !! http://github.com/glejeune/Capcode.more !!\n\
158
- !! !!\n\
159
- !! Each plugin is a gem that\xE2\x80\x99s can be !!\n\
160
- !! installed separately. !!\n\
161
- !! !!\n\
162
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\
163
- For more information about Capcode, see \n http://capcode.rubyforge.org\n\n\
164
- You can also read the Capcode book (fr) at\n http://algorithmique.net/capcode.html\n\n"
117
+ post_install_message: |+
118
+
119
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
120
+ !! !!
121
+ !! 1.0.0 HAS A MAJOR ENHANCEMENT !!
122
+ !! ----------------------------------- !!
123
+ !! !!
124
+ !! Statics files are now loaded with a new !!
125
+ !! class. So if you specify !!
126
+ !! !!
127
+ !! set :static, "static" !!
128
+ !! !!
129
+ !! All files in the "static" directory !!
130
+ !! will be loaded from the URI !!
131
+ !! !!
132
+ !! http://server:port/<page> !!
133
+ !! !!
134
+ !! Where <page> is any file in the static !!
135
+ !! directory. You no longer need to use !!
136
+ !! !!
137
+ !! http://server:port/static/<page> !!
138
+ !! !!
139
+ !! For more information, see example !!
140
+ !! static.rb !!
141
+ !! !!
142
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
143
+
144
+ For more information about Capcode, see
145
+ http://capcode.rubyforge.org
146
+
147
+ You can also read the Capcode book (fr) at
148
+ http://algorithmique.net/capcode.html
149
+
165
150
  rdoc_options:
166
151
  - --quiet
167
152
  - --title
@@ -197,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
182
  requirements: []
198
183
 
199
184
  rubyforge_project: capcode
200
- rubygems_version: 1.4.2
185
+ rubygems_version: 1.8.4
201
186
  signing_key:
202
187
  specification_version: 3
203
188
  summary: Capcode is a web microframework