Class: Rack::RPC::Endpoint::JSONRPC::Server
- Inherits:
-
Object
- Object
- Rack::RPC::Endpoint::JSONRPC::Server
- Defined in:
- lib/rack/rpc/endpoint/jsonrpc.rb
Overview
Instance Method Summary (collapse)
- - (Rack::Response) execute(request)
-
- (Server) initialize(server, options = {})
constructor
A new instance of Server.
- - (String) process(input, context = nil)
- - (Array) process_batch(batch, context = nil)
- - (Hash) process_request(struct, context = nil)
Constructor Details
- (Server) initialize(server, options = {})
Returns a new instance of Server
17 18 19 |
# File 'lib/rack/rpc/endpoint/jsonrpc.rb', line 17 def initialize(server, = {}) @server, @options = server, .dup end |
Instance Method Details
- (Rack::Response) execute(request)
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rack/rpc/endpoint/jsonrpc.rb', line 24 def execute(request) # Store the request so that it can be accessed from the server methods: @server.request = request if @server.respond_to?(:request=) request_body = request.body.read request_body.force_encoding(Encoding::UTF_8) if request_body.respond_to?(:force_encoding) # Ruby 1.9+ Rack::Response.new([process(request_body, request)], 200, { 'Content-Type' => (request.content_type || CONTENT_TYPE).to_s, }) end |
- (String) process(input, context = nil)
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rack/rpc/endpoint/jsonrpc.rb', line 40 def process(input, context = nil) response = nil begin response = case (json = JSON.parse(input)) when Array then process_batch(json, context) when Hash then process_request(json, context) end rescue JSON::ParserError => exception response = JSONRPC::Response.new response.error = JSONRPC::ParseError.new(:message => exception.to_s) end response.to_json + "\n" end |
- (Array) process_batch(batch, context = nil)
58 59 60 |
# File 'lib/rack/rpc/endpoint/jsonrpc.rb', line 58 def process_batch(batch, context = nil) batch.map { |struct| process_request(struct, context) } end |
- (Hash) process_request(struct, context = nil)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rack/rpc/endpoint/jsonrpc.rb', line 66 def process_request(struct, context = nil) response = JSONRPC::Response.new begin request = JSONRPC::Request.new(struct, context) response.id = request.id raise ::TypeError, "invalid JSON-RPC request" unless request.valid? case operator = @server.class[request.method] when nil raise ::NoMethodError, "undefined operation `#{request.method}'" when Class # a Rack::RPC::Operation subclass response.result = operator.new(request).execute else response.result = @server.__send__(operator, *request.params) end rescue ::TypeError => exception # FIXME response.error = JSONRPC::ClientError.new(:message => exception.to_s) rescue ::NoMethodError => exception response.error = JSONRPC::NoMethodError.new(:message => exception.to_s) rescue ::ArgumentError => exception response.error = JSONRPC::ArgumentError.new(:message => exception.to_s) rescue ::Rack::RPC::Error => exception response.error = JSONRPC::Error.new(:message => exception.to_s, :code => exception.code, :data => exception.data) rescue => exception response.error = JSONRPC::InternalError.new(:message => exception.to_s) end response.to_hash.delete_if { |k, v| v.nil? } end |