ext_direct 0.0.0 → 0.1.0
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/README.rdoc +9 -1
- data/VERSION +1 -1
- data/ext_direct.gemspec +8 -2
- data/lib/ext_direct/dispatcher.rb +41 -0
- data/lib/ext_direct/request.rb +48 -0
- data/lib/ext_direct/response.rb +5 -0
- data/lib/ext_direct/router.rb +29 -0
- data/lib/ext_direct/service/base.rb +31 -0
- data/lib/ext_direct/service/provider.rb +33 -0
- data/lib/ext_direct.rb +31 -1
- data/rails/init.rb +5 -0
- metadata +9 -3
data/README.rdoc
CHANGED
@@ -18,7 +18,15 @@ Install as a gem:
|
|
18
18
|
|
19
19
|
gem install ext_direct
|
20
20
|
|
21
|
-
|
21
|
+
Then in "config/environment.rb" require middleware:
|
22
|
+
|
23
|
+
config.middleware.use "ExtDirect::Router", "/direct"
|
24
|
+
|
25
|
+
Great job! Now you are ready to use this library. Remember, this release is tested and runs with Rails 2.3.
|
26
|
+
|
27
|
+
== Example usage
|
28
|
+
|
29
|
+
Of course, I'd drink beer :)
|
22
30
|
|
23
31
|
== Copyright
|
24
32
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.0
|
data/ext_direct.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ext_direct}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Marcin Lewandowski"]
|
12
|
-
s.date = %q{2010-05-
|
12
|
+
s.date = %q{2010-05-28}
|
13
13
|
s.description = %q{The Ext Direct plugin is a simple implementation of Ext Direct router to call server-side methods on the client-side.}
|
14
14
|
s.email = %q{marcin.martio.lewandowski@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,6 +25,12 @@ Gem::Specification.new do |s|
|
|
25
25
|
"ext_direct.gemspec",
|
26
26
|
"install.rb",
|
27
27
|
"lib/ext_direct.rb",
|
28
|
+
"lib/ext_direct/dispatcher.rb",
|
29
|
+
"lib/ext_direct/request.rb",
|
30
|
+
"lib/ext_direct/response.rb",
|
31
|
+
"lib/ext_direct/router.rb",
|
32
|
+
"lib/ext_direct/service/base.rb",
|
33
|
+
"lib/ext_direct/service/provider.rb",
|
28
34
|
"rails/init.rb",
|
29
35
|
"test/ext_direct_test.rb",
|
30
36
|
"test/test_helper.rb",
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ExtDirect
|
2
|
+
class Dispatcher
|
3
|
+
# dispatch to a service method
|
4
|
+
def self.dispatch(request, response)
|
5
|
+
params = request.data # the class to use
|
6
|
+
action = request.action.underscore.to_sym # the method to execute
|
7
|
+
method = request.method.underscore.to_sym # the arguments to be passed to the method
|
8
|
+
tid = request.tid # the transaction id to associate with this request
|
9
|
+
|
10
|
+
begin
|
11
|
+
data = {} # the response will be json encoded
|
12
|
+
|
13
|
+
# the result of the method call
|
14
|
+
result = ExtDirect::Service::Provider.execute(action, method, params)
|
15
|
+
|
16
|
+
if request.event?
|
17
|
+
data["type"] = "event"
|
18
|
+
data["name"] = "message"
|
19
|
+
data["data"] = result
|
20
|
+
else
|
21
|
+
data["type"] = request.type
|
22
|
+
data["action"] = request.action
|
23
|
+
data["method"] = request.method
|
24
|
+
data["tid"] = tid
|
25
|
+
data["result"] = result
|
26
|
+
end
|
27
|
+
rescue Exception => e
|
28
|
+
# a exception on the server-side
|
29
|
+
data["type"] = "exception"
|
30
|
+
data["message"] = e.message
|
31
|
+
data["where"] = e.backtrace
|
32
|
+
ensure
|
33
|
+
if request.rpc? and request.upload?
|
34
|
+
response.write "<html><body><textarea>#{data.to_json}</textarea></body></html>"
|
35
|
+
else
|
36
|
+
response.write data.to_json
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ExtDirect
|
2
|
+
class Request < Rack::Request
|
3
|
+
def action
|
4
|
+
params.fetch("extAction", params.fetch("action", "unknown"))
|
5
|
+
end
|
6
|
+
|
7
|
+
def method
|
8
|
+
params.fetch("extMethod", params.fetch("method", "unknown"))
|
9
|
+
end
|
10
|
+
|
11
|
+
def data
|
12
|
+
if type == :event
|
13
|
+
event_params = params.dup
|
14
|
+
event_params.delete("action")
|
15
|
+
event_params.delete("method")
|
16
|
+
event_params
|
17
|
+
else
|
18
|
+
params.fetch("data", [])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def tid
|
23
|
+
params.fetch("extTID", params.fetch("tid", 0))
|
24
|
+
end
|
25
|
+
|
26
|
+
def type
|
27
|
+
if get?
|
28
|
+
:event
|
29
|
+
elsif post? and params.include? "type"
|
30
|
+
params.fetch("type").to_sym
|
31
|
+
else
|
32
|
+
:unknown
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def event?
|
37
|
+
(type == :event)
|
38
|
+
end
|
39
|
+
|
40
|
+
def rpc?
|
41
|
+
(type == :rpc)
|
42
|
+
end
|
43
|
+
|
44
|
+
def upload?
|
45
|
+
params.include? "extUpload"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module ExtDirect
|
4
|
+
class Router
|
5
|
+
def initialize(app, route_path)
|
6
|
+
@app, @route_path = app, route_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
if env["PATH_INFO"].match("^#{@route_path}")
|
11
|
+
if env["CONTENT_TYPE"] and env["CONTENT_TYPE"].match("^application/json")
|
12
|
+
# support for parsing a POST body data in a JSON format
|
13
|
+
env["rack.input"].rewind
|
14
|
+
env.update("rack.request.form_hash" => JSON.parse(env["rack.input"].read), "rack.request.form_input" => env["rack.input"])
|
15
|
+
end
|
16
|
+
|
17
|
+
request = ExtDirect::Request.new(env)
|
18
|
+
response = ExtDirect::Response.new("", 200, {"Content-Type" => "application/json"})
|
19
|
+
|
20
|
+
# direct requests from the client
|
21
|
+
ExtDirect::Dispatcher.dispatch(request, response)
|
22
|
+
|
23
|
+
response.finish
|
24
|
+
else
|
25
|
+
@app.call(env)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ExtDirect
|
2
|
+
module Service
|
3
|
+
class Base
|
4
|
+
class << self
|
5
|
+
def service(name, &proc)
|
6
|
+
action = action_name_format(self.model_name)
|
7
|
+
method = method_name_format(name)
|
8
|
+
ExtDirect::Service::Provider.register(:remoting, action, method, proc)
|
9
|
+
end
|
10
|
+
|
11
|
+
alias_method :remoting_service, :service
|
12
|
+
|
13
|
+
def polling_service(name, &proc)
|
14
|
+
action = action_name_format(self.model_name)
|
15
|
+
method = method_name_format(name)
|
16
|
+
ExtDirect::Service::Provider.register(:polling, action, method, proc)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def action_name_format(name)
|
22
|
+
name.to_s.underscore.gsub("_service", "")
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_name_format(name)
|
26
|
+
name.to_s.underscore
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ExtDirect
|
2
|
+
module Service
|
3
|
+
class Provider
|
4
|
+
@@store = {}
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def register(type, action, method, proc)
|
8
|
+
action = action.to_sym
|
9
|
+
method = method.to_sym
|
10
|
+
|
11
|
+
@@store[action] = {} unless @@store[action]
|
12
|
+
@@store[action][method] = {} unless @@store[action][method]
|
13
|
+
@@store[action][method][:type] = type
|
14
|
+
@@store[action][method][:proc] = proc
|
15
|
+
end
|
16
|
+
|
17
|
+
def exist?(action, method)
|
18
|
+
(@@store[action.to_sym] and @@store[action.to_sym][method.to_sym])
|
19
|
+
end
|
20
|
+
|
21
|
+
def type_of(action, method)
|
22
|
+
false unless exist?(action, method)
|
23
|
+
@@store[action.to_sym][method.to_sym][:type]
|
24
|
+
end
|
25
|
+
|
26
|
+
def execute(action, method, params = nil)
|
27
|
+
raise "Unknown action or method" unless exist?(action, method)
|
28
|
+
@@store[action.to_sym][method.to_sym][:proc].call(params)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/ext_direct.rb
CHANGED
@@ -1 +1,31 @@
|
|
1
|
-
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2010 Marcin Lewandowski
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
require 'ext_direct/router'
|
25
|
+
require 'ext_direct/dispatcher'
|
26
|
+
require 'ext_direct/request'
|
27
|
+
require 'ext_direct/response'
|
28
|
+
require 'ext_direct/service/base'
|
29
|
+
require 'ext_direct/service/provider'
|
30
|
+
|
31
|
+
module ExtDirect; end
|
data/rails/init.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.0
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Marcin Lewandowski
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-28 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -36,6 +36,12 @@ files:
|
|
36
36
|
- ext_direct.gemspec
|
37
37
|
- install.rb
|
38
38
|
- lib/ext_direct.rb
|
39
|
+
- lib/ext_direct/dispatcher.rb
|
40
|
+
- lib/ext_direct/request.rb
|
41
|
+
- lib/ext_direct/response.rb
|
42
|
+
- lib/ext_direct/router.rb
|
43
|
+
- lib/ext_direct/service/base.rb
|
44
|
+
- lib/ext_direct/service/provider.rb
|
39
45
|
- rails/init.rb
|
40
46
|
- test/ext_direct_test.rb
|
41
47
|
- test/test_helper.rb
|