swerling-synapse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ pkg/
2
+ doc/
3
+ ext/Makefile
4
+ ext/mkmf.log
5
+ ext/*.o
6
+ ext/*.so
@@ -0,0 +1,3 @@
1
+ == 0.0.1 / 2009-07-18
2
+
3
+ * initial release
@@ -0,0 +1,126 @@
1
+ synapse
2
+ by Steven Swerling
3
+ http://tab-a.slot-z.net
4
+
5
+ == DESCRIPTION:
6
+
7
+ Synapse is an application framework that does almost nothing.
8
+ Basically this is just a tiny wrapper for the Rack::Router
9
+ (see http://github.com/carllerche/rack-router)
10
+
11
+ very alpha-ish stuff here. Seems to work though.
12
+
13
+ == SYNOPSIS:
14
+
15
+ Here is an example Synapse application (foo_app.rb):
16
+
17
+ require 'synapse'
18
+
19
+ class FooApp < Synapse::App
20
+
21
+ def router
22
+ @router ||= Rack::Router.new(nil, {}) do |r|
23
+ r.map "/yip/", :get, :to => self, :with => { :action => "yip" }
24
+ r.map "/yap/:yap_variable", :get, :to => self, :with => { :action => "yapfoo" }
25
+ r.map "/yap/", :get, :to => self, :with => { :action => "yap" }
26
+ r.map "/:anything", :get, :to => self, :with => { :action => "no_route" }
27
+ r.map "/", :get, :to => self, :with => { :action => "home" }
28
+ end
29
+ end
30
+
31
+ def yip; "yip"; end
32
+ def yap; "yap"; end
33
+ def home; "home"; end
34
+
35
+ def no_route
36
+ # Note: 'self.env' is the rack env
37
+ self.response[:body] = "route not found for: '#{self.env['REQUEST_URI']}'"
38
+ self.response[:status_code] = 404
39
+ end
40
+
41
+ def yapfoo
42
+ "yapfoo, #{self.params[:yap_variable]}"
43
+ end
44
+
45
+ end
46
+
47
+ And here is an example rack config, foo_app.ru:
48
+
49
+ require 'foo_app'
50
+ run FooApp.new.as_rack_app
51
+
52
+ Run FooApp w/ rackup or shotgun:
53
+
54
+ rackup --server=thin foo.ru -p 3000
55
+
56
+ or
57
+
58
+ shotgun --server=thin foo.ru -p 3000
59
+
60
+ == FEATURES
61
+
62
+ When a Synapse application handles a rack request, it
63
+
64
+ 1. Duplicates self (so it's thread safe)
65
+ 2. Sets @response, @params, @env (the rack env)
66
+ 3. Calls the action that Rack::Router route that matched. If the action returns a String, that is used for the @response[:body]
67
+
68
+ The @response is a hash used to return rack status code, headers hash, and body. Actions may do what they please with
69
+ the response. Default response:
70
+
71
+ @response = {
72
+ :status_code => 200,
73
+ :headers => {'Content-Type' => 'text/html'},
74
+ :body => nil
75
+ }
76
+
77
+
78
+ Actions are expected to side-effect the :status_code, :headers, and :body. As a convenience, if an action returns a
79
+ string, it is assumed that that string is the :body. An exception is thrown if the :body is not set to something.
80
+
81
+ That's it. Really not much to see here. Just gives you a thread-safe rack-based web framework that consists of
82
+ nothing but a router.
83
+
84
+ == PROBLEMS
85
+
86
+ None known.
87
+
88
+ == REQUIREMENTS:
89
+
90
+ * rack
91
+ * Rack::Router (the rack-router gem), see http://github.com/carllerche/rack-router
92
+
93
+ == INSTALL:
94
+
95
+ * first install rack and rack-router
96
+ * gem install synapse
97
+
98
+ == Acknowledgements:
99
+
100
+ rack and rack-router were used, obviously.
101
+ 'bones' used for gem generation.
102
+
103
+ == LICENSE:
104
+
105
+ (The MIT License)
106
+
107
+ Copyright (c) 2009 Steven Swerling
108
+
109
+ Permission is hereby granted, free of charge, to any person obtaining
110
+ a copy of this software and associated documentation files (the
111
+ 'Software'), to deal in the Software without restriction, including
112
+ without limitation the rights to use, copy, modify, merge, publish,
113
+ distribute, sublicense, and/or sell copies of the Software, and to
114
+ permit persons to whom the Software is furnished to do so, subject to
115
+ the following conditions:
116
+
117
+ The above copyright notice and this permission notice shall be
118
+ included in all copies or substantial portions of the Software.
119
+
120
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
121
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
122
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
123
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
124
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
125
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
126
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,126 @@
1
+ synapse
2
+ by Steven Swerling
3
+ http://tab-a.slot-z.net
4
+
5
+ == DESCRIPTION:
6
+
7
+ Synapse is an application framework that does almost nothing.
8
+ Basically this is just a tiny wrapper for the Rack::Router
9
+ (see http://github.com/carllerche/rack-router)
10
+
11
+ very alpha-ish stuff here. Seems to work though.
12
+
13
+ == SYNOPSIS:
14
+
15
+ Here is an example Synapse application (foo_app.rb):
16
+
17
+ require 'synapse'
18
+
19
+ class FooApp < Synapse::App
20
+
21
+ def router
22
+ @router ||= Rack::Router.new(nil, {}) do |r|
23
+ r.map "/yip/", :get, :to => self, :with => { :action => "yip" }
24
+ r.map "/yap/:yap_variable", :get, :to => self, :with => { :action => "yapfoo" }
25
+ r.map "/yap/", :get, :to => self, :with => { :action => "yap" }
26
+ r.map "/:anything", :get, :to => self, :with => { :action => "no_route" }
27
+ r.map "/", :get, :to => self, :with => { :action => "home" }
28
+ end
29
+ end
30
+
31
+ def yip; "yip"; end
32
+ def yap; "yap"; end
33
+ def home; "home"; end
34
+
35
+ def no_route
36
+ # Note: 'self.env' is the rack env
37
+ self.response[:body] = "route not found for: '#{self.env['REQUEST_URI']}'"
38
+ self.response[:status_code] = 404
39
+ end
40
+
41
+ def yapfoo
42
+ "yapfoo, #{self.params[:yap_variable]}"
43
+ end
44
+
45
+ end
46
+
47
+ And here is an example rack config, foo_app.ru:
48
+
49
+ require 'foo_app'
50
+ run FooApp.new.as_rack_app
51
+
52
+ Run FooApp w/ rackup or shotgun:
53
+
54
+ rackup --server=thin foo.ru -p 3000
55
+
56
+ or
57
+
58
+ shotgun --server=thin foo.ru -p 3000
59
+
60
+ == FEATURES
61
+
62
+ When a Synapse application handles a rack request, it
63
+
64
+ 1. Duplicates self (so it's thread safe)
65
+ 2. Sets @response, @params, @env (the rack env)
66
+ 3. Calls the action that Rack::Router route that matched. If the action returns a String, that is used for the @response[:body]
67
+
68
+ The @response is a hash used to return rack status code, headers hash, and body. Actions may do what they please with
69
+ the response. Default response:
70
+
71
+ @response = {
72
+ :status_code => 200,
73
+ :headers => {'Content-Type' => 'text/html'},
74
+ :body => nil
75
+ }
76
+
77
+
78
+ Actions are expected to side-effect the :status_code, :headers, and :body. As a convenience, if an action returns a
79
+ string, it is assumed that that string is the :body. An exception is thrown if the :body is not set to something.
80
+
81
+ That's it. Really not much to see here. Just gives you a thread-safe rack-based web framework that consists of
82
+ nothing but a router.
83
+
84
+ == PROBLEMS
85
+
86
+ None known.
87
+
88
+ == REQUIREMENTS:
89
+
90
+ * rack
91
+ * Rack::Router (the rack-router gem), see http://github.com/carllerche/rack-router
92
+
93
+ == INSTALL:
94
+
95
+ * first install rack and rack-router
96
+ * gem install synapse
97
+
98
+ == Acknowledgements:
99
+
100
+ rack and rack-router were used, obviously.
101
+ 'bones' used for gem generation.
102
+
103
+ == LICENSE:
104
+
105
+ (The MIT License)
106
+
107
+ Copyright (c) 2009 Steven Swerling
108
+
109
+ Permission is hereby granted, free of charge, to any person obtaining
110
+ a copy of this software and associated documentation files (the
111
+ 'Software'), to deal in the Software without restriction, including
112
+ without limitation the rights to use, copy, modify, merge, publish,
113
+ distribute, sublicense, and/or sell copies of the Software, and to
114
+ permit persons to whom the Software is furnished to do so, subject to
115
+ the following conditions:
116
+
117
+ The above copyright notice and this permission notice shall be
118
+ included in all copies or substantial portions of the Software.
119
+
120
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
121
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
122
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
123
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
124
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
125
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
126
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # Look in the tasks/setup.rb file for the various options that can be
2
+ # configured in this Rakefile. The .rake files in the tasks directory
3
+ # are where the options are used.
4
+
5
+ begin
6
+ require 'bones'
7
+ Bones.setup
8
+ rescue LoadError
9
+ begin
10
+ load 'tasks/setup.rb'
11
+ rescue LoadError
12
+ raise RuntimeError, '### please install the "bones" gem ###'
13
+ end
14
+ end
15
+
16
+ ensure_in_path 'lib'
17
+ require 'synapse_info'
18
+
19
+ task :default => 'spec:run'
20
+
21
+ PROJ.name = 'synapse'
22
+ PROJ.authors = 'Steven Swerling'
23
+ PROJ.email = 'sswerling@yahoo.com'
24
+ PROJ.url = 'http://tab-a.slot-z.net'
25
+ PROJ.version = Synapse::VERSION
26
+ PROJ.rubyforge.name = 'synapse'
27
+ PROJ.gem.dependencies = ['rack', 'rack-router']
28
+ PROJ.rdoc.opts = ["--inline-source"]
29
+ PROJ.rdoc.exclude = ["^tasks/setup\.rb$", "lib/synapse_info.rb"]
30
+
31
+ PROJ.spec.opts << '--color'
32
+
33
+ task :default => 'spec:run'
34
+ namespace :my do
35
+ namespace :gem do
36
+ task :package => [:clobber] do
37
+ this_dir = File.join(File.dirname(__FILE__))
38
+ sh "rm -rf #{File.join(this_dir, 'pkg')}"
39
+ sh "rm -rf #{File.join(this_dir, 'doc')}"
40
+ sh "cp #{File.join(this_dir, 'README.txt')} #{File.join(this_dir, 'README.rdoc')} "
41
+ Rake::Task['gem:package'].invoke
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__),'../lib/synapse.rb')
2
+
3
+ #
4
+ # Example synapse application
5
+ #
6
+ class FooApp < Synapse::App
7
+
8
+ def router
9
+ return @router ||= Rack::Router.new(nil, {}) do |r|
10
+ r.map "/yip/", :get, :to => self, :with => { :action => "yip" }
11
+ r.map "/yap/:yap_variable", :get, :to => self, :with => { :action => "yapfoo" }
12
+ r.map "/yap/", :get, :to => self, :with => { :action => "yap" }
13
+ r.map "/:anything", :get, :to => self, :with => { :action => "no_route" }
14
+ r.map "/", :get, :to => self, :with => { :action => "home" }
15
+ end
16
+ end
17
+
18
+ def yip; "yip"; end
19
+ def yap; "yap"; end
20
+ def home; "home"; end
21
+
22
+ def no_route
23
+ self.response[:body] = "route not found for: '#{self.env['REQUEST_URI']}'"
24
+ self.response[:status_code] = 404
25
+ end
26
+
27
+ def yapfoo
28
+ "yapfoo, #{self.params[:yap_variable]}"
29
+ end
30
+ end
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__),'foo_app.rb')
2
+ run FooApp.new.as_rack_app
@@ -0,0 +1,12 @@
1
+ # base ruby requires
2
+ require 'logger'
3
+
4
+ # gems dependencies
5
+ require 'rubygems'
6
+ require 'rack'
7
+ require 'rack/router'
8
+
9
+ # my files (require_all_libs_relative_to is a bones util method in synapse_info.rb)
10
+ require 'synapse_info'
11
+ Synapse.require_all_libs_relative_to(__FILE__)
12
+
@@ -0,0 +1,95 @@
1
+ module Synapse
2
+
3
+ #
4
+ # See the synopsis section of README.txt for usage.
5
+ #
6
+ # See the RackRouter project for the kinds of routes you can setup.
7
+ #
8
+ # Variables of note:
9
+ #
10
+ # @response
11
+ # a hash with keys :body, :headers, :status_code, the 3 items all rack handlers are expected to set.
12
+ # Body is a string, status code is an http status code integer, and headers is a hash that
13
+ # should conform to rack's contract.
14
+ #
15
+ # @env
16
+ # The rack env passed into this apps #call method
17
+ #
18
+ # @params
19
+ # The params that the matching Rack::Router route set.
20
+ #
21
+ class App
22
+ attr_accessor :response, :params, :env, :logger
23
+
24
+ # Options:
25
+ # :logger => where to log to.
26
+ # Note this is not the same thing as the rack access log (although you
27
+ # can pass that logger in if you want). Default: Logger.new(STDOUT)
28
+ def initialize(opts = {})
29
+ @logger = opts[:logger] || Logger.new(STDOUT)
30
+ end
31
+
32
+ # the router for this Synapse::App. Subclasses are _required_ to override this.
33
+ # (see README.txt for example usage)
34
+ def router
35
+ raise "#{self.class} must implement a 'router' method that returns a Rack::Router"
36
+ end
37
+
38
+ # Alias for #router
39
+ def as_rack_app; self.router; end # alias_method doesn't seem to work here (it doesnt call the subclass)
40
+
41
+ # The rack #call method
42
+ def call(env)
43
+ dup._call(env) # be thread-safe
44
+ end
45
+
46
+ #
47
+ # Misc Sugar
48
+ #
49
+
50
+ # The name of the action method, determined by the Route that Rack::Router bound to the incoming request
51
+ def action
52
+ self.params[:action]
53
+ end
54
+
55
+ # Overridable method that handles missing action that was defined by a route
56
+ def theres_no_action
57
+ self.response[:body] = "Action '#{self.action}' not found in '#{self.class}'"
58
+ self.response[:status_code] = 500
59
+ end
60
+
61
+ protected
62
+
63
+ # :stopdoc:
64
+
65
+ def _call(env)
66
+ start_time = Time.now.to_f
67
+
68
+ @env = env
69
+ @params = env['rack_router.params']
70
+ @response = {
71
+ :status_code => 200,
72
+ :headers => {'Content-Type' => 'text/html'},
73
+ :body => nil
74
+ }
75
+
76
+ action = self.action
77
+ if self.respond_to?(action)
78
+ body = self.send(self.action)
79
+ else
80
+ body = theres_no_action
81
+ end
82
+
83
+ response[:body] = body if body.is_a?(String)
84
+ raise "You have to set the response body" if response[:body].nil?
85
+
86
+ logger.debug("It took #{Time.now.to_f - start_time} sec for #{self.class} to handle request.")
87
+ [response[:status_code], response[:headers], response[:body]]
88
+ end
89
+
90
+ # :startdoc:
91
+ end
92
+ end
93
+
94
+
95
+
@@ -0,0 +1,51 @@
1
+ #
2
+ # See README.txt for usage.
3
+ #
4
+ module Synapse
5
+
6
+ # :stopdoc:
7
+
8
+ VERSION = '0.0.1'
9
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
10
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
11
+
12
+ # Returns the version string for the library.
13
+ #
14
+ def self.version
15
+ VERSION
16
+ end
17
+
18
+ # Returns the library path for the module. If any arguments are given,
19
+ # they will be joined to the end of the libray path using
20
+ # <tt>File.join</tt>.
21
+ #
22
+ def self.libpath( *args )
23
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
24
+ end
25
+
26
+ # Returns the lpath for the module. If any arguments are given,
27
+ # they will be joined to the end of the path using
28
+ # <tt>File.join</tt>.
29
+ #
30
+ def self.path( *args )
31
+ args.empty? ? PATH : ::File.join(PATH, args.flatten)
32
+ end
33
+
34
+ # Utility method used to require all files ending in .rb that lie in the
35
+ # directory below this file that has the same name as the filename passed
36
+ # in. Optionally, a specific _directory_ name can be passed in such that
37
+ # the _filename_ does not have to be equivalent to the directory.
38
+ #
39
+ def self.require_all_libs_relative_to( fname, dir = nil )
40
+ dir ||= ::File.basename(fname, '.*')
41
+ search_me = ::File.expand_path(
42
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
43
+
44
+ Dir.glob(search_me).sort.each {|rb| require rb}
45
+ end
46
+
47
+ # :startdoc:
48
+
49
+ end # module Synapse
50
+
51
+
@@ -0,0 +1,16 @@
1
+
2
+ require File.expand_path(
3
+ File.join(File.dirname(__FILE__), %w[.. lib synapse]))
4
+
5
+ Spec::Runner.configure do |config|
6
+ # == Mock Framework
7
+ #
8
+ # RSpec uses it's own mocking framework by default. If you prefer to
9
+ # use mocha, flexmock or RR, uncomment the appropriate line:
10
+ #
11
+ # config.mock_with :mocha
12
+ # config.mock_with :flexmock
13
+ # config.mock_with :rr
14
+ end
15
+
16
+ # EOF
@@ -0,0 +1,7 @@
1
+
2
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
3
+
4
+ describe Synapse do
5
+ end
6
+
7
+ # EOF
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'rack'
3
+
4
+ require 'foo_app.rb' # loads $router
5
+
6
+ #$router = FooApp.as_rack_app
7
+ inner_app = FooApp.new.as_rack_app
8
+
9
+ # create an env
10
+ env = 'development' # anoher switch
11
+ case env
12
+ when "development"
13
+ app = Rack::Builder.new {
14
+ use Rack::CommonLogger, $stderr
15
+ use Rack::ShowExceptions
16
+ use Rack::Lint
17
+ run inner_app
18
+ }
19
+ when "production"
20
+ app = Rack::Builder.new {
21
+ use Rack::CommonLogger, $stderr
22
+ run inner_app
23
+ }
24
+ else
25
+ raise "don't recognize '#{env}' run env"
26
+ end
27
+
28
+ # pick a server
29
+ begin
30
+ # (server = Rack::Handler::Mongrel)
31
+ #rescue LoadError => le
32
+ (server = Rack::Handler::Thin)
33
+ rescue LoadError => le
34
+ (server = Rack::Handler::WEBrick)
35
+ end
36
+
37
+ options = {:Port => 3000, :Host => "0.0.0.0", :AccessLog => []}
38
+ server.run app, options
File without changes
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swerling-synapse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Steven Swerling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack-router
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: bones
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.4.0
44
+ version:
45
+ description: Synapse is a web application framework that does practically nothing. Basically this is just a tiny wrapper for the Rack::Router (see http://github.com/carllerche/rack-router) very alpha-ish stuff here. Seems to work though.
46
+ email: sswerling@yahoo.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - History.txt
53
+ - README.rdoc
54
+ - README.txt
55
+ files:
56
+ - .gitignore
57
+ - History.txt
58
+ - README.rdoc
59
+ - README.txt
60
+ - Rakefile
61
+ - example/foo_app.rb
62
+ - example/foo_app.ru
63
+ - lib/synapse.rb
64
+ - lib/synapse/base.rb
65
+ - lib/synapse_info.rb
66
+ - spec/spec_helper.rb
67
+ - spec/synapse_spec.rb
68
+ - spstest.rb
69
+ - test/test_synapse.rb
70
+ has_rdoc: false
71
+ homepage: http://tab-a.slot-z.net
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --inline-source
75
+ - --main
76
+ - README.txt
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project: synapse
94
+ rubygems_version: 1.2.0
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Synapse is a web application framework that does practically nothing
98
+ test_files:
99
+ - test/test_synapse.rb