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.
Files changed (2) hide show
  1. data/lib/webfx.rb +136 -81
  2. 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
- def run_app(env)
39
- begin
40
- request = Rack::Request.new(env)
41
-
42
- def static(type, file)
43
- [200, { 'Content-Type' => type, 'Cache-Control' => 'public, max-age=86400' }, IO.read(file)]
44
- end
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
- if request.path == '/'
47
- return static('text/html', 'public/index.html')
48
- end
120
+ if request.path == '/'
121
+ static('text/html', 'public/index.html', env)
122
+ end
49
123
 
50
- if request.path.match(/(.*).js/)
51
- return static('text/javascript', "public/#{$1}.js")
52
- end
124
+ if request.path.match(/(.*).js$/)
125
+ static('text/javascript', "public/#{$1}.js", env)
126
+ end
53
127
 
54
- if request.path.match(/(.*).css/)
55
- return static('text/css', "public/#{$1}.css")
56
- end
128
+ if request.path.match(/(.*).css$/)
129
+ static('text/css', "public/#{$1}.css", env)
130
+ end
57
131
 
58
- if request.path.match(/(.*).jpg/)
59
- return static('image/jpg', ".#{$1}.jpg")
60
- end
132
+ if request.path.match(/(.*).jpg$/)
133
+ static('image/jpg', ".#{$1}.jpg", env)
134
+ end
61
135
 
62
- params = request.params
63
-
64
- if request.post?
65
- if not params['file']
66
- body = request.body.read
67
- params.merge!(JSON.parse(body)) if body != ''
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
- end
70
-
71
- force_method = params['_method']
72
- if force_method
73
- method = force_method
74
- params.delete('_method')
75
- else
76
- method = request.request_method
77
- end
78
- method.downcase!
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
- @@routes[method].each do |item|
81
- if request.path[1..-1].match(item[:regexp])
82
- me = @@authenticate ? @@authenticate.call(params) : nil
83
-
84
- case $~.length
85
- when 1 then result = item[:fn].call(params, me)
86
- when 2 then result = item[:fn].call($1, params, me)
87
- when 3 then result = item[:fn].call($1, $2, params, me)
88
- when 4 then result = item[:fn].call($1, $2, $3, params, me)
89
- when 5 then result = item[:fn].call($1, $2, $3, $4, params, me)
90
- else result = item[:fn].call($~, params, me)
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
- rescue => e
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
- end
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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
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: 2010-12-30 00:00:00 -08:00
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: Very minimal RESTful, json, sinatra-inspired framework.
65
+ summary: Async for thin, EventMachine, very minimal, RESTful, json, sinatra-inspired framework.
66
66
  test_files: []
67
67