webfx 0.0.6 → 0.0.7

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 +99 -111
  2. metadata +5 -5
@@ -1,72 +1,86 @@
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)
3
+ class Request
4
+ attr_reader :user, :params, :body, :path, :method
5
+
6
+ def initialize(env)
7
+ request = Rack::Request.new(env)
8
+ params = request.params
9
+
10
+ if params['_method']
11
+ method = params['_method']
12
+ else
13
+ method = request.request_method
9
14
  end
15
+
16
+ @user = @@authenticate ? @@authenticate.call(params) : nil
17
+ @params = params
18
+ @body = params['file'] ? '' : (request.post? or request.put?) ? request.body.read : ''
19
+ @path = request.path
20
+ @method = method.downcase
10
21
  end
11
-
12
- def each &blk
13
- @body_callback = blk
14
- end
15
22
  end
16
23
 
17
24
 
18
- class MyResult
19
- attr_reader :env
20
-
21
- def initialize(env)
22
- @env = env
23
- end
24
- end
25
+ class Response
26
+ attr_writer :status, :header
25
27
 
28
+ class DeferrableBody
29
+ include EM::Deferrable
26
30
 
27
- class Context
31
+ def call(body)
32
+ body.each do |chunk|
33
+ @body_callback.call(chunk)
34
+ end
35
+ end
28
36
 
29
- def initialize(method, me, env, body)
30
- @method = method
31
- @me = me
37
+ def each &blk
38
+ @body_callback = blk
39
+ end
40
+ end
41
+
42
+ def initialize(env)
32
43
  @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]]
44
+ @body = DeferrableBody.new
45
+ @status = nil
46
+ @header = nil
47
+ end
39
48
 
40
- if response[0] < 400
41
- @body.succeed
42
- else
43
- @body.fail
44
- end
49
+ def fail(message)
50
+ @status = 500 if @status.nil?
51
+ @header = {'Content-Type' => 'text/plain'} if @header.nil?
52
+ @env['async.callback'].call [@status, @header, @body]
53
+ @body.call [message]
54
+ @body.fail
45
55
  end
46
-
47
- def succeed(result)
48
- if result.kind_of? MyResult
49
- respond result.env
50
- return
51
- end
52
56
 
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]
57
+ def succeed(body)
58
+ @status = 200 if @status.nil?
59
+ @header = {'Content-Type' => 'application/json'} if @header.nil?
60
+ @env['async.callback'].call [@status, @header, @body]
61
+
62
+ if @header['Content-Type'] == 'application/json' and
63
+ not ((body.kind_of? String) and (['{', '['].include? body.slice(0, 1)))
64
+ body = body.to_json
63
65
  end
66
+
67
+ @body.call [body]
68
+ @body.succeed
64
69
  end
65
-
66
- def fail(message)
67
- respond [500, {'Content-Type' => 'text/plain'}, message]
70
+
71
+ def deleted
72
+ @status = 200 if @status.nil?
73
+ @header = {'Content-Type' => 'text/plain'} if @header.nil?
74
+ @env['async.callback'].call [@status, @header, @body]
75
+ @body.succeed
76
+ end
77
+
78
+ def updated
79
+ @status = 200 if @status.nil?
80
+ @header = {'Content-Type' => 'text/plain'} if @header.nil?
81
+ @env['async.callback'].call [@status, @header, @body]
82
+ @body.succeed
68
83
  end
69
-
70
84
  end
71
85
 
72
86
 
@@ -102,75 +116,49 @@ class WebFx
102
116
  AsyncResponse = [-1, {}, []].freeze
103
117
 
104
118
  def call(env)
105
-
106
- EventMachine::next_tick do
119
+ request = Request.new(env)
120
+ response = Response.new(env)
107
121
 
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
119
-
120
- if request.path == '/'
121
- static('text/html', 'public/index.html', env)
122
- end
123
-
124
- if request.path.match(/(.*).js$/)
125
- static('text/javascript', "public/#{$1}.js", env)
126
- end
127
-
128
- if request.path.match(/(.*).css$/)
129
- static('text/css', "public/#{$1}.css", env)
130
- end
131
-
132
- if request.path.match(/(.*).jpg$/)
133
- static('image/jpg', ".#{$1}.jpg", env)
134
- end
135
-
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
143
- end
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
-
122
+ EM::next_tick do
157
123
  found = false
158
124
 
159
- @@routes[method].each do |item|
125
+ @@routes[request.method].each do |item|
160
126
  if request.path[1..-1].match(item[:regexp])
161
127
  found = true
162
128
  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)
129
+ when 1 then item[:fn].call(request, response)
130
+ when 2 then item[:fn].call($1, request, response)
131
+ when 3 then item[:fn].call($1, $2, request, response)
132
+ when 4 then item[:fn].call($1, $2, $3, request, response)
133
+ when 5 then item[:fn].call($1, $2, $3, $4, request, response)
134
+ else item[:fn].call($~, request, response)
169
135
  end
170
136
  end
171
137
  end
172
138
 
173
- ctx.respond [404, {'Content-Type' => 'text/plain'}, 'Not found.'] if not found
139
+ if not found
140
+ def static(type, file, response)
141
+ begin
142
+ response.header = {'Content-Type' => type, 'Cache-Control' => 'public, max-age=86400'}
143
+ response.succeed(File.read(file))
144
+ rescue => e
145
+ response.fail(e.message)
146
+ end
147
+ end
148
+
149
+ if request.path == '/'
150
+ static('text/html', 'public/index.html', response)
151
+ elsif request.path.match(/(.*).js$/)
152
+ static('text/javascript', "public#{$1}.js", response)
153
+ elsif request.path.match(/(.*).css$/)
154
+ static('text/css', "public#{$1}.css", response)
155
+ elsif request.path.match(/(.*).jpg$/)
156
+ static('image/jpg', ".#{$1}.jpg", response)
157
+ else
158
+ response.status = 404
159
+ response.fail('Not found.')
160
+ end
161
+ end
174
162
  end
175
163
 
176
164
  AsyncResponse
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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
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: 2011-01-06 00:00:00 -08:00
18
+ date: 2011-01-08 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: Async for thin, EventMachine, very minimal, RESTful, json, sinatra-inspired framework.
65
+ summary: Async for thin, EventMachine, heroku, very minimal, RESTful, json, sinatra-inspired framework.
66
66
  test_files: []
67
67