merb-ext-direct 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-01-14
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/merb-ext-direct.rb
7
+ lib/merb-ext-direct/xrequest.rb
8
+ lib/merb-ext-direct/xresponse.rb
9
+ lib/merb-ext-direct/xexception.rb
10
+ lib/merb-ext-direct/mixins/remoting_provider.rb
11
+ script/console
12
+ script/destroy
13
+ script/generate
14
+ test/test_helper.rb
15
+ test/test_merb-ext-direct.rb
@@ -0,0 +1,7 @@
1
+
2
+ For more information on merb-ext-direct, see http://merb-ext-direct.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
@@ -0,0 +1,48 @@
1
+ = merb-ext-direct
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/merb-ext-direct'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('merb-ext-direct', Merb::Ext::Direct::VERSION) do |p|
7
+ p.developer('Chris Scott', 'chris.scott@extjs.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,13 @@
1
+ module Merb
2
+ module Ext
3
+ module Direct
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
8
+
9
+ require File.join(File.dirname(__FILE__), 'merb-ext-direct', 'xrequest')
10
+ require File.join(File.dirname(__FILE__), 'merb-ext-direct', 'xresponse')
11
+ require File.join(File.dirname(__FILE__), 'merb-ext-direct', 'xexception')
12
+ require File.join(File.dirname(__FILE__), 'merb-ext-direct', 'mixins', 'remoting_provider')
13
+
@@ -0,0 +1,107 @@
1
+ ##
2
+ # Ext::Direct::RemotingProvider
3
+ # @mixin
4
+ # This is a mixin containing just one action, "rpc", which you can include into any Merb controller to turn it into an
5
+ # Ext.Direct router
6
+ # @usage include Ext::Direct::RemotingProvider
7
+ # @author Chris Scott
8
+ # @TODO before_actions and forms
9
+ #
10
+ module Merb::Ext::Direct
11
+ module RemotingProvider
12
+
13
+ # standard ruby method called when some class does:
14
+ # include Ext::Direct::RemotingProvider
15
+ def self.included(base)
16
+ # must explicity specify controller-actions to make callable. security-wise, this is a *good* thing.
17
+ # just one action is added.
18
+ base.show_action(:rpc)
19
+ end
20
+
21
+ ##
22
+ # rpc
23
+ # remote procedure call handler for Ext.direct requests.
24
+ #
25
+ def rpc
26
+ if !request.ajax? && !params["extAction"]
27
+ return "Ext::Direct::RemotingProvider#rpc -- This method is an ajax-handler only"
28
+ end
29
+
30
+ is_form = false
31
+ is_upload = false
32
+ if params["extAction"] && params["extMethod"]
33
+ # create fake response here.
34
+ is_form = true
35
+ is_upload = params.delete("extUpload") == 'true'
36
+
37
+ request = {
38
+ "xcontroller" => params.delete("extAction"),
39
+ "xaction" => params.delete("extMethod"),
40
+ "type" => "rpc",
41
+ "id" => params.delete("id"),
42
+ "tid" => params.delete("extTID"),
43
+ "format" => params.delete("format"),
44
+ "data" => params
45
+ }
46
+ params.delete('controller')
47
+ params.delete('action')
48
+
49
+ res = "<html><body><textarea>#{handle_request(request).gsub(/&quot;/, "\&quot;")}</textarea></body></html>"
50
+ Merb.logger.info('OUTPUT: ' + res)
51
+ res
52
+
53
+ elsif (params[:inflated_object])
54
+ # multiple requests found. I'm not sure how this "inflated_object" mechanism works. This is Merb magic?
55
+ responses = []
56
+ params[:inflated_object].each do |req|
57
+ responses << handle_request(req)
58
+ end
59
+ # controllers always return a string, so each response has already been json-encoded.
60
+ # since we're dealing with multiple requests here, we have to manually string-concat-wrap the retured
61
+ # string-of-json.
62
+ #
63
+ return "[" + responses.join(',') + "]"
64
+ else
65
+ return handle_request(params)
66
+ end
67
+ end
68
+
69
+ ##
70
+ #
71
+ protected
72
+ #
73
+ ##
74
+
75
+ ##
76
+ # handle_request
77
+ # # @throws XException when anything goes wrong to gracefully handle on client.
78
+ # @see Merb::Controller#part method, google: "merb parts". A Merb "part" is a kind of sub-controller
79
+ # with its onwn view capabilites but cannot be directly routed-to. Using part() method here is similar
80
+ # to using Ext.getCmp(id), eg: part(req.controller => req.action) ~ Ext.getCmp('thing').doSomething(req);
81
+ # However, unlike Ext.getCmp(), part(controller => action) returns a string to be added to the render stread
82
+ # and not a component instance, just as all merb render actions.
83
+ # Finally, we append the string "Part" at the end of the controller name in order to prevent having
84
+ # to suffix our javascript names accordingly. "Part" here is necessarily needed in order to prevent
85
+ # namespace clash with related parent-controller or model. In Merb, given a controller named Products,
86
+ # we'll have a model named Product along with a part named ProductPart, ProductViewPart,
87
+ # ProductGridPart, etc
88
+ #
89
+ def handle_request(req)
90
+ req = XRequest.new(req)
91
+
92
+ begin
93
+ # here be your Ext.direct.Transaction as a ruby Hash.
94
+ # turn it into an offical ruby XRequest instance.
95
+ # now constantize the "controller" and send "action" to it.
96
+ return part(Extlib::Inflection.constantize(req.controller) => req.action, :xrequest => req)
97
+ rescue XException => e
98
+ # caught an XException...return an Ext.Direct-friendly XExceptionResponse
99
+ return XExceptionResponse.new(req, e).to_json
100
+ rescue StandardError => e
101
+ # might be an invalid controller and/or action if we got here.
102
+ # we can be more specific here and trap on NameError if we wish, intead of base StandardError
103
+ return XExceptionResponse.new(req, e).to_json
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,36 @@
1
+ ##
2
+ # XException
3
+ # General exception class for all Ext application exceptions.
4
+ # @author Chris Scott
5
+ #
6
+ class XException < StandardError
7
+ end
8
+
9
+ ##
10
+ # XExceptionResponse
11
+ # A special extension of XResponse for returning responses where an Exception was raised. includes a back-trace in the response which
12
+ # should probably be turned on with a debug switch only.
13
+ # @author Chris Scott
14
+ #
15
+ class XExceptionResponse < XResponse
16
+ attr_accessor :where
17
+
18
+ ##
19
+ # initialize
20
+ # @param {XRequest}
21
+ # @param {StandardError}
22
+ #
23
+ def initialize(req, e)
24
+ super(req)
25
+ @type = 'exception'
26
+ @message = e.message
27
+ @where = e.backtrace
28
+ end
29
+
30
+ def to_h
31
+ data = super
32
+ data[:type] = 'exception'
33
+ data[:where] = @where.join("\n")
34
+ data
35
+ end
36
+ end
@@ -0,0 +1,31 @@
1
+ ###
2
+ # XRequest
3
+ # A standard response class suitable for Ext.Direct requests.
4
+ # @author Chris Scott
5
+ #
6
+ class XRequest
7
+ attr_reader :id, :tid, :controller, :action, :type, :params
8
+
9
+ def initialize(params)
10
+ # TODO: simply setting @id, @params
11
+ @id = (params["id"].to_i > 0) ? params["id"].to_i : (params["data"].kind_of?(Array) && (params["data"].first.kind_of?(Integer) || params["data"].first.nil?)) ? params["data"].shift: nil
12
+ @tid = params["tid"]
13
+ @type = params["type"]
14
+ @params = (params["data"].kind_of?(Array) && params["data"].length == 1 && params["data"].first.kind_of?(Hash)) ? params["data"].first : params["data"] || []
15
+ @controller = params["xcontroller"]
16
+ @action = params["xaction"]
17
+ end
18
+
19
+ ##
20
+ # arg
21
+ # return a request argument at index. can be used to access either [] or {}
22
+ # @param {String/Integer} index
23
+ # @raises XException if params doesn't exist.
24
+ #
25
+ def arg(index)
26
+ if params[index].nil?
27
+ raise XException.new("Attempt to access unknown request argument '#{index.to_s}' on transaction #{@tid}")
28
+ end
29
+ @params[index]
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ ###
2
+ # XResponse
3
+ # A standard response class suitable for Ext.Direct AJAX responses.
4
+ # @author Chris Scott
5
+ #
6
+ class XResponse
7
+ attr_accessor :type, :status, :errors, :success, :message, :result
8
+ attr_reader :tid
9
+
10
+ def initialize(req)
11
+ @tid = req.tid
12
+ @type = req.type
13
+ @status = false
14
+ @message = ''
15
+ @result = []
16
+ @errors = []
17
+ end
18
+
19
+ def to_h
20
+ {:tid => @tid, :status => @status, :type => @type, :message => @message, :result => @result, :errors => @errors}
21
+ end
22
+
23
+ def to_json
24
+ self.to_h.to_json
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/merb-ext-direct.rb'}"
9
+ puts "Loading merb-ext-direct gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/merb-ext-direct'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestMerb-ext-direct < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb-ext-direct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Scott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-14 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ description: FIX (describe your package)
36
+ email:
37
+ - chris.scott@extjs.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - PostInstall.txt
46
+ - README.rdoc
47
+ files:
48
+ - History.txt
49
+ - Manifest.txt
50
+ - PostInstall.txt
51
+ - README.rdoc
52
+ - Rakefile
53
+ - lib/merb-ext-direct.rb
54
+ - lib/merb-ext-direct/xrequest.rb
55
+ - lib/merb-ext-direct/xresponse.rb
56
+ - lib/merb-ext-direct/xexception.rb
57
+ - lib/merb-ext-direct/mixins/remoting_provider.rb
58
+ - script/console
59
+ - script/destroy
60
+ - script/generate
61
+ - test/test_helper.rb
62
+ - test/test_merb-ext-direct.rb
63
+ has_rdoc: true
64
+ homepage: FIX (url)
65
+ post_install_message: PostInstall.txt
66
+ rdoc_options:
67
+ - --main
68
+ - README.rdoc
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project: merb-ext-direct
86
+ rubygems_version: 1.3.1
87
+ signing_key:
88
+ specification_version: 2
89
+ summary: FIX (describe your package)
90
+ test_files:
91
+ - test/test_merb-ext-direct.rb
92
+ - test/test_helper.rb