anupom-anobik 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +9 -2
- data/Manifest +4 -1
- data/README.rdoc +39 -26
- data/Rakefile +1 -1
- data/anobik.gemspec +7 -5
- data/config.ru +10 -0
- data/lib/anobik/app.rb +22 -0
- data/lib/anobik/consts.rb +6 -0
- data/lib/anobik/server.rb +66 -0
- data/lib/anobik/utils.rb +19 -0
- data/lib/rack/anobik.rb +63 -35
- data/{anobik-server → server} +3 -3
- data/test/spec_rack_anobik.rb +40 -31
- metadata +10 -5
- data/lib/anobik.rb +0 -75
data/CHANGELOG.txt
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
=== 0.0.3 / 2009-09-10
|
2
|
+
|
3
|
+
* 1 major changes
|
4
|
+
|
5
|
+
* Added erb template rendering support
|
6
|
+
* Added phusion passanger support
|
7
|
+
* New directory structure introduced
|
8
|
+
|
1
9
|
=== 0.0.2 / 2009-05-24
|
2
10
|
|
3
11
|
* 1 major changes
|
@@ -10,5 +18,4 @@
|
|
10
18
|
|
11
19
|
* 1 major enhancement
|
12
20
|
|
13
|
-
* Anobik's Birthday!
|
14
|
-
|
21
|
+
* Anobik's Birthday!
|
data/Manifest
CHANGED
data/README.rdoc
CHANGED
@@ -7,43 +7,55 @@
|
|
7
7
|
Anobik is a simple rack middleware ruby microframework
|
8
8
|
|
9
9
|
== FEATURES/PROBLEMS:
|
10
|
-
|
11
10
|
Anobik is a Rack based microframework that provides an easy way to map URL patterns to classes.
|
12
11
|
URL patterns can be literal strings or regular expressions.
|
13
12
|
when URLs are processed:
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
* the beginning and end are anchored (^ $)
|
14
|
+
* an optional end slash is added (/?)
|
15
|
+
* the i option is added for case-insensitive searches
|
16
|
+
* respective class method is called depending on the request method (GET/POST etc.)
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
Anobik is
|
22
|
-
|
18
|
+
Anobik does not provide any database handling mechanism yet.
|
19
|
+
You can ofcourse use any opensource database access layer with it - it's pretty flexible.
|
20
|
+
Anobik is suitable for small micro-sites where you would love to use your
|
21
|
+
favorite programming language but Rails is a bit heavy for that.
|
23
22
|
|
24
23
|
Anobik acts as a Rack middleware app and that means you can use it with a
|
25
24
|
number of webservers including Thin, LiteSpeed, Phusion Passangeretc.
|
26
|
-
And
|
25
|
+
And the best part is, you can also use it with Rails, Sinatra or any other Rack based frameworks.
|
26
|
+
|
27
|
+
Anobik is truely feather-weight. It has < 250 LOC!
|
28
|
+
It does not have many features but it can get you started early.
|
29
|
+
More docs and more changes are coming soon, stay tuuneed!
|
27
30
|
|
28
31
|
== SYNOPSIS:
|
29
32
|
|
30
|
-
* routes.rb
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
* index.rb
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
33
|
+
* configs/routes.rb
|
34
|
+
class Routes
|
35
|
+
URLS = {
|
36
|
+
"/" => "index",
|
37
|
+
"/page/(\\d+)" => "page",
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
* resources/index.rb
|
42
|
+
class Index < Anobik::Resource
|
43
|
+
def get
|
44
|
+
@hello = "Hello"
|
45
|
+
render 'index', {:world => 'world'}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
* faces/index.erb
|
50
|
+
<%= [@hello, @world].join(' ') %>
|
51
|
+
|
52
|
+
* command
|
53
|
+
$ ruby -rubygems server
|
54
|
+
OR
|
55
|
+
$ rackup config.ru
|
44
56
|
|
45
57
|
* url
|
46
|
-
|
58
|
+
GET /index/1
|
47
59
|
|
48
60
|
== REQUIREMENTS:
|
49
61
|
|
@@ -51,7 +63,8 @@ Ruby 1.9 and Rack are the pre-requisites
|
|
51
63
|
|
52
64
|
== INSTALL:
|
53
65
|
|
54
|
-
|
66
|
+
gem sources -a http://gems.github.com
|
67
|
+
sudo gem install anupom-anobik
|
55
68
|
|
56
69
|
== LICENSE:
|
57
70
|
|
data/Rakefile
CHANGED
data/anobik.gemspec
CHANGED
@@ -2,15 +2,17 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{anobik}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Anupom Syam"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-09-10}
|
10
10
|
s.description = %q{Rack middleware Ruby micro-framework}
|
11
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/
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG.txt", "lib/anobik/server.rb", "lib/anobik/app.rb", "lib/rack/anobik.rb", "README.rdoc"]
|
13
|
+
s.files = ["test/spec_rack_anobik.rb", "Rakefile", "CHANGELOG.txt", "lib/anobik/server.rb", "lib/anobik/app.rb",
|
14
|
+
"lib/anobik/consts.rb", "lib/anobik/utils.rb", "lib/rack/anobik.rb", "server",
|
15
|
+
"config.ru", "Manifest", "README.rdoc", "anobik.gemspec"]
|
14
16
|
s.has_rdoc = true
|
15
17
|
s.homepage = %q{http://github.com/anupom/anobik}
|
16
18
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Anobik", "--main", "README.rdoc"]
|
@@ -31,4 +33,4 @@ Gem::Specification.new do |s|
|
|
31
33
|
else
|
32
34
|
s.add_dependency(%q<rack>, [">= 0"])
|
33
35
|
end
|
34
|
-
end
|
36
|
+
end
|
data/config.ru
ADDED
data/lib/anobik/app.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'rack/anobik'
|
3
|
+
require 'anobik/utils'
|
4
|
+
|
5
|
+
module Anobik
|
6
|
+
class App
|
7
|
+
def self.create debugger, production
|
8
|
+
Rack::Builder.new {
|
9
|
+
use Rack::CommonLogger if debugger
|
10
|
+
use Rack::ShowExceptions
|
11
|
+
use Rack::ShowStatus
|
12
|
+
use Rack::Static, :urls => ['/public']
|
13
|
+
use Rack::Anobik, :url => '/', :production => production
|
14
|
+
|
15
|
+
#should never get through here if url is set to '/'
|
16
|
+
run lambda { |env|
|
17
|
+
[404, {'Content-Type' => 'text/plain'}, 'Not a Anobik Request :)']
|
18
|
+
}
|
19
|
+
}.to_app
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'anobik/app'
|
2
|
+
require 'optparse'
|
3
|
+
|
4
|
+
module Anobik
|
5
|
+
class Server
|
6
|
+
#
|
7
|
+
# Runs the Anobik middleware app
|
8
|
+
#
|
9
|
+
def self.run
|
10
|
+
#partially stolen from Rails
|
11
|
+
options = {
|
12
|
+
:Port => 9291,
|
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 = Anobik::App::create options[:debugger], options[:production]
|
54
|
+
|
55
|
+
trap(:INT) { exit }
|
56
|
+
|
57
|
+
puts "=> Ctrl-C to shutdown server"
|
58
|
+
|
59
|
+
begin
|
60
|
+
server.run app, options.merge(:AccessLog => [])
|
61
|
+
ensure
|
62
|
+
puts "Exiting"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/anobik/utils.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'anobik/consts'
|
2
|
+
|
3
|
+
#TODO make @request/@response $request/$response?
|
4
|
+
module Anobik
|
5
|
+
class Resource
|
6
|
+
def initialize
|
7
|
+
@anobik_env = yield
|
8
|
+
@request = Rack::Request.new(@anobik_env)
|
9
|
+
@response = Rack::Request.new(@anobik_env)
|
10
|
+
end
|
11
|
+
|
12
|
+
def render facename, local_assigns = {}
|
13
|
+
local_assigns.each do |key,value|
|
14
|
+
eval "@#{key} = #{value.inspect}"
|
15
|
+
end
|
16
|
+
ERB.new(File.read(ANOBIK_ROOT + ::Anobik::FACES_DIR + facename + '.erb')).result(binding)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/rack/anobik.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'anobik/consts'
|
2
|
+
|
1
3
|
module Rack
|
2
4
|
#
|
3
5
|
# Anobik is a Rack based microframework that provides an easy way to map URL patterns to classes.
|
@@ -8,34 +10,48 @@ module Rack
|
|
8
10
|
# * the i option is added for case-insensitive searches
|
9
11
|
# * respective class method is called depending on the request method (GET/POST etc.)
|
10
12
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# Anobik is
|
14
|
-
#
|
13
|
+
# Anobik does not provide any database handling mechanism yet.
|
14
|
+
# You can ofcourse use any opensource database access layer with it - it's pretty flexible.
|
15
|
+
# Anobik is suitable for small micro-sites where you would love to use your
|
16
|
+
# favorite programming language but Rails is a bit heavy for that.
|
15
17
|
#
|
16
18
|
# Anobik acts as a Rack middleware app and that means you can use it with a
|
17
19
|
# number of webservers including Thin, LiteSpeed, Phusion Passangeretc.
|
18
|
-
# And
|
20
|
+
# And the best part is, you can also use it with Rails, Sinatra or any other Rack based frameworks.
|
21
|
+
#
|
22
|
+
# Anobik is truely feather-weight. It has < 250 LOC!
|
23
|
+
# It does not have many features but it can get you started early.
|
24
|
+
# More docs coming soon, stay tuuneed!
|
19
25
|
#
|
20
26
|
# e.g:
|
21
27
|
#
|
22
|
-
# * routes.rb
|
28
|
+
# * configs/routes.rb
|
23
29
|
# class Routes
|
24
30
|
# URLS = {
|
25
31
|
# "/" => "index",
|
26
|
-
# "/
|
32
|
+
# "/page/(\\d+)" => "page",
|
27
33
|
# }
|
28
34
|
# end
|
29
35
|
#
|
30
|
-
# * index.rb
|
31
|
-
# class Index
|
32
|
-
# def get
|
33
|
-
#
|
36
|
+
# * resources/index.rb
|
37
|
+
# class Index < Anobik::Resource
|
38
|
+
# def get
|
39
|
+
# @hello = "Hello"
|
40
|
+
# render 'index', {:world => 'world'}
|
34
41
|
# end
|
35
42
|
# end
|
36
43
|
#
|
44
|
+
# * faces/index.erb
|
45
|
+
# <%= [@hello, @world].join(' ') %>
|
46
|
+
#
|
47
|
+
# * command
|
48
|
+
# $ ruby -rubygems server
|
49
|
+
# OR
|
50
|
+
# $ rackup config.ru
|
51
|
+
#
|
37
52
|
# * url
|
38
53
|
# GET /index/1
|
54
|
+
#
|
39
55
|
#
|
40
56
|
# MIT-License - Anupom Syam
|
41
57
|
#
|
@@ -50,7 +66,6 @@ module Rack
|
|
50
66
|
:missing_routes_class => "Class %s is missing in routes file %s.rb"
|
51
67
|
}
|
52
68
|
STATUSES = { :ok => 200, :bad_request => 404}
|
53
|
-
ROUTES_FILENAME = 'routes'
|
54
69
|
|
55
70
|
def initialize app, options
|
56
71
|
@app = app
|
@@ -63,7 +78,7 @@ module Rack
|
|
63
78
|
def call env
|
64
79
|
req = Rack::Request.new(env)
|
65
80
|
path = req.path_info
|
66
|
-
method = req.request_method
|
81
|
+
method = req.request_method.downcase
|
67
82
|
|
68
83
|
@headers = { 'Content-Type' => 'text/html' }
|
69
84
|
|
@@ -73,16 +88,19 @@ module Rack
|
|
73
88
|
end
|
74
89
|
|
75
90
|
begin
|
76
|
-
|
91
|
+
anobik_load_config ::Anobik::ROUTES_FILE
|
77
92
|
rescue Exception
|
78
|
-
return anobik_error(:missing_routes_file, [
|
93
|
+
return anobik_error(:missing_routes_file, [::Anobik::ROUTES_FILE,
|
94
|
+
ANOBIK_ROOT + ::Anobik::CONFIG_DIR])
|
79
95
|
end
|
80
96
|
|
81
|
-
routes_classname = to_class_name
|
97
|
+
routes_classname = to_class_name ::Anobik::ROUTES_FILE
|
82
98
|
begin
|
83
99
|
raise NameError unless Object.const_get(routes_classname).kind_of? Class
|
84
|
-
|
85
|
-
return anobik_error(:missing_routes_class,
|
100
|
+
rescue Exception
|
101
|
+
return anobik_error(:missing_routes_class,
|
102
|
+
[routes_classname, ANOBIK_ROOT + ::Anobik::CONFIG_DIR +
|
103
|
+
::Anobik::ROUTES_FILE])
|
86
104
|
end
|
87
105
|
|
88
106
|
begin
|
@@ -91,39 +109,41 @@ module Rack
|
|
91
109
|
rescue Exception
|
92
110
|
urls = { "/" => "index" }
|
93
111
|
end
|
94
|
-
|
112
|
+
resource_filename = nil
|
95
113
|
matches = nil
|
96
114
|
urls.each do |regex, filename|
|
97
115
|
matches = path.match(Regexp.new('^' << @url << regex << '/?$', true))
|
98
116
|
unless matches.nil?
|
99
|
-
|
117
|
+
resource_filename = filename
|
100
118
|
break
|
101
119
|
end
|
102
120
|
end
|
103
121
|
|
104
|
-
if
|
122
|
+
if resource_filename.nil?
|
105
123
|
return anobik_error(:invalid_path, [path])
|
106
124
|
end
|
107
125
|
|
108
126
|
begin
|
109
|
-
|
127
|
+
anobik_load_resource resource_filename
|
110
128
|
rescue Exception
|
111
|
-
return anobik_error(:missing_file, [
|
129
|
+
return anobik_error(:missing_file, [resource_filename, ANOBIK_ROOT +
|
130
|
+
::Anobik::RESOURCE_DIR])
|
112
131
|
end
|
113
132
|
|
114
|
-
|
133
|
+
resource_classname = to_class_name resource_filename
|
115
134
|
begin
|
116
|
-
raise NameError unless Object.const_get(
|
135
|
+
raise NameError unless Object.const_get(resource_classname).kind_of? Class
|
117
136
|
rescue Exception
|
118
|
-
return anobik_error(:missing_class, [
|
137
|
+
return anobik_error(:missing_class, [resource_classname, ANOBIK_ROOT +
|
138
|
+
::Anobik::RESOURCE_DIR + resource_filename])
|
119
139
|
end
|
120
140
|
|
121
|
-
|
122
|
-
unless
|
123
|
-
return anobik_error(:missing_method, [
|
141
|
+
resource = Object.const_get(resource_classname).new(){env}
|
142
|
+
unless resource.respond_to?(method)
|
143
|
+
return anobik_error(:missing_method, [resource_classname, method])
|
124
144
|
end
|
125
145
|
|
126
|
-
body = eval '
|
146
|
+
body = eval 'resource.' << method <<
|
127
147
|
'(' << matches.captures.join(' , ') << ')'
|
128
148
|
|
129
149
|
[STATUSES[:ok], @headers, body]
|
@@ -141,19 +161,27 @@ module Rack
|
|
141
161
|
return [STATUSES[:bad_request], @headers, '']
|
142
162
|
end
|
143
163
|
|
144
|
-
def
|
145
|
-
|
164
|
+
def anobik_load_config filename
|
165
|
+
anobik_load ::Anobik::CONFIG_DIR, filename
|
146
166
|
end
|
147
167
|
|
148
|
-
def
|
168
|
+
def anobik_load_resource filename
|
169
|
+
anobik_load ::Anobik::RESOURCE_DIR, filename
|
170
|
+
end
|
171
|
+
|
172
|
+
def anobik_load dirname, filename
|
149
173
|
if (@production)
|
150
|
-
require filename
|
174
|
+
require dirname + filename
|
151
175
|
else
|
152
176
|
classname = to_class_name(filename).to_sym
|
153
177
|
Object.class_eval { remove_const classname } if Object.const_defined? classname
|
154
|
-
Kernel.load filename + '.rb'
|
178
|
+
Kernel.load dirname + filename + '.rb'
|
155
179
|
end
|
156
180
|
end
|
181
|
+
|
182
|
+
def anobik_root
|
183
|
+
ANOBIK_ROOT
|
184
|
+
end
|
157
185
|
|
158
186
|
end
|
159
187
|
end
|
data/{anobik-server → server}
RENAMED
data/test/spec_rack_anobik.rb
CHANGED
@@ -32,7 +32,7 @@ describe "Rack::Anobik" do
|
|
32
32
|
|
33
33
|
setup do
|
34
34
|
remove_routes_file
|
35
|
-
|
35
|
+
remove_resource_file 'index'
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should return default when non-anobik URL with production" do
|
@@ -90,7 +90,7 @@ describe "Rack::Anobik" do
|
|
90
90
|
end
|
91
91
|
|
92
92
|
it "should return 404 when filename does not exist with production" do
|
93
|
-
|
93
|
+
remove_resource_file 'index'
|
94
94
|
create_routes_file "class Routes\nend"
|
95
95
|
response = anobik_app_production.get('/')
|
96
96
|
response.should equal_not_found
|
@@ -98,7 +98,7 @@ describe "Rack::Anobik" do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should return 500 when filename does not exist with dev" do
|
101
|
-
|
101
|
+
remove_resource_file 'index'
|
102
102
|
create_routes_file "class Routes\nend"
|
103
103
|
response = anobik_app_dev.get('/')
|
104
104
|
response.should equal_response({:status => 500, :body => 'missing ' << 'in directory'})
|
@@ -107,74 +107,74 @@ describe "Rack::Anobik" do
|
|
107
107
|
|
108
108
|
it "should return 404 when filename exists but class missing with production" do
|
109
109
|
create_routes_file "class Routes\nend"
|
110
|
-
|
110
|
+
create_resource_file 'index', ''
|
111
111
|
response = anobik_app_production.get('/')
|
112
112
|
response.should equal_not_found
|
113
113
|
remove_routes_file
|
114
|
-
|
114
|
+
remove_resource_file 'index'
|
115
115
|
end
|
116
116
|
|
117
117
|
it "should return 500 when filename exists but class missing with dev" do
|
118
118
|
create_routes_file "class Routes\nend"
|
119
|
-
|
119
|
+
create_resource_file 'index', ''
|
120
120
|
response = anobik_app_dev.get('/')
|
121
121
|
response.should equal_response({:status => 500, :body => 'Class Index ' << 'not found in file'})
|
122
122
|
remove_routes_file
|
123
|
-
|
123
|
+
remove_resource_file 'index'
|
124
124
|
end
|
125
125
|
|
126
126
|
it "should return 404 when class exists but method missing with production" do
|
127
127
|
create_routes_file "class Routes\nend"
|
128
|
-
|
128
|
+
create_resource_file 'index', "class Index\nend"
|
129
129
|
response = anobik_app_production.get('/')
|
130
130
|
response.should equal_not_found
|
131
131
|
remove_routes_file
|
132
|
-
|
132
|
+
remove_resource_file 'index'
|
133
133
|
end
|
134
134
|
|
135
135
|
it "should return 500 when class exists but method missing with dev" do
|
136
136
|
create_routes_file "class Routes\nend"
|
137
|
-
|
137
|
+
create_resource_file 'index', "class Index\nend"
|
138
138
|
response = anobik_app_dev.get('/')
|
139
|
-
response.should equal_response({:status => 500, :body => 'Method Index::#
|
139
|
+
response.should equal_response({:status => 500, :body => 'Method Index::#get ' << 'not supported'})
|
140
140
|
remove_routes_file
|
141
|
-
|
141
|
+
remove_resource_file 'index'
|
142
142
|
end
|
143
143
|
|
144
144
|
it "should return 200 for GET when everything's ok" do
|
145
145
|
create_routes_file routes_file
|
146
|
-
|
146
|
+
create_resource_file 'index', index_file
|
147
147
|
response = anobik_app_dev.get('/')
|
148
148
|
response.should equal_response({:status => 200, :body => 'GET_' << 'SUCCESS'})
|
149
149
|
remove_routes_file
|
150
|
-
|
150
|
+
remove_resource_file 'index'
|
151
151
|
end
|
152
152
|
|
153
153
|
it "should return 200 when regex is valid" do
|
154
154
|
create_routes_file routes_file
|
155
|
-
|
155
|
+
create_resource_file 'index', index_file
|
156
156
|
response = anobik_app_dev.get('/index/98')
|
157
157
|
response.should equal_response({:status => 200, :body => 'GET_' << 'SUCCESS98'})
|
158
158
|
remove_routes_file
|
159
|
-
|
159
|
+
remove_resource_file 'index'
|
160
160
|
end
|
161
161
|
|
162
|
-
|
162
|
+
it "should return 200 when root URL is called and index is present" do
|
163
163
|
create_routes_file routes_file
|
164
|
-
|
164
|
+
create_resource_file 'index', index_file
|
165
165
|
response = anobik_app_dev.get('/')
|
166
166
|
response.should equal_response({:status => 200, :body => 'GET_' << 'SUCCESS'})
|
167
167
|
remove_routes_file
|
168
|
-
|
168
|
+
remove_resource_file 'index'
|
169
169
|
end
|
170
170
|
|
171
171
|
it "should handle POST data" do
|
172
172
|
create_routes_file routes_file
|
173
|
-
|
173
|
+
create_resource_file 'index', index_file
|
174
174
|
response = anobik_app_dev.post('/')
|
175
175
|
response.should equal_response({:status => 200, :body => 'POST_SUC' << 'CESS'})
|
176
176
|
remove_routes_file
|
177
|
-
|
177
|
+
remove_resource_file 'index'
|
178
178
|
end
|
179
179
|
|
180
180
|
xit "should handle DELETE requests" do
|
@@ -193,7 +193,7 @@ describe "Rack::Anobik" do
|
|
193
193
|
Rack::Builder.new {
|
194
194
|
use Rack::ShowExceptions
|
195
195
|
use Rack::ShowStatus
|
196
|
-
use Rack::Static, :urls => ['/
|
196
|
+
use Rack::Static, :urls => ['/public']
|
197
197
|
use Rack::Anobik, :url => options[:url], :production => options[:production]
|
198
198
|
run lambda { |env|
|
199
199
|
[200, { 'Content-Type' => 'text/plain' }, 'Not a Anobik Request :)']
|
@@ -216,27 +216,36 @@ describe "Rack::Anobik" do
|
|
216
216
|
end
|
217
217
|
|
218
218
|
def create_routes_file str
|
219
|
-
create_file 'routes', str
|
219
|
+
create_file 'routes', 'configs/', str
|
220
220
|
end
|
221
221
|
|
222
222
|
def remove_routes_file
|
223
|
-
remove_file 'routes'
|
223
|
+
remove_file 'routes', 'configs/'
|
224
224
|
end
|
225
225
|
|
226
|
-
def
|
227
|
-
|
226
|
+
def create_resource_file filename, str
|
227
|
+
create_file filename, 'resources/', str
|
228
228
|
end
|
229
229
|
|
230
|
-
def
|
231
|
-
|
230
|
+
def remove_resource_file filename
|
231
|
+
remove_file filename, 'resources/'
|
232
232
|
end
|
233
233
|
|
234
|
+
def create_file filename, directory, str
|
235
|
+
::File.open(ANOBIK_ROOT + directory + filename + '.rb', 'w') {|f| f.write(str) }
|
236
|
+
end
|
237
|
+
|
238
|
+
def remove_file filename, directory
|
239
|
+
file = ANOBIK_ROOT + directory + filename + '.rb'
|
240
|
+
::File.delete(file) if ::File.exist?(file)
|
241
|
+
end
|
242
|
+
|
234
243
|
def index_file
|
235
244
|
return "class Index\n" <<
|
236
|
-
"def
|
237
|
-
"'GET_SUCCESS'<<id.to_s\n" <<
|
245
|
+
"def get(id=nil)\n" <<
|
246
|
+
"'GET_SUCCESS' << id.to_s\n" <<
|
238
247
|
"end\n" <<
|
239
|
-
"def
|
248
|
+
"def post()\n" <<
|
240
249
|
"'POST_SUCCESS'\n" <<
|
241
250
|
"end\n" <<
|
242
251
|
"end"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anupom-anobik
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anupom Syam
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-10 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,16 +30,21 @@ extensions: []
|
|
30
30
|
|
31
31
|
extra_rdoc_files:
|
32
32
|
- CHANGELOG.txt
|
33
|
-
- lib/anobik.rb
|
33
|
+
- lib/anobik/server.rb
|
34
|
+
- lib/anobik/app.rb
|
34
35
|
- lib/rack/anobik.rb
|
35
36
|
- README.rdoc
|
36
37
|
files:
|
37
38
|
- test/spec_rack_anobik.rb
|
38
39
|
- Rakefile
|
39
40
|
- CHANGELOG.txt
|
40
|
-
- lib/anobik.rb
|
41
|
+
- lib/anobik/server.rb
|
42
|
+
- lib/anobik/app.rb
|
43
|
+
- lib/anobik/consts.rb
|
44
|
+
- lib/anobik/utils.rb
|
41
45
|
- lib/rack/anobik.rb
|
42
|
-
-
|
46
|
+
- server
|
47
|
+
- config.ru
|
43
48
|
- Manifest
|
44
49
|
- README.rdoc
|
45
50
|
- anobik.gemspec
|
data/lib/anobik.rb
DELETED
@@ -1,75 +0,0 @@
|
|
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
|