ruby_ext_direct 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +4 -0
  3. data/README +5 -0
  4. data/Rakefile +1 -0
  5. data/examples/API/Gemfile +29 -0
  6. data/examples/API/app/actions/api_action.rb +18 -0
  7. data/examples/API/app/actions/home_action.rb +11 -0
  8. data/examples/API/app/actions/router_action.rb +18 -0
  9. data/examples/API/application.rb +37 -0
  10. data/examples/API/config/database.yml +6 -0
  11. data/examples/API/config/routes.rb +6 -0
  12. data/examples/API/config.ru +25 -0
  13. data/examples/API/public/NamesApp/app/store/NamesStore.js +48 -0
  14. data/examples/API/public/NamesApp/app/view/namesWindow.js +21 -0
  15. data/examples/API/public/NamesApp/app/view/ui/namesWindow.js +58 -0
  16. data/examples/API/public/NamesApp/designer.html +19 -0
  17. data/examples/API/public/NamesApp/designer.js +32 -0
  18. data/examples/API/public/NamesApp/designer_includeOrder.txt +3 -0
  19. data/examples/API/public/namesApp.xds +200 -0
  20. data/examples/rack/config.ru +40 -0
  21. data/examples/rack/exposed_classes/countries.rb +10 -0
  22. data/examples/rack/exposed_classes/names.rb +82 -0
  23. data/examples/rack/html/.DS_Store +0 -0
  24. data/examples/rack/html/NamesApp/app/store/NamesStore.js +56 -0
  25. data/examples/rack/html/NamesApp/app/view/namesWindow.js +49 -0
  26. data/examples/rack/html/NamesApp/app/view/ui/namesWindow.js +92 -0
  27. data/examples/rack/html/NamesApp/designer.html +19 -0
  28. data/examples/rack/html/NamesApp/designer.js +32 -0
  29. data/examples/rack/html/NamesApp/designer_includeOrder.txt +3 -0
  30. data/examples/rack/html/names.xds +275 -0
  31. data/lib/ext_direct/api.rb +53 -0
  32. data/lib/ext_direct/router.rb +97 -0
  33. data/lib/ext_direct/version.rb +3 -0
  34. data/lib/ext_direct.rb +13 -0
  35. data/ruby_ext_direct.gemspec +24 -0
  36. data/test/test_ext_direct.rb +129 -0
  37. metadata +92 -0
