webfx 0.0.5 → 0.0.6
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/lib/webfx.rb +136 -81
- metadata +5 -5
data/lib/webfx.rb
CHANGED
@@ -1,5 +1,75 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
+
class DeferrableBody
|
4
|
+
include EventMachine::Deferrable
|
5
|
+
|
6
|
+
def call(body)
|
7
|
+
body.each do |chunk|
|
8
|
+
@body_callback.call(chunk)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def each &blk
|
13
|
+
@body_callback = blk
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
class MyResult
|
19
|
+
attr_reader :env
|
20
|
+
|
21
|
+
def initialize(env)
|
22
|
+
@env = env
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
class Context
|
28
|
+
|
29
|
+
def initialize(method, me, env, body)
|
30
|
+
@method = method
|
31
|
+
@me = me
|
32
|
+
@env = env
|
33
|
+
@body = body
|
34
|
+
end
|
35
|
+
|
36
|
+
def respond(response)
|
37
|
+
@env['async.callback'].call [response[0], response[1], @body]
|
38
|
+
@body.call [response[2]]
|
39
|
+
|
40
|
+
if response[0] < 400
|
41
|
+
@body.succeed
|
42
|
+
else
|
43
|
+
@body.fail
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def succeed(result)
|
48
|
+
if result.kind_of? MyResult
|
49
|
+
respond result.env
|
50
|
+
return
|
51
|
+
end
|
52
|
+
|
53
|
+
case @method
|
54
|
+
when 'get', 'post'
|
55
|
+
if result.kind_of? String and ['{', '['].include? result.slice(0, 1)
|
56
|
+
json = result
|
57
|
+
else
|
58
|
+
json = result.to_json
|
59
|
+
end
|
60
|
+
respond [200, {'Content-Type' => 'application/json'}, json]
|
61
|
+
else
|
62
|
+
respond [200, {'Content-Type' => 'application/json'}, {:result => :success}.to_json]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def fail(message)
|
67
|
+
respond [500, {'Content-Type' => 'text/plain'}, message]
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
|
3
73
|
@@routes = {'get' => [], 'post' => [], 'put' => [], 'delete' => []}
|
4
74
|
@@authenticate = false
|
5
75
|
|
@@ -27,97 +97,82 @@ def authenticate(&fn)
|
|
27
97
|
@@authenticate = fn
|
28
98
|
end
|
29
99
|
|
30
|
-
class MyResult
|
31
|
-
attr_reader :env
|
32
|
-
|
33
|
-
def initialize(env)
|
34
|
-
@env = env
|
35
|
-
end
|
36
|
-
end
|
37
100
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
101
|
+
class WebFx
|
102
|
+
AsyncResponse = [-1, {}, []].freeze
|
103
|
+
|
104
|
+
def call(env)
|
105
|
+
|
106
|
+
EventMachine::next_tick do
|
107
|
+
|
108
|
+
request = Rack::Request.new(env)
|
109
|
+
|
110
|
+
def static(type, file, env)
|
111
|
+
begin
|
112
|
+
body = DeferrableBody.new
|
113
|
+
body.call [IO.read(file)]
|
114
|
+
env['async.callback'].call [200, { 'Content-Type' => type, 'Cache-Control' => 'public, max-age=86400' }, body]
|
115
|
+
rescue => e
|
116
|
+
env['async.callback'].call [404, { 'Content-Type' => 'text/plain'}, 'Not found.']
|
117
|
+
end
|
118
|
+
end
|
45
119
|
|
46
|
-
|
47
|
-
|
48
|
-
|
120
|
+
if request.path == '/'
|
121
|
+
static('text/html', 'public/index.html', env)
|
122
|
+
end
|
49
123
|
|
50
|
-
|
51
|
-
|
52
|
-
|
124
|
+
if request.path.match(/(.*).js$/)
|
125
|
+
static('text/javascript', "public/#{$1}.js", env)
|
126
|
+
end
|
53
127
|
|
54
|
-
|
55
|
-
|
56
|
-
|
128
|
+
if request.path.match(/(.*).css$/)
|
129
|
+
static('text/css', "public/#{$1}.css", env)
|
130
|
+
end
|
57
131
|
|
58
|
-
|
59
|
-
|
60
|
-
|
132
|
+
if request.path.match(/(.*).jpg$/)
|
133
|
+
static('image/jpg', ".#{$1}.jpg", env)
|
134
|
+
end
|
61
135
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
136
|
+
params = request.params
|
137
|
+
|
138
|
+
if request.post?
|
139
|
+
if not params['file']
|
140
|
+
body = request.body.read
|
141
|
+
params.merge!(JSON.parse(body)) if body != ''
|
142
|
+
end
|
68
143
|
end
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
144
|
+
|
145
|
+
force_method = params['_method']
|
146
|
+
if force_method
|
147
|
+
method = force_method
|
148
|
+
params.delete('_method')
|
149
|
+
else
|
150
|
+
method = request.request_method
|
151
|
+
end
|
152
|
+
method.downcase!
|
153
|
+
|
154
|
+
me = @@authenticate ? @@authenticate.call(params) : nil
|
155
|
+
ctx = Context.new(method, me, env, DeferrableBody.new)
|
156
|
+
|
157
|
+
found = false
|
79
158
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
return result.env if result.kind_of? MyResult
|
94
|
-
|
95
|
-
case method
|
96
|
-
when 'get', 'post'
|
97
|
-
if result.kind_of? String and ['{', '['].include? result.slice(0, 1)
|
98
|
-
json = result
|
99
|
-
else
|
100
|
-
json = result.to_json
|
101
|
-
end
|
102
|
-
return [200, {'Content-Type' => 'application/json'}, json]
|
103
|
-
else
|
104
|
-
return [200, {'Content-Type' => 'application/json'}, {:result => :success}.to_json]
|
159
|
+
@@routes[method].each do |item|
|
160
|
+
if request.path[1..-1].match(item[:regexp])
|
161
|
+
found = true
|
162
|
+
case $~.length
|
163
|
+
when 1 then item[:fn].call(params, ctx)
|
164
|
+
when 2 then item[:fn].call($1, params, ctx)
|
165
|
+
when 3 then item[:fn].call($1, $2, params, ctx)
|
166
|
+
when 4 then item[:fn].call($1, $2, $3, params, ctx)
|
167
|
+
when 5 then item[:fn].call($1, $2, $3, $4, params, ctx)
|
168
|
+
else item[:fn].call($~, params, ctx)
|
169
|
+
end
|
105
170
|
end
|
106
171
|
end
|
107
|
-
end
|
108
|
-
[404, {'Content-Type' => 'text/plain'}, 'Not found.']
|
109
172
|
|
110
|
-
|
111
|
-
result = {:message => e.message, :callstack => e.backtrace.join("\n")}
|
112
|
-
[500, {'Content-Type' => 'text/plain' }, e.message]
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
|
117
|
-
module WebFx
|
118
|
-
class Application
|
119
|
-
def self.call(env)
|
120
|
-
run_app(env)
|
173
|
+
ctx.respond [404, {'Content-Type' => 'text/plain'}, 'Not found.'] if not found
|
121
174
|
end
|
122
|
-
|
175
|
+
|
176
|
+
AsyncResponse
|
177
|
+
end
|
123
178
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webfx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors: []
|
13
13
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-06 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -62,6 +62,6 @@ rubyforge_project:
|
|
62
62
|
rubygems_version: 1.3.7
|
63
63
|
signing_key:
|
64
64
|
specification_version: 3
|
65
|
-
summary:
|
65
|
+
summary: Async for thin, EventMachine, very minimal, RESTful, json, sinatra-inspired framework.
|
66
66
|
test_files: []
|
67
67
|
|