Skip to content

hallelujah/json-rpc

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Json Rpc

Implementation of JSON RPC 2.0 protocol. It allows you to create easily json rpc server.

Usage

Simple Rack example:

require 'json-rpc'

class SyncApp
  include JsonRpc
  def call env
    result = rpc_call(env) { |e|
      logger.info "#{e}"
    }
    result
  end

  def rpc_sum a, b
    a + b
  end
end
run SyncApp.new

Asynchronous Event Machine example:

require 'json-rpc'

class AsyncApp
  include JsonRpc
  AsyncResponse = [-1, {}, []].freeze
  def call env
    rpc_call(env)
  end

  def rpc_sum a, b
    result = Rpc::AsyncResult.new
    EventMachine::next_tick do
      result.reply a + b
      result.succeed
    end
    result
  end
end

Test it works:

$ thin -R example.ru -p 4242 -d start
$ curl "http://localhost:4242/rpc?jsonrpc=2.0&method=sum&params=%5B21%2C21%5D"
{"jsonrpc":"2.0","id":0,"result":42}

License

Copyright 2011 Helios Technologies Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Json RPC Module for Rack application

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%