anupom-anobik 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.txt +14 -0
- data/Manifest +8 -0
- data/README.rdoc +79 -0
- data/Rakefile +15 -0
- data/anobik-server +5 -0
- data/anobik.gemspec +34 -0
- data/lib/anobik.rb +75 -0
- data/lib/rack/anobik.rb +159 -0
- data/test/spec_rack_anobik.rb +248 -0
- metadata +78 -0
data/CHANGELOG.txt
ADDED
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
= Anobik
|
2
|
+
|
3
|
+
* http://github.com/anupom/anobik
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Anobik is a simple rack middleware ruby microframework
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
Anobik is a Rack based microframework that provides an easy way to map URL patterns to classes.
|
12
|
+
URL patterns can be literal strings or regular expressions.
|
13
|
+
when URLs are processed:
|
14
|
+
* the beginning and end are anchored (^ $)
|
15
|
+
* an optional end slash is added (/?)
|
16
|
+
* the i option is added for case-insensitive searches
|
17
|
+
* respective class method is called depending on the request method (GET/POST etc.)
|
18
|
+
|
19
|
+
It does not provide any view templating or database handling mechanism.
|
20
|
+
you can ofcourse use any opensource view templating engine or database access layer with it.
|
21
|
+
Anobik is very similar to web.py, but it is not a direct clone of web.py.
|
22
|
+
It is ofcourse highly inspired by web.py and other similar lightweight microframeworks.
|
23
|
+
|
24
|
+
Anobik acts as a Rack middleware app and that means you can use it with a
|
25
|
+
number of webservers including Thin, LiteSpeed, Phusion Passangeretc.
|
26
|
+
And BTW, you can also use it with Rails, Sinatra or any other Rack based frameworks.
|
27
|
+
|
28
|
+
== SYNOPSIS:
|
29
|
+
|
30
|
+
* routes.rb
|
31
|
+
class Routes
|
32
|
+
URLS = {
|
33
|
+
"/" => "index",
|
34
|
+
"/index/(\\d+)" => "index",
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
* index.rb
|
39
|
+
class Index
|
40
|
+
def get(id=nil)
|
41
|
+
"Hello world from anobik! #{id}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
* url
|
46
|
+
GET /index/1
|
47
|
+
|
48
|
+
== REQUIREMENTS:
|
49
|
+
|
50
|
+
Ruby 1.9 and Rack are the pre-requisites
|
51
|
+
|
52
|
+
== INSTALL:
|
53
|
+
|
54
|
+
sudo gem install anobik
|
55
|
+
|
56
|
+
== LICENSE:
|
57
|
+
|
58
|
+
(The MIT License)
|
59
|
+
|
60
|
+
Copyright (c) 2009 syamantics.com
|
61
|
+
|
62
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
63
|
+
a copy of this software and associated documentation files (the
|
64
|
+
'Software'), to deal in the Software without restriction, including
|
65
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
66
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
67
|
+
permit persons to whom the Software is furnished to do so, subject to
|
68
|
+
the following conditions:
|
69
|
+
|
70
|
+
The above copyright notice and this permission notice shall be
|
71
|
+
included in all copies or substantial portions of the Software.
|
72
|
+
|
73
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
74
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
75
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
76
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
77
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
78
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
79
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('anobik', '0.0.2') do |p|
|
6
|
+
p.description = "Rack middleware Ruby micro-framework"
|
7
|
+
p.url = "http://github.com/anupom/anobik"
|
8
|
+
p.author = "Anupom Syam"
|
9
|
+
p.email = "anupom.syam@gmail.com"
|
10
|
+
p.ignore_pattern = ["nbproject", "tmp"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
p.runtime_dependencies = ['rack']
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each
|
data/anobik-server
ADDED
data/anobik.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{anobik}
|
5
|
+
s.version = "0.0.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Anupom Syam"]
|
9
|
+
s.date = %q{2009-05-24}
|
10
|
+
s.description = %q{Rack middleware Ruby micro-framework}
|
11
|
+
s.email = %q{anupom.syam@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG.txt", "lib/anobik.rb", "lib/rack/anobik.rb", "README.rdoc"]
|
13
|
+
s.files = ["test/spec_rack_anobik.rb", "Rakefile", "CHANGELOG.txt", "lib/anobik.rb", "lib/rack/anobik.rb", "anobik-server", "Manifest", "README.rdoc", "anobik.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/anupom/anobik}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Anobik", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{anobik}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Rack middleware Ruby micro-framework}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_runtime_dependency(%q<rack>, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<rack>, [">= 0"])
|
33
|
+
end
|
34
|
+
end
|
data/lib/anobik.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'rack/anobik'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
class AnobikServer
|
6
|
+
#
|
7
|
+
# Runs the Anobik middleware app
|
8
|
+
#
|
9
|
+
def self.run
|
10
|
+
#partially stolen from Rails
|
11
|
+
options = {
|
12
|
+
:Port => 3001,
|
13
|
+
:Host => "0.0.0.0",
|
14
|
+
:detach => false,
|
15
|
+
:debugger => false,
|
16
|
+
:production => false
|
17
|
+
}
|
18
|
+
|
19
|
+
ARGV.clone.options do |option|
|
20
|
+
option.on("-p", "--port=port", Integer,
|
21
|
+
"Runs Server on the specified port.", "Default: #{options[:Port]}") { |v| options[:Port] = v }
|
22
|
+
option.on("-b", "--binding=ip", String,
|
23
|
+
"Binds Server to the specified ip.", "Default: #{options[:Host]}") { |v| options[:Host] = v }
|
24
|
+
option.on("-d", "--daemon", "Make server run as a Daemon.") { options[:detach] = true }
|
25
|
+
option.on("-x", "--production", "Run the server in production mode.") { options[:production] = true }
|
26
|
+
option.on("-u", "--debugger", "Enable rack server debugging.") { options[:debugger] = true }
|
27
|
+
|
28
|
+
option.separator ""
|
29
|
+
|
30
|
+
option.on("-h", "--help", "Show this help message.") { puts option.help; exit }
|
31
|
+
option.parse!
|
32
|
+
end
|
33
|
+
|
34
|
+
unless server = Rack::Handler.get(ARGV.first) rescue nil
|
35
|
+
begin
|
36
|
+
server = Rack::Handler::Mongrel
|
37
|
+
rescue LoadError => e
|
38
|
+
server = Rack::Handler::WEBrick
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
puts "=> Booting with #{server}"
|
43
|
+
puts "=> Running in production mode" if options[:production]
|
44
|
+
puts "=> Application starting on http://#{options[:Host]}:#{options[:Port]}#{options[:path]}"
|
45
|
+
|
46
|
+
if options[:detach]
|
47
|
+
puts "=> Running as deamon with pid: #{Process.pid}"
|
48
|
+
Process.daemon
|
49
|
+
else
|
50
|
+
puts "=> Call with -d to detach"
|
51
|
+
end
|
52
|
+
|
53
|
+
app = Rack::Builder.new {
|
54
|
+
use Rack::CommonLogger if options[:debugger]
|
55
|
+
use Rack::ShowExceptions
|
56
|
+
use Rack::ShowStatus
|
57
|
+
use Rack::Static, :urls => ['/statics']
|
58
|
+
use Rack::Anobik, :url => '/', :production => options[:production]
|
59
|
+
|
60
|
+
run lambda { |env|
|
61
|
+
[404, {'Content-Type' => 'text/plain'}, 'Not a Anobik Request :)']
|
62
|
+
}
|
63
|
+
}.to_app
|
64
|
+
|
65
|
+
trap(:INT) { exit }
|
66
|
+
|
67
|
+
puts "=> Ctrl-C to shutdown server"
|
68
|
+
|
69
|
+
begin
|
70
|
+
server.run app, options.merge(:AccessLog => [])
|
71
|
+
ensure
|
72
|
+
puts "Exiting"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/lib/rack/anobik.rb
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
module Rack
|
2
|
+
#
|
3
|
+
# Anobik is a Rack based microframework that provides an easy way to map URL patterns to classes.
|
4
|
+
# URL patterns can be literal strings or regular expressions.
|
5
|
+
# when URLs are processed:
|
6
|
+
# * the beginning and end are anchored (^ $)
|
7
|
+
# * an optional end slash is added (/?)
|
8
|
+
# * the i option is added for case-insensitive searches
|
9
|
+
# * respective class method is called depending on the request method (GET/POST etc.)
|
10
|
+
#
|
11
|
+
# It does not provide any view templating or database handling mechanism.
|
12
|
+
# you can ofcourse use any opensource view templating engine or database access layer with it.
|
13
|
+
# Anobik is very similar to web.py, but it is not a direct clone of web.py.
|
14
|
+
# It is ofcourse highly inspired by web.py and other similar lightweight microframeworks.
|
15
|
+
#
|
16
|
+
# Anobik acts as a Rack middleware app and that means you can use it with a
|
17
|
+
# number of webservers including Thin, LiteSpeed, Phusion Passangeretc.
|
18
|
+
# And BTW, you can also use it with Rails, Sinatra or any other Rack based frameworks.
|
19
|
+
#
|
20
|
+
# e.g:
|
21
|
+
#
|
22
|
+
# * routes.rb
|
23
|
+
# class Routes
|
24
|
+
# URLS = {
|
25
|
+
# "/" => "index",
|
26
|
+
# "/index/(\\d+)" => "index",
|
27
|
+
# }
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# * index.rb
|
31
|
+
# class Index
|
32
|
+
# def get (id=nil)
|
33
|
+
# "Hello world from web.rb! #{id}"
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# * url
|
38
|
+
# GET /index/1
|
39
|
+
#
|
40
|
+
# MIT-License - Anupom Syam
|
41
|
+
#
|
42
|
+
class Anobik
|
43
|
+
|
44
|
+
ERR_MESSAGES = {
|
45
|
+
:missing_method => "Method %s::#%s not supported",
|
46
|
+
:missing_class => "Class %s not found in file %s.rb",
|
47
|
+
:missing_file => "File %s.rb missing in directory %s",
|
48
|
+
:invalid_path => "URL pattern unknown, Path %s not found",
|
49
|
+
:missing_routes_file => "Routes file %s.rb is not present in directory %s",
|
50
|
+
:missing_routes_class => "Class %s is missing in routes file %s.rb"
|
51
|
+
}
|
52
|
+
STATUSES = { :ok => 200, :bad_request => 404}
|
53
|
+
ROUTES_FILENAME = 'routes'
|
54
|
+
|
55
|
+
def initialize app, options
|
56
|
+
@app = app
|
57
|
+
@url = options[:url] || ''
|
58
|
+
@url = '' if @url == '/'
|
59
|
+
@production = options[:production]
|
60
|
+
$LOAD_PATH << ANOBIK_ROOT
|
61
|
+
end
|
62
|
+
|
63
|
+
def call env
|
64
|
+
req = Rack::Request.new(env)
|
65
|
+
path = req.path_info
|
66
|
+
method = req.request_method
|
67
|
+
|
68
|
+
@headers = { 'Content-Type' => 'text/html' }
|
69
|
+
|
70
|
+
#guard statements:
|
71
|
+
unless path.index(@url) == 0
|
72
|
+
return @app.call(env)
|
73
|
+
end
|
74
|
+
|
75
|
+
begin
|
76
|
+
anobik_load ROUTES_FILENAME
|
77
|
+
rescue Exception
|
78
|
+
return anobik_error(:missing_routes_file, [ROUTES_FILENAME, ANOBIK_ROOT])
|
79
|
+
end
|
80
|
+
|
81
|
+
routes_classname = to_class_name ROUTES_FILENAME
|
82
|
+
begin
|
83
|
+
raise NameError unless Object.const_get(routes_classname).kind_of? Class
|
84
|
+
rescue Exception
|
85
|
+
return anobik_error(:missing_routes_class, [routes_classname, ANOBIK_ROOT+ROUTES_FILENAME])
|
86
|
+
end
|
87
|
+
|
88
|
+
begin
|
89
|
+
urls = Object.const_get(routes_classname)::URLS
|
90
|
+
raise unless urls.kind_of? Hash
|
91
|
+
rescue Exception
|
92
|
+
urls = { "/" => "index" }
|
93
|
+
end
|
94
|
+
controller_filename = nil
|
95
|
+
matches = nil
|
96
|
+
urls.each do |regex, filename|
|
97
|
+
matches = path.match(Regexp.new('^' << @url << regex << '/?$', true))
|
98
|
+
unless matches.nil?
|
99
|
+
controller_filename = filename
|
100
|
+
break
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
if controller_filename.nil?
|
105
|
+
return anobik_error(:invalid_path, [path])
|
106
|
+
end
|
107
|
+
|
108
|
+
begin
|
109
|
+
anobik_load controller_filename
|
110
|
+
rescue Exception
|
111
|
+
return anobik_error(:missing_file, [controller_filename, ANOBIK_ROOT])
|
112
|
+
end
|
113
|
+
|
114
|
+
controller_classname = to_class_name controller_filename
|
115
|
+
begin
|
116
|
+
raise NameError unless Object.const_get(controller_classname).kind_of? Class
|
117
|
+
rescue Exception
|
118
|
+
return anobik_error(:missing_class, [controller_classname, ANOBIK_ROOT+controller_filename])
|
119
|
+
end
|
120
|
+
|
121
|
+
controller = Object.const_get(controller_classname).new
|
122
|
+
unless controller.respond_to?(method)
|
123
|
+
return anobik_error(:missing_method, [controller_classname, method])
|
124
|
+
end
|
125
|
+
|
126
|
+
body = eval 'controller.' << method <<
|
127
|
+
'(' << matches.captures.join(' , ') << ')'
|
128
|
+
|
129
|
+
[STATUSES[:ok], @headers, body]
|
130
|
+
end
|
131
|
+
|
132
|
+
def to_class_name filename
|
133
|
+
#shamelessly copied from Rails
|
134
|
+
filename.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
135
|
+
end
|
136
|
+
|
137
|
+
def anobik_error(err, array = [])
|
138
|
+
#will be handled by RACK::ShowExceptions
|
139
|
+
raise ERR_MESSAGES[err] % array unless @production
|
140
|
+
#will be handled by RACK::ShowStatus
|
141
|
+
return [STATUSES[:bad_request], @headers, '']
|
142
|
+
end
|
143
|
+
|
144
|
+
def anobik_root
|
145
|
+
ANOBIK_ROOT
|
146
|
+
end
|
147
|
+
|
148
|
+
def anobik_load filename
|
149
|
+
if (@production)
|
150
|
+
require filename
|
151
|
+
else
|
152
|
+
classname = to_class_name(filename).to_sym
|
153
|
+
Object.class_eval { remove_const classname } if Object.const_defined? classname
|
154
|
+
Kernel.load filename + '.rb'
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,248 @@
|
|
1
|
+
require 'test/spec'
|
2
|
+
|
3
|
+
require 'rack'
|
4
|
+
require 'rack/mock'
|
5
|
+
require 'rack/anobik'
|
6
|
+
|
7
|
+
ANOBIK_ROOT = "#{File.dirname(File.expand_path(__FILE__))}/" unless defined?(ANOBIK_ROOT)
|
8
|
+
|
9
|
+
describe "Rack::Anobik" do
|
10
|
+
|
11
|
+
class EqualResponse < Test::Spec::CustomShould
|
12
|
+
def assumptions(response)
|
13
|
+
response.status.should.equal(object[:status])
|
14
|
+
response.body.should.match(object[:body])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def equal_response(response)
|
19
|
+
EqualResponse.new(response)
|
20
|
+
end
|
21
|
+
|
22
|
+
class EqualNotFound < Test::Spec::CustomShould
|
23
|
+
def assumptions(response)
|
24
|
+
response.status.should.equal(404)
|
25
|
+
response.body.should.match('Not Found')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def equal_not_found()
|
30
|
+
EqualNotFound.new(nil)
|
31
|
+
end
|
32
|
+
|
33
|
+
setup do
|
34
|
+
remove_routes_file
|
35
|
+
remove_file 'index'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return default when non-anobik URL with production" do
|
39
|
+
response = anobik_app_production( '/other/').get('/')
|
40
|
+
response.should equal_response({:status => 200, :body => 'Not a Anobik Request'})
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return default when non-anobik url requested with dev" do
|
44
|
+
response = anobik_app_dev('/other/').get('/')
|
45
|
+
response.should equal_response({:status => 200, :body => 'Not a Anobik Request'})
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have the same ANOBIK_ROOT" do
|
49
|
+
ANOBIK_ROOT.should.equal( Rack::Anobik.new(nil, options('/', false)).anobik_root)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return 404 when routes.rb is not present with production" do
|
53
|
+
remove_routes_file
|
54
|
+
response = anobik_app_production.get('/')
|
55
|
+
response.should equal_not_found
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return 500 when routes.rb is not present with dev" do
|
59
|
+
remove_routes_file
|
60
|
+
response = anobik_app_dev.get('/')
|
61
|
+
response.should equal_response({:status => 500, :body => 'routes.rb ' << 'is not present in directory'})
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return 404 when Routes class is not defined with production" do
|
65
|
+
create_routes_file ''
|
66
|
+
response = anobik_app_production.get('/')
|
67
|
+
response.should equal_not_found
|
68
|
+
remove_routes_file
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should return 500 when Routes class is not defined with dev" do
|
72
|
+
create_routes_file ''
|
73
|
+
response = anobik_app_dev.get('/')
|
74
|
+
response.should equal_response({:status => 500, :body => 'Class Routes ' << 'is missing in routes file'})
|
75
|
+
remove_routes_file
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should return 404 when no URL pattern matches with production" do
|
79
|
+
create_routes_file "class Routes\nend"
|
80
|
+
response = anobik_app_production.get('/unknown/pattern')
|
81
|
+
response.should equal_not_found
|
82
|
+
remove_routes_file
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should return 500 when no URL pattern matches with dev" do
|
86
|
+
create_routes_file "class Routes\nend"
|
87
|
+
response =anobik_app_dev.get('/unknown/pattern')
|
88
|
+
response.should equal_response({:status => 500, :body => 'URL pattern ' << 'unknown'})
|
89
|
+
remove_routes_file
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return 404 when filename does not exist with production" do
|
93
|
+
remove_file 'index'
|
94
|
+
create_routes_file "class Routes\nend"
|
95
|
+
response = anobik_app_production.get('/')
|
96
|
+
response.should equal_not_found
|
97
|
+
remove_routes_file
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should return 500 when filename does not exist with dev" do
|
101
|
+
remove_file 'index'
|
102
|
+
create_routes_file "class Routes\nend"
|
103
|
+
response = anobik_app_dev.get('/')
|
104
|
+
response.should equal_response({:status => 500, :body => 'missing ' << 'in directory'})
|
105
|
+
remove_routes_file
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should return 404 when filename exists but class missing with production" do
|
109
|
+
create_routes_file "class Routes\nend"
|
110
|
+
create_file 'index', ''
|
111
|
+
response = anobik_app_production.get('/')
|
112
|
+
response.should equal_not_found
|
113
|
+
remove_routes_file
|
114
|
+
remove_file 'index'
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should return 500 when filename exists but class missing with dev" do
|
118
|
+
create_routes_file "class Routes\nend"
|
119
|
+
create_file 'index', ''
|
120
|
+
response = anobik_app_dev.get('/')
|
121
|
+
response.should equal_response({:status => 500, :body => 'Class Index ' << 'not found in file'})
|
122
|
+
remove_routes_file
|
123
|
+
remove_file 'index'
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should return 404 when class exists but method missing with production" do
|
127
|
+
create_routes_file "class Routes\nend"
|
128
|
+
create_file 'index', "class Index\nend"
|
129
|
+
response = anobik_app_production.get('/')
|
130
|
+
response.should equal_not_found
|
131
|
+
remove_routes_file
|
132
|
+
remove_file 'index'
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should return 500 when class exists but method missing with dev" do
|
136
|
+
create_routes_file "class Routes\nend"
|
137
|
+
create_file 'index', "class Index\nend"
|
138
|
+
response = anobik_app_dev.get('/')
|
139
|
+
response.should equal_response({:status => 500, :body => 'Method Index::#GET ' << 'not supported'})
|
140
|
+
remove_routes_file
|
141
|
+
remove_file 'index'
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should return 200 for GET when everything's ok" do
|
145
|
+
create_routes_file routes_file
|
146
|
+
create_file 'index', index_file
|
147
|
+
response = anobik_app_dev.get('/')
|
148
|
+
response.should equal_response({:status => 200, :body => 'GET_' << 'SUCCESS'})
|
149
|
+
remove_routes_file
|
150
|
+
remove_file 'index'
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should return 200 when regex is valid" do
|
154
|
+
create_routes_file routes_file
|
155
|
+
create_file 'index', index_file
|
156
|
+
response = anobik_app_dev.get('/index/98')
|
157
|
+
response.should equal_response({:status => 200, :body => 'GET_' << 'SUCCESS98'})
|
158
|
+
remove_routes_file
|
159
|
+
remove_file 'index'
|
160
|
+
end
|
161
|
+
|
162
|
+
xit "should return 200 when root URL is called and index is present" do
|
163
|
+
create_routes_file routes_file
|
164
|
+
create_file 'index', index_file
|
165
|
+
response = anobik_app_dev.get('/')
|
166
|
+
response.should equal_response({:status => 200, :body => 'GET_' << 'SUCCESS'})
|
167
|
+
remove_routes_file
|
168
|
+
remove_file 'index'
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should handle POST data" do
|
172
|
+
create_routes_file routes_file
|
173
|
+
create_file 'index', index_file
|
174
|
+
response = anobik_app_dev.post('/')
|
175
|
+
response.should equal_response({:status => 200, :body => 'POST_SUC' << 'CESS'})
|
176
|
+
remove_routes_file
|
177
|
+
remove_file 'index'
|
178
|
+
end
|
179
|
+
|
180
|
+
xit "should handle DELETE requests" do
|
181
|
+
end
|
182
|
+
|
183
|
+
xit "should handle PUT request" do
|
184
|
+
end
|
185
|
+
|
186
|
+
xit "should handle pseudo-DELETE requests" do
|
187
|
+
end
|
188
|
+
|
189
|
+
xit "should handle pseudo-PUT request" do
|
190
|
+
end
|
191
|
+
|
192
|
+
def anobik_app options
|
193
|
+
Rack::Builder.new {
|
194
|
+
use Rack::ShowExceptions
|
195
|
+
use Rack::ShowStatus
|
196
|
+
use Rack::Static, :urls => ['/statics']
|
197
|
+
use Rack::Anobik, :url => options[:url], :production => options[:production]
|
198
|
+
run lambda { |env|
|
199
|
+
[200, { 'Content-Type' => 'text/plain' }, 'Not a Anobik Request :)']
|
200
|
+
}
|
201
|
+
}.to_app
|
202
|
+
end
|
203
|
+
|
204
|
+
def anobik_app_production anobik_url=nil
|
205
|
+
anobik_url = '/' if anobik_url.nil?
|
206
|
+
Rack::MockRequest.new(anobik_app options(anobik_url, true))
|
207
|
+
end
|
208
|
+
|
209
|
+
def anobik_app_dev anobik_url=nil
|
210
|
+
anobik_url = '/' if anobik_url.nil?
|
211
|
+
Rack::MockRequest.new(anobik_app options(anobik_url, false))
|
212
|
+
end
|
213
|
+
|
214
|
+
def options url, production
|
215
|
+
{ :url => url, :production =>production }
|
216
|
+
end
|
217
|
+
|
218
|
+
def create_routes_file str
|
219
|
+
create_file 'routes', str
|
220
|
+
end
|
221
|
+
|
222
|
+
def remove_routes_file
|
223
|
+
remove_file 'routes'
|
224
|
+
end
|
225
|
+
|
226
|
+
def create_file filename, str
|
227
|
+
::File.open(ANOBIK_ROOT + filename + '.rb', 'w') {|f| f.write(str) }
|
228
|
+
end
|
229
|
+
|
230
|
+
def remove_file filename
|
231
|
+
::File.delete(ANOBIK_ROOT + filename + '.rb') if ::File.exist?(ANOBIK_ROOT + filename + '.rb')
|
232
|
+
end
|
233
|
+
|
234
|
+
def index_file
|
235
|
+
return "class Index\n" <<
|
236
|
+
"def GET(id=nil)\n" <<
|
237
|
+
"'GET_SUCCESS'<<id.to_s\n" <<
|
238
|
+
"end\n" <<
|
239
|
+
"def POST()\n" <<
|
240
|
+
"'POST_SUCCESS'\n" <<
|
241
|
+
"end\n" <<
|
242
|
+
"end"
|
243
|
+
end
|
244
|
+
|
245
|
+
def routes_file
|
246
|
+
"class Routes\nURLS = {\n'/' => 'index',\n'/index/(\\d+)' => 'index'\n}\nend"
|
247
|
+
end
|
248
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: anupom-anobik
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anupom Syam
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-24 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
|
+
description: Rack middleware Ruby micro-framework
|
26
|
+
email: anupom.syam@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- CHANGELOG.txt
|
33
|
+
- lib/anobik.rb
|
34
|
+
- lib/rack/anobik.rb
|
35
|
+
- README.rdoc
|
36
|
+
files:
|
37
|
+
- test/spec_rack_anobik.rb
|
38
|
+
- Rakefile
|
39
|
+
- CHANGELOG.txt
|
40
|
+
- lib/anobik.rb
|
41
|
+
- lib/rack/anobik.rb
|
42
|
+
- anobik-server
|
43
|
+
- Manifest
|
44
|
+
- README.rdoc
|
45
|
+
- anobik.gemspec
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/anupom/anobik
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --line-numbers
|
51
|
+
- --inline-source
|
52
|
+
- --title
|
53
|
+
- Anobik
|
54
|
+
- --main
|
55
|
+
- README.rdoc
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "1.2"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: anobik
|
73
|
+
rubygems_version: 1.2.0
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: Rack middleware Ruby micro-framework
|
77
|
+
test_files: []
|
78
|
+
|