Neurogami-restafarian 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ pkg
@@ -0,0 +1,4 @@
1
+ == 1.0.0 / 2008-12-20
2
+
3
+ * 1 major enhancement
4
+ * Birthday!
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/restafarian
6
+ lib/restafarian.rb
7
+ lib/restafarian/restafarian.rb
8
+ spec/restafarian_spec.rb
9
+ spec/spec_helper.rb
10
+ test/test_restafarian.rb
@@ -0,0 +1,58 @@
1
+ Neurogami::Restafarian
2
+ =======================
3
+
4
+ by James Britt /Neurogami
5
+ www.neurogami.com
6
+
7
+
8
+ DESCRIPTION
9
+ -----------
10
+
11
+ A bit of Rack middleware to do REST routing
12
+
13
+ FEATURES/PROBLEMS
14
+ -----------------
15
+
16
+ * FIXME (list of features or problems)
17
+ * too many FIXME's !
18
+
19
+ SYNOPSIS
20
+ -----------
21
+
22
+ FIXME (code sample of usage)
23
+
24
+ REQUIREMENTS
25
+ ------------
26
+
27
+ * FIXME (list of requirements)
28
+
29
+ INSTALL
30
+ ---------
31
+
32
+ * FIXME (sudo gem install, anything else)
33
+
34
+ LICENSE
35
+ -------
36
+
37
+ (The MIT License)
38
+
39
+ Copyright (c) 2008 James Britt /Neurogami LLC
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining
42
+ a copy of this software and associated documentation files (the
43
+ 'Software'), to deal in the Software without restriction, including
44
+ without limitation the rights to use, copy, modify, merge, publish,
45
+ distribute, sublicense, and/or sell copies of the Software, and to
46
+ permit persons to whom the Software is furnished to do so, subject to
47
+ the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be
50
+ included in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
53
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
56
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
57
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
58
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
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
+ load 'tasks/setup.rb'
10
+ end
11
+
12
+ ensure_in_path 'lib'
13
+ require 'restafarian'
14
+
15
+ task :default => 'spec:run'
16
+
17
+ PROJ.name = 'restafarian'
18
+ PROJ.authors = 'James Britt'
19
+ PROJ.email = 'james@neurogami.com'
20
+ PROJ.url = 'http:// neurogami.com/code/restafarian'
21
+ PROJ.version = Restafarian::VERSION
22
+ # PROJ.rubyforge.name = 'restafarian'
23
+
24
+ PROJ.spec.opts << '--color'
25
+
26
+ # EOF
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(
4
+ File.join(File.dirname(__FILE__), %w[.. lib restafarian]))
5
+
6
+ # Put your code here
7
+
8
+ # EOF
@@ -0,0 +1,49 @@
1
+ module Restafarian
2
+
3
+ # :stopdoc:
4
+ VERSION = '0.1.0'
5
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
6
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
7
+ # :startdoc:
8
+
9
+ # Returns the version string for the library.
10
+ #
11
+ def self.version
12
+ VERSION
13
+ end
14
+
15
+ # Returns the library path for the module. If any arguments are given,
16
+ # they will be joined to the end of the libray path using
17
+ # <tt>File.join</tt>.
18
+ #
19
+ def self.libpath( *args )
20
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
21
+ end
22
+
23
+ # Returns the lpath for the module. If any arguments are given,
24
+ # they will be joined to the end of the path using
25
+ # <tt>File.join</tt>.
26
+ #
27
+ def self.path( *args )
28
+ args.empty? ? PATH : ::File.join(PATH, args.flatten)
29
+ end
30
+
31
+ # Utility method used to rquire all files ending in .rb that lie in the
32
+ # directory below this file that has the same name as the filename passed
33
+ # in. Optionally, a specific _directory_ name can be passed in such that
34
+ # the _filename_ does not have to be equivalent to the directory.
35
+ #
36
+ def self.require_all_libs_relative_to( fname, dir = nil )
37
+ (STDERR.puts( " \n :DEBUG\n #{__FILE__}:#{__LINE__} self.require_all_libs_relative_to #{fname}" ); STDERR.flush ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
38
+ dir ||= ::File.basename(fname, '.*')
39
+ search_me = ::File.expand_path( ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
40
+ warn "search_me: #{search_me}"
41
+ Dir.glob(search_me).sort.each {|rb|
42
+ warn rb
43
+ require rb}
44
+ end
45
+
46
+ end # module Restafarian
47
+
48
+ Restafarian.require_all_libs_relative_to(__FILE__)
49
+ # EOF
@@ -0,0 +1,157 @@
1
+ require 'pp'
2
+ module Rack
3
+
4
+ # We use this to memoize rack.input reading so we do not
5
+ # lose this in the actual app after reading the stream in
6
+ # our middleware
7
+ # SEEMS TO BE UNCALLED FOR SINCE WE CAN REWIND THE REAL THING
8
+ #class InputWrapper
9
+
10
+ # def initialize(input)
11
+ # @input = input
12
+ # @input_data = nil
13
+ # end
14
+
15
+ # ## * +gets+ must be called without arguments and return a string,
16
+ # ## or +nil+ on EOF.
17
+ # def gets(*args)
18
+ # return @input_data if @input_data
19
+ # @input_data = @input.gets
20
+ # @input_data
21
+ # end
22
+
23
+
24
+ # ## * +read+ must be called without or with one integer argument
25
+ # ## and return a string, or +nil+ on EOF.
26
+ # def read(*args)
27
+ # return @input_data if @input_data
28
+ # @input_data = @input.read(*args)
29
+ # @input_data
30
+ # end
31
+
32
+ # ## * +each+ must be called without arguments and only yield Strings.
33
+ # def each(*args)
34
+ # (STDERR.puts( " :DEBUG #{__FILE__}:#{__LINE__} each" ); STDERR.flush ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG'']"}}"))
35
+ # @input.each { |line|
36
+ # yield line
37
+ # }
38
+ # end
39
+
40
+
41
+ # def rewind *args
42
+ # # ???
43
+ # end
44
+
45
+
46
+
47
+ module Neurogami
48
+
49
+ class Restafarian
50
+
51
+ RESTA_METHODS = %w(index create delete update )
52
+
53
+ F = ::File
54
+
55
+ def to_rest_parts(path)
56
+ # We have some scenarios:
57
+ # '/'
58
+ # ''
59
+ # '/something'
60
+ # '/something/'
61
+ # '/foo/bar'
62
+ #
63
+ # We can strip any wrapping '/' chars so that
64
+ # we get
65
+ # ''
66
+ # 'foo'
67
+ # 'foo/bar'
68
+ #
69
+ path.sub!( /^\//, '')
70
+ path.sub!( /\/$/, '')
71
+ path.strip!
72
+
73
+ return {:base => '', :args => [] } if path.empty?
74
+
75
+ parts = path.split('/')
76
+ base = parts.shift
77
+ { :base => base, :args => parts }
78
+ end
79
+
80
+ def initialize app, path='./'
81
+ @app = app
82
+ @root = F.expand_path(path)
83
+ end
84
+
85
+
86
+ def env_input_to_hash env
87
+ input_data = env['rack.input'].read
88
+ return {} if input_data.to_s.strip.empty?
89
+ input_data = CGI.unescape(input_data.to_s.strip)
90
+ return {} unless input_data =~ /=/
91
+ pairs = input_data.split('&')
92
+ h = {}
93
+ pairs.each { |pair| k,v = pair.split('='); h[k] = v }
94
+ h
95
+ end
96
+
97
+ def call env
98
+ path = env["PATH_INFO"]
99
+ request_hash = env_input_to_hash(env)
100
+ env['rack.input'].rewind
101
+ warn "request_hash = #{request_hash.pretty_inspect }"
102
+
103
+
104
+ # Allow for clients to pass psuedo request type via, say, a form field
105
+ # and update the values passed to the end app
106
+
107
+ # Ramaze seems to depend on the request methof being POST or GET; having
108
+ # PUT seems to prevent code from getting the posted values.
109
+ original_request_method = env['REQUEST_METHOD'].to_s
110
+ STDERR.puts( ":DEBUG #{__FILE__}:#{__LINE__} request_hash = #{request_hash.inspect} " ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
111
+ env['REQUEST_METHOD'] = request_hash['X-REQUEST_METHOD'] if request_hash['X-REQUEST_METHOD']
112
+ path_segements = to_rest_parts(path)
113
+
114
+ STDERR.puts( "+++++ DEBUG #{__FILE__}:#{__LINE__} env['REQUEST_METHOD'].upcase = #{env['REQUEST_METHOD'].upcase}" ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
115
+
116
+ # Do not allow direct calls to REST mappings
117
+ return [403, {'Content-Type' => 'text/plain'}, '' ] if RESTA_METHODS.include?(path_segements[:base])
118
+ return [403, {'Content-Type' => 'text/plain'}, '' ] if path_segements[:args] && RESTA_METHODS.include?(path_segements[:args].first)
119
+
120
+ action = case env['REQUEST_METHOD'].upcase
121
+ when 'GET'
122
+ ''
123
+ when 'PUT'
124
+ 'update'
125
+ when 'DELETE'
126
+ 'delete'
127
+ when 'POST'
128
+ #warn (env.methods - Object.methods).sort.join("\n")
129
+ #rack_response = Rack::Response.new
130
+ #rack_request = Rack::Request.new(env)
131
+ #warn rack_request.class
132
+ #warn (rack_request.methods - Object.methods).sort.join("\n")
133
+ #warn "BODY: " + rack_request.body.read
134
+
135
+ 'create'
136
+ else
137
+ 'index' # Maybe not?
138
+ end
139
+
140
+ segments = [path_segements[:base], action, path_segements[:args] ].flatten
141
+ segments.delete_if{|a| a.strip.empty? }
142
+
143
+ new_path = '/' + segments.flatten.join('/')
144
+ env['PATH_INFO'] = new_path
145
+ env["REQUEST_PATH"] = new_path
146
+ env["REQUEST_URI"] = new_path + ( env['QUERY_STRING'].to_s.strip.empty? ? '' : '?' + env['QUERY_STRING'].to_s.strip )
147
+ env['REQUEST_METHOD'] = original_request_method
148
+ warn "Middleware Restafarian ..."
149
+
150
+ STDERR.puts( ":DEBUG #{__FILE__}:#{__LINE__} New env = #{env.pretty_inspect}" ) if ENV['JAMES_SCA_JDEV_MACHINE'] # JGBDEBUG
151
+
152
+
153
+ return @app.call(env)
154
+ end
155
+ end
156
+ end
157
+ end
Binary file
@@ -0,0 +1,7 @@
1
+
2
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
3
+
4
+ describe Restafarian do
5
+ end
6
+
7
+ # EOF
@@ -0,0 +1,16 @@
1
+
2
+ require File.expand_path(
3
+ File.join(File.dirname(__FILE__), %w[.. lib restafarian]))
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
File without changes
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Neurogami-restafarian
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Britt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-12 00:00:00 -07:00
13
+ default_executable: restafarian
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bones
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.5.0
24
+ version:
25
+ description: A bit of Rack middleware to do REST routing
26
+ email: james@neurogami.com
27
+ executables:
28
+ - restafarian
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.md
35
+ - bin/restafarian
36
+ files:
37
+ - .gitignore
38
+ - History.txt
39
+ - Manifest.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/restafarian
43
+ - lib/restafarian.rb
44
+ - lib/restafarian/restafarian.rb
45
+ - restafarian.gemspec
46
+ - spec/restafarian_spec.rb
47
+ - spec/spec_helper.rb
48
+ - test/test_restafarian.rb
49
+ has_rdoc: false
50
+ homepage: http://neurogami.com/code/restafarian
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.txt
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: !binary |
72
+ AA==
73
+
74
+ rubygems_version: 1.2.0
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A bit of Rack middleware to do REST routing
78
+ test_files:
79
+ - test/test_restafarian.rb