@@ -0,0 +1,97 @@
1
+ module ExtDirect
2
+ class Router
3
+ def self.route(request)
4
+ result = nil
5
+ params = self.parse_request(request)
6
+
7
+ if params.is_a?(Array)
8
+ result = []
9
+ params.each do |p|
10
+ result << self.call_method(p)
11
+ end
12
+ else
13
+ result = self.call_method(params)
14
+ end
15
+
16
+ result
17
+ end
18
+
19
+ private
20
+ def self.call_method(params)
21
+ data = ''
22
+ #get Class
23
+ klass = self.class.const_get(params[:klass_name])
24
+ #call method on class
25
+ klass_instance = klass.new
26
+ method_to_call = klass_instance.method(params[:method_to_call_name].to_sym)
27
+
28
+ if method_to_call.parameters.size > 0
29
+ data = method_to_call.call(params[:args])
30
+ else
31
+ data = method_to_call.call
32
+ end
33
+
34
+ response = {
35
+ :type => params[:call_type],
36
+ :tid => params[:tid],
37
+ :action => params[:klass_name],
38
+ :method => params[:method_to_call_name],
39
+ :result => data
40
+ }
41
+ rescue Exception => e
42
+ response = {
43
+ :type => 'exception',
44
+ :message => e.message,
45
+ :where => e.backtrace.join("\n")
46
+ }
47
+ end
48
+
49
+ def self.parse_request(request)
50
+ params = nil
51
+ xparams_raw = JSON::parse(request)
52
+
53
+ if xparams_raw.is_a? Array
54
+ params = []
55
+ xparams_raw.each do |p|
56
+ params << self.xparams_to_params(p)
57
+ end
58
+ else
59
+ params = self.xparams_to_params(xparams_raw)
60
+ end
61
+
62
+ params
63
+ end
64
+
65
+ def self.xparams_to_params(xparams)
66
+ params = {}
67
+
68
+ xparams_action = 'action'
69
+ xparams_method = 'method'
70
+ xparams_tid = 'tid'
71
+ xparams_type = 'type'
72
+
73
+ #determine if transaction is a Form post
74
+ form_transaction = false
75
+ if xparams.include?('extTID')
76
+ form_transaction = true
77
+ xparams_action = 'extAction'
78
+ xparams_method = 'extMethod'
79
+ xparams_tid = 'extTID'
80
+ end
81
+
82
+ params.store(:tid, xparams.delete(xparams_tid))
83
+ params.store(:klass_name, xparams.delete(xparams_action))
84
+ params.store(:method_to_call_name, xparams.delete(xparams_method))
85
+ params.store(:call_type, xparams.delete(xparams_type))
86
+
87
+ if form_transaction
88
+ params.store(:args, xparams)
89
+ else
90
+ params.store(:args, xparams['data'])
91
+ end
92
+
93
+ params
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module ExtDirect
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ext_direct.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "ext_direct/version"
2
+ require "ext_direct/api"
3
+ require "ext_direct/router"
4
+
5
+ class String
6
+ def classify_it
7
+ self.split('_').map{|c| c.capitalize}.join('')
8
+ end
9
+ end
10
+
11
+ module ExtDirect
12
+
13
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ext_direct/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ruby_ext_direct"
7
+ s.version = ExtDirect::VERSION
8
+ s.authors = ["Mehmet Celik"]
9
+ s.email = ["mehmet@celik.be"]
10
+ s.homepage = ""
11
+ s.summary = %q{Ext.direct}
12
+ s.description = %q{Ext.direct}
13
+
14
+ s.rubyforge_project = "ruby_ext_direct"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,129 @@
1
+ $LOAD_PATH << '../lib'
2
+ require 'rubygems'
3
+ require 'shoulda'
4
+ require 'test/unit'
5
+ require 'ext_direct'
6
+
7
+ class MyClass
8
+ def method_without_arguments
9
+ "method_without_arguments called"
10
+ end
11
+
12
+ def method_with_arguments(arg1)
13
+ "method_with_arguments called: #{arg1}"
14
+ end
15
+ end
16
+
17
+
18
+
19
+ class TestExtDirect < Test::Unit::TestCase
20
+ context 'Test ext_direct gem' do
21
+ setup do
22
+ @request_without_arguments = "{\"action\":\"MyClass\",\"method\":\"method_without_arguments\",\"data\":null,\"type\":\"rpc\",\"tid\":1}"
23
+ @request_with_arguments = "{\"action\":\"MyClass\",\"method\":\"method_with_arguments\",\"data\":[42],\"type\":\"rpc\",\"tid\":1}"
24
+ @request_with_form_arguments = "{\"extAction\":\"MyClass\",\"extMethod\":\"method_with_arguments\",\"extTID\":1, \"a\":1,\"b\":2}"
25
+ @request_multiple_actions = "[{\"action\":\"MyClass\",\"method\":\"method_with_arguments\",\"data\":[{\"id\":1,\"name\":\"Mehmet Celik1\",\"address\":\"Country road 8, Takemehome\",\"country\":\"be\"}],\"type\":\"rpc\",\"tid\":2},{\"action\":\"MyClass\",\"method\":\"method_without_arguments\",\"data\":null,\"type\":\"rpc\",\"tid\":3}]"
26
+ end
27
+
28
+ should "parse request without arguments" do
29
+ expected = {:tid=>1,
30
+ :klass_name=>"MyClass",
31
+ :method_to_call_name=>"method_without_arguments",
32
+ :call_type=>"rpc",
33
+ :args=>nil}
34
+
35
+ result = ExtDirect::Router.send(:parse_request, @request_without_arguments)
36
+
37
+ assert_equal(expected, result)
38
+ end
39
+
40
+ should "parse request with arguments" do
41
+ expected = {:tid=>1,
42
+ :klass_name=>"MyClass",
43
+ :method_to_call_name=>"method_with_arguments",
44
+ :call_type=>"rpc",
45
+ :args=>[42]}
46
+
47
+ result = ExtDirect::Router.send(:parse_request, @request_with_arguments)
48
+
49
+ assert_equal(expected, result)
50
+ end
51
+
52
+ should "parse request with form arguments" do
53
+ expected = {:tid=>1,
54
+ :klass_name=>"MyClass",
55
+ :method_to_call_name=>"method_with_arguments",
56
+ :call_type=>nil, #TODO figure out which type this is!!!
57
+ :args=>{"a"=>1, "b"=>2}}
58
+ result = ExtDirect::Router.send(:parse_request, @request_with_form_arguments)
59
+
60
+ assert_equal(expected, result)
61
+ end
62
+
63
+ should "parse multiple actions" do
64
+ expected = [{:tid=>2,
65
+ :klass_name=>"MyClass",
66
+ :method_to_call_name=>"method_with_arguments",
67
+ :call_type=>"rpc",
68
+ :args=>
69
+ [{"id"=>1,
70
+ "name"=>"Mehmet Celik1",
71
+ "address"=>"Country road 8, Takemehome",
72
+ "country"=>"be"}]},
73
+ {:tid=>3,
74
+ :klass_name=>"MyClass",
75
+ :method_to_call_name=>"method_without_arguments",
76
+ :call_type=>"rpc",
77
+ :args=>nil}]
78
+ result = ExtDirect::Router.send(:parse_request, @request_multiple_actions)
79
+
80
+ assert_equal(Array, result.class)
81
+ assert_equal(2, result.size)
82
+ assert_equal(expected, result)
83
+
84
+ end
85
+
86
+ context 'Calling methods' do
87
+ setup do
88
+ ExtDirect::Api.expose MyClass
89
+ end
90
+
91
+ should "call method without parameters" do
92
+ expected = "method_without_arguments called"
93
+
94
+ result = ExtDirect::Router.route(@request_without_arguments)[:result]
95
+
96
+ assert_equal(expected, result)
97
+ end
98
+
99
+ should "call method with parameters" do
100
+ expected = 'method_with_arguments called: [42]'
101
+ result = ExtDirect::Router.route(@request_with_arguments)[:result]
102
+ assert_equal(expected, result)
103
+ end
104
+
105
+ should "call method with form parameters" do
106
+ expected = "method_with_arguments called: {\"a\"=>1, \"b\"=>2}"
107
+ result = ExtDirect::Router.route(@request_with_form_arguments)[:result]
108
+ assert_equal(expected, result)
109
+ end
110
+
111
+ should "call multiple actions" do
112
+ expected = [{:type=>"rpc",
113
+ :tid=>2,
114
+ :action=>"MyClass",
115
+ :method=>"method_with_arguments",
116
+ :result=>
117
+ "method_with_arguments called: [{\"id\"=>1, \"name\"=>\"Mehmet Celik1\", \"address\"=>\"Country road 8, Takemehome\", \"country\"=>\"be\"}]"},
118
+ {:type=>"rpc",
119
+ :tid=>3,
120
+ :action=>"MyClass",
121
+ :method=>"method_without_arguments",
122
+ :result=>"method_without_arguments called"}]
123
+
124
+ result = ExtDirect::Router.route(@request_multiple_actions)#[:result]
125
+ assert_equal(expected, result)
126
+ end
127
+ end
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_ext_direct
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Mehmet Celik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-10-21 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Ext.direct
18
+ email:
19
+ - mehmet@celik.be
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - README
30
+ - Rakefile
31
+ - examples/API/Gemfile
32
+ - examples/API/app/actions/api_action.rb
33
+ - examples/API/app/actions/home_action.rb
34
+ - examples/API/app/actions/router_action.rb
35
+ - examples/API/application.rb
36
+ - examples/API/config.ru
37
+ - examples/API/config/database.yml
38
+ - examples/API/config/routes.rb
39
+ - examples/API/public/NamesApp/app/store/NamesStore.js
40
+ - examples/API/public/NamesApp/app/view/namesWindow.js
41
+ - examples/API/public/NamesApp/app/view/ui/namesWindow.js
42
+ - examples/API/public/NamesApp/designer.html
43
+ - examples/API/public/NamesApp/designer.js
44
+ - examples/API/public/NamesApp/designer_includeOrder.txt
45
+ - examples/API/public/namesApp.xds
46
+ - examples/rack/config.ru
47
+ - examples/rack/exposed_classes/countries.rb
48
+ - examples/rack/exposed_classes/names.rb
49
+ - examples/rack/html/.DS_Store
50
+ - examples/rack/html/NamesApp/app/store/NamesStore.js
51
+ - examples/rack/html/NamesApp/app/view/namesWindow.js
52
+ - examples/rack/html/NamesApp/app/view/ui/namesWindow.js
53
+ - examples/rack/html/NamesApp/designer.html
54
+ - examples/rack/html/NamesApp/designer.js
55
+ - examples/rack/html/NamesApp/designer_includeOrder.txt
56
+ - examples/rack/html/names.xds
57
+ - lib/ext_direct.rb
58
+ - lib/ext_direct/api.rb
59
+ - lib/ext_direct/router.rb
60
+ - lib/ext_direct/version.rb
61
+ - ruby_ext_direct.gemspec
62
+ - test/test_ext_direct.rb
63
+ has_rdoc: true
64
+ homepage: ""
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: ruby_ext_direct
87
+ rubygems_version: 1.6.2
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Ext.direct
91
+ test_files:
92
+ - test/test_ext_direct.rb