This is a Rack middleware that facilitates the creation of protocol-agnostic RPC servers. The current implementation provides support for JSON-RPC 2.0 and XML-RPC.
- Handles JSON-RPC and XML-RPC requests with the same code.
- Compatible with any Rack application and any Rack-based framework.
- Provides Rails-style controller filtering for your RPC methods.
require 'rack/rpc'
class Server < Rack::RPC::Server
def hello_world
"Hello, world!"
end
rpc 'hello_world' => :hello_world
end
require 'rack/rpc'
class Server < Rack::RPC::Server
before_filter :check_auth
def hello_world
"Hello, world!"
end
rpc 'hello_world' => :hello_world
private
def check_auth
raise "Not authorized" unless authorized
end
end
require 'rack/rpc'
class Server < Rack::RPC::Server
before_filter :check_auth, :only => :super_secret_hello_world do
raise "Not authorized" unless authorized
end
def hello_world
"Hello, world!"
end
rpc 'hello_world' => :hello_world
def super_secret_hello_world
'super_secret_hello_world'
end
rpc 'super_secret_hello_world' => :super_secret_hello_world
end
# config.ru
use Rack::RPC::Endpoint, Server.new
run MyApplication
# config.ru
use Rack::RPC::Endpoint, Server.new, :path => '/api'
run MyApplication
The :only
and :except
options for filters can take a single method or an
array of methods.
You can halt execution in a filter by raising an exception. An error response will be returned with the exception's message set as the error object's message text.
By default, methods will only be invoked on POST
requests to "/rpc". The
default path can be overridden by sending a :path
option when creating
your middleware (see example above).
The protocol used is determined by the CONTENT_TYPE
header
("application/xml" and "text/xml" for XML and "application/json" for JSON).
The recommended installation method is via RubyGems. To install the latest official release of the gem, do:
% [sudo] gem install rack-rpc
To get a local working copy of the development repository, do:
% git clone git://github.com/datagraph/rack-rpc.git
Alternatively, download the latest development version as a tarball as follows:
% wget http://github.com/datagraph/rack-rpc/tarball/master
This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying {file:UNLICENSE} file.