Skip to content
Browse files

finish initial routing implementation and specs

  • Loading branch information...
1 parent 4592962 commit c01b29475c8d265ed21b3424595e05692691d154 @chriskite committed Apr 4, 2012
Showing with 53 additions and 28 deletions.
  1. +1 −1 Rakefile
  2. +5 −1 lib/jimson/router.rb
  3. +20 −1 lib/jimson/router/map.rb
  4. +26 −24 lib/jimson/server.rb
  5. +1 −1 spec/server_spec.rb
View
2 Rakefile
@@ -14,7 +14,7 @@ end
task :default => :rspec
-require 'rake/rdoctask'
+require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
View
6 lib/jimson/router.rb
@@ -4,7 +4,11 @@ module Jimson
class Router
extend Forwardable
- def_delegator :@map, :handler_for_method
+ def_delegators :@map, :handler_for_method,
+ :root,
+ :namespace,
+ :jimson_methods,
+ :strip_method_namespace
def initialize
@map = Map.new
View
21 lib/jimson/router/map.rb
@@ -22,7 +22,7 @@ def root(handler)
# Define the handler for a namespace
#
def namespace(ns, handler)
- @routes[ns] = handler
+ @routes[ns.to_s] = handler
end
#
@@ -33,6 +33,25 @@ def handler_for_method(method)
@routes[ns]
end
+ #
+ # Strip off the namespace part of a method and return the bare method name
+ #
+ def strip_method_namespace(method)
+ # Currently doesn't support nested namespaces, so just return the last part
+ method.split('.').last
+ end
+
+ #
+ # Return an array of all methods on handlers in the map, fully namespaced
+ #
+ def jimson_methods
+ arr = @routes.keys.map do |ns|
+ prefix = (ns == '' ? '' : "#{ns}.")
+ @routes[ns].class.jimson_exposed_methods.map { |method| prefix + method }
+ end
+ arr.flatten
+ end
+
end
end
end
View
50 lib/jimson/server.rb
@@ -3,6 +3,7 @@
require 'rack/response'
require 'multi_json'
require 'jimson/handler'
+require 'jimson/router'
require 'jimson/server/error'
module Jimson
@@ -11,12 +12,12 @@ class Server
class System
extend Handler
- def initialize(handler)
- @handler = handler
+ def initialize(router)
+ @router = router
end
def listMethods
- @handler.class.jimson_exposed_methods
+ @router.jimson_methods
end
def isAlive
@@ -26,10 +27,10 @@ def isAlive
JSON_RPC_VERSION = '2.0'
- attr_accessor :handler, :host, :port, :opts
+ attr_accessor :router, :host, :port, :opts
#
- # +handler+ is an instance of the class to expose as a JSON-RPC server
+ # +router_or_handler+ is an instance of Jimson::Router or extends Jimson::Handler
#
# +opts+ may include:
# * :host - the hostname or ip to bind to
@@ -38,8 +39,17 @@ def isAlive
#
# Remaining options are forwarded to the underlying Rack server.
#
- def initialize(handler, opts = {})
- @handler = handler
+ def initialize(router_or_handler, opts = {})
+ if !router_or_handler.is_a?(Router)
+ # arg is a handler, wrap it in a Router
+ @router = Router.new
+ @router.root router_or_handler
+ else
+ # arg is a router
+ @router = router_or_handler
+ end
+ @router.namespace 'system', System.new(@router)
+
@host = opts.delete(:host) || '0.0.0.0'
@port = opts.delete(:port) || 8999
@opts = opts
@@ -153,30 +163,22 @@ def create_response(request)
end
def dispatch_request(method, params)
- # normally route requests to the user-suplied handler
- handler = @handler
-
- # switch to the System handler if a system method was called
- sys_regex = /^system\./
- if method =~ sys_regex
- handler = System.new(@handler)
- # remove the 'system.' prefix before from the method name
- method.gsub!(sys_regex, '')
- end
-
- method = method.to_s
+ method_name = method.to_s
+ handler = @router.handler_for_method(method_name)
+ method_name = @router.strip_method_namespace(method_name)
- if !handler.class.jimson_exposed_methods.include?(method) \
- || !handler.respond_to?(method)
+ if handler.nil? \
+ || !handler.class.jimson_exposed_methods.include?(method_name) \
+ || !handler.respond_to?(method_name)
raise Server::Error::MethodNotFound.new(method)
end
if params.nil?
- return handler.send(method)
+ return handler.send(method_name)
elsif params.is_a?(Hash)
- return handler.send(method, params)
+ return handler.send(method_name, params)
else
- return handler.send(method, *params)
+ return handler.send(method_name, *params)
end
end
View
2 spec/server_spec.rb
@@ -353,7 +353,7 @@ def post_json(hash)
resp = MultiJson.decode(last_response.body)
resp.should == {
'jsonrpc' => '2.0',
- 'result' => ['subtract', 'sum', 'notify_hello', 'update', 'get_data', 'ugly_method'].sort,
+ 'result' => ["get_data", "notify_hello", "subtract", "sum", "ugly_method", "update", "system.isAlive", "system.listMethods"],
'id' => 1
}
end

0 comments on commit c01b294

Please sign in to comment.
Something went wrong with that request. Please try again.