equipment 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,83 @@
1
+ require 'equipment'
2
+ require 'ext/patches'
3
+ require 'ext/forward'
4
+
5
+ module Ext
6
+
7
+ # Generic authentication module. Used by BasicAuth for example.
8
+ #
9
+ # == Dependencies
10
+ #
11
+ # * Equipment
12
+ # * Forward
13
+ # * Patches
14
+ #
15
+ # == TODO
16
+ #
17
+ # * More doc
18
+ module Security
19
+ extend Equipment
20
+ depends_on Patches
21
+ depends_on Forward
22
+
23
+ def self.equip(app)
24
+ super
25
+ app::Controllers::Unauthorized.send :include, app::C, app::Base, app::Models
26
+ app::Controllers::Unauthenticated.send :include, app::C, app::Base, app::Models
27
+ end
28
+
29
+ module Base
30
+ # Blank authentication, returns [nil, nil]
31
+ def authenticate; return nil, nil end
32
+
33
+ # Blank authorization, returns true
34
+ def authorize(user, pass, force=false); not force; end
35
+
36
+ # Checks the credencials when serving a page if `authorize` is defined
37
+ # in your app, Base or controller.
38
+ def service(*a)
39
+ puts "Walking trough service"
40
+ user, pass = authenticate
41
+ if authorize(user, pass)
42
+ super
43
+ elsif not user
44
+ puts "Forwarding to Unauthenticated"
45
+ forward(app::Controllers::Unauthenticated)
46
+ else
47
+ puts "Forwarding to Unauthorized"
48
+ forward(app::Controllers::Unauthorized, user)
49
+ end
50
+ self
51
+ end
52
+
53
+ end
54
+
55
+ module Controllers
56
+ # Called when an url is not authorized. You can override this is your app.
57
+ # Or create an `unauthorized` view. @bad_user is the user name.
58
+ class Unauthorized
59
+ def self.urls; [] end # do not serve urls directly.
60
+ def authenticate; return nil, nil end
61
+ def authorize(user, pass, force=false); true; end
62
+
63
+ def get(bad_user)
64
+ @bad_user = bad_user
65
+ render(:unauthorized) rescue "Unauthorized user : #@bad_user"
66
+ end
67
+ end
68
+
69
+ # Customize by creating an "unauthenticated" view
70
+ class Unauthenticated
71
+ def self.urls; [] end # do not serve urls directly.
72
+ def authenticate; return nil, nil end
73
+ def authorize(user, pass, force=false); true; end
74
+
75
+ def get
76
+ render(:unauthenticated) rescue 'Unauthenticated'
77
+ end
78
+ end
79
+
80
+ end
81
+ end
82
+ end
83
+
@@ -0,0 +1,84 @@
1
+ require 'equipment'
2
+ require 'ext/app_util'
3
+ require 'ext/forward'
4
+
5
+ begin
6
+ require 'mime/types' # requires that package
7
+ rescue LoadError
8
+ STDERR.puts '*** mime/types not available. You will have to specify mime \
9
+ types in Equipment::Sendfile.'
10
+ end
11
+
12
+ module Ext
13
+
14
+ # The Sendfile equipment allows you to send files to the browser. See
15
+ # Base#sendfile for more options.
16
+ #
17
+ # == Features
18
+ #
19
+ # * Autodetects if the `sendfile` extension is used in mongrel.
20
+ # * Provides mime-type resolution trough the 'mime/types' library.
21
+ # I encourage everybody to use that lib when dealing with mime types.
22
+ #
23
+ # == Dependencies
24
+ #
25
+ # * Equipment
26
+ # * AppUtil
27
+ # * Forward
28
+ # * `mime-types` package. Available as a gem.
29
+ # * `sendfile` package (optional). Available as a gem.
30
+ #
31
+ # == Example
32
+ #
33
+ # module Controllers
34
+ # class ShowPasswd
35
+ # def get
36
+ # # ...
37
+ # sendfile('/etc/password')
38
+ # end
39
+ # end
40
+ # end
41
+ #
42
+ # == TODO
43
+ #
44
+ # * Implement force_download
45
+ #
46
+ module Sendfile
47
+ extend Equipment
48
+ depends_on AppUtil
49
+ depends_on Forward
50
+
51
+ module Base
52
+ # Sends the file passed in the path to the browser.
53
+ #
54
+ # If content_type is given, it won't try to lookup in the MIME::Types
55
+ # library.
56
+ #
57
+ def sendfile(path, content_type = nil)
58
+ # Kind of check if the file exists using the rescue
59
+ file = File.open(path.to_s)
60
+
61
+ unless content_type
62
+ mime = MIME::Types.type_for(path).first || MIME::Types['text/plain'].first
63
+ @headers['Content-Type'] = mime.to_s
64
+ @headers['Content-Encoding'] = mime.encoding
65
+ else
66
+ @headers['Content-Type'] = content_type
67
+ end
68
+
69
+ # Sendfile is used ?
70
+ if $mongrel_has_sendfile
71
+ @headers['X-Sendfile'] = path
72
+ else
73
+ @body = file.read
74
+ end
75
+
76
+ rescue Errno::ENOENT, Errno::EISDIR # File not found
77
+ forward(app::Controllers::NotFound, @env.PATH_INFO)
78
+ ensure
79
+ file.close if file
80
+ end
81
+ end
82
+ end
83
+ end
84
+
@@ -0,0 +1,72 @@
1
+ require 'equipment'
2
+ require 'ext/patches'
3
+
4
+ module Ext
5
+ # A microcospical extension to decouple views from camping. Especially
6
+ # the ones based on templates.
7
+ #
8
+ # Adds a V class to your app on which all instance variables
9
+ # will be sent before rendering the view.
10
+ #
11
+ # == Dependencies
12
+ #
13
+ # * Equipment
14
+ # * Patches
15
+ #
16
+ # == TODO
17
+ #
18
+ # * Add support for layouts
19
+ module TemplateView
20
+ extend Equipment
21
+ depends_on Patches
22
+
23
+ def self.equip(app)
24
+ super
25
+ app.module_eval <<-SOME_VIEW
26
+ # Some kind of view data proxy for template-based renderers
27
+ class V
28
+ include Helpers
29
+ include Controllers
30
+ def bind(m,*a,&b); binding; end
31
+ def R(klass, *args); self / super; end
32
+ end
33
+ SOME_VIEW
34
+ end
35
+
36
+ module Base
37
+ # Takes template_root from ViewsClassMethods#template_root.
38
+ #
39
+ # You can change that by setting @template_root in your controller.
40
+ def template_root; @template_root || app::Views.template_root || '' end
41
+
42
+ # Same as #template_root
43
+ def use_cache; @use_cache || app::Views.use_cache; end
44
+ end
45
+
46
+ module ViewsClassMethods
47
+ # Sets the template_root. Usage is up to the template.
48
+ attr_accessor :template_root
49
+
50
+ # Set to true to add cache. Usage is up to the template.
51
+ attr_accessor :use_cache
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ class Object
58
+
59
+ # Sets all instance variables of an object to another
60
+ #
61
+ # if force is set to true, existing instance variables will be overwritten
62
+ def instance_variables_send(obj, force=false)
63
+ instance_variables.each do |v|
64
+ if force or not obj.instance_variables.include? v
65
+ obj.instance_variable_set(v, instance_variable_get(v))
66
+ end
67
+ end
68
+ obj
69
+ end
70
+ alias :ivs_send :instance_variables_send
71
+ end
72
+
@@ -0,0 +1,83 @@
1
+ require 'equipment'
2
+ require 'ext/app_util'
3
+
4
+ module Ext
5
+
6
+ # The UseHelper allows you to define in your view what js and css scripts
7
+ # you're using.
8
+ #
9
+ # That can then be later used in the layout.
10
+ #
11
+ # This is useful when you descript external styles.
12
+ #
13
+ # == Example
14
+ #
15
+ # def layout
16
+ # html do
17
+ # head do
18
+ # javascripts.each { |js| script :type=>'text/javascript', :src=>js }
19
+ # stylesheets.each { |css| link :type=>'text/css', :rel=>'stylesheet', :href=>css }
20
+ # end
21
+ # body { yield }
22
+ # end
23
+ # end
24
+ #
25
+ # def someview
26
+ # use 'prototype.js', 'layout.css'
27
+ # # ...
28
+ # end
29
+ #
30
+ # == Dependency
31
+ #
32
+ # * Equipment
33
+ # * AppUtil
34
+ #
35
+ # == TODO
36
+ #
37
+ # * More doc
38
+ #
39
+ # == Possible issues
40
+ #
41
+ # It is possible that the @__flash instance variable is not transmitted well
42
+ # between the controller and the view. This may cause unwanted behavior.
43
+ module UseHelper
44
+ extend Equipment
45
+ def self.equip(app)
46
+ app::Base.module_eval do
47
+ def intialize(r,env,m)
48
+ @__javascripts ||= [] # initializing those vars is needed
49
+ @__stylesheets ||= []
50
+ puts "UseHelper::Base : Initialized well" if $DBG
51
+ super
52
+ end
53
+ end
54
+ super
55
+ end
56
+
57
+ module Helpers
58
+ def javascripts
59
+ @__javascripts ||= []
60
+ end
61
+ def stylesheets
62
+ @__stylesheets ||= []
63
+ end
64
+ def use(*elems)
65
+ elems.each do |elem|
66
+ case elem
67
+ when /\.js$/
68
+ javascripts << elem unless javascripts.include? elem
69
+ when /\.css$/
70
+ stylesheets << elem unless stylesheets.include? elem
71
+ else
72
+ raise "Unknow type : #{elem}"
73
+ end
74
+ end
75
+ end
76
+ def use?(elem)
77
+ javascripts.include? elem or stylesheets.include? elem
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+
@@ -0,0 +1,47 @@
1
+ require 'equipment'
2
+
3
+ module Ext
4
+ # Uses the Builder::XmlMarkup to build xml views. Not working as I want yet.
5
+ #
6
+ # == Depencency
7
+ #
8
+ # * Equipment
9
+ #
10
+ # == TODO
11
+ #
12
+ # * Reimplement this
13
+ #
14
+ module XmlView
15
+ extend Equipment
16
+
17
+ module Base
18
+
19
+
20
+ =begin
21
+ def xml_view?(view)
22
+ return /application\/xml|\*\/\*/ =~ (env.HTTP_ACCEPT || '*/*')
23
+ end
24
+
25
+ def view(m, *a, &b)
26
+ if xml_view?(m)
27
+ return xml_view(m, *a, &b)
28
+ end
29
+ super
30
+ end
31
+ =end
32
+
33
+ # For controllers which pass back XML directly, this method allows quick assignment
34
+ # of the status code and takes care of generating the XML headers. Takes a block
35
+ # which receives the Builder::XmlMarkup object.
36
+ def xml_view(m, *a, &b)
37
+ xml = Builder::XmlMarkup.new
38
+ xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
39
+ yield xml
40
+ @headers['Content-Type'] = 'application/xml'
41
+ @headers['Encoding'] = 'UTF-8'
42
+ xml.target!
43
+ end
44
+ end
45
+ end
46
+ end
47
+
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: equipment
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-07-06 00:00:00 +02:00
8
+ summary: Camping equipments
9
+ require_paths:
10
+ - lib
11
+ email: zimba.tm@gmail.com
12
+ homepage: http://equipment.rubyforge.org
13
+ rubyforge_project: equipment
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Jonas Pfenniger
30
+ files:
31
+ - lib
32
+ - .
33
+ - examples
34
+ - lib/ext
35
+ - lib/ext/patches.rb
36
+ - examples/erubytest.rb
37
+ - examples/index.erb
38
+ - lib/ext/eruby_view.rb
39
+ - lib/ext/template_view.rb
40
+ - lib/ext/og_session.rb
41
+ - examples/sendfiletest.rb
42
+ - lib/ext/basic_auth.rb
43
+ - lib/ext/xml_view.rb
44
+ - README
45
+ - lib/ext/use_helper.rb
46
+ - lib/equipment.rb
47
+ - lib/ext/js_helpers.rb
48
+ - lib/ext/controls.rb
49
+ - lib/ext/ressource.rb
50
+ - examples/basicauthtest.rb
51
+ - examples/patchestest.rb
52
+ - examples/flashtest.rb
53
+ - lib/ext/app_util.rb
54
+ - lib/ext/forward.rb
55
+ - lib/ext/security.rb
56
+ - lib/ext/sendfile.rb
57
+ - examples/ogtest.rb
58
+ - examples/mounttest.rb
59
+ - lib/ext/forms.rb
60
+ - lib/ext/og_scaffold.rb
61
+ - lib/ext/mount.rb
62
+ - lib/ext/og.rb
63
+ - ProjectInfo
64
+ - lib/ext/flash.rb
65
+ test_files: []
66
+
67
+ rdoc_options: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ requirements: []
76
+
77
+ dependencies: []
78
+