Skip to content

Latest commit

 

History

History
64 lines (41 loc) · 1.57 KB

README.md

File metadata and controls

64 lines (41 loc) · 1.57 KB

jsonrpc

Build Status

A Ruby implementation of JSON-RPC 2.0. It's as simple as a parser and a response builder.

Installation

Add this line to your application's Gemfile:

gem 'jsonrpc', git: 'https://github.com/codingstones/jsonrpc'

And then execute:

$ bundle

(We'll publish the gem to RubyGems soon).

Usage

How to build a JSON-RPC request:

require 'jsonrpc'

# First, we need a json with valid JSON-RPC format
p = {
    jsonrpc: '2.0',
    params: [1, 2],
    method: 'sum',
    id: 'foo'
}
request_body = JSON.generate(p)


# Let's parse it!
parser = JsonRPC::Parser.new
request = parser.parse(request_body)
request.invalid?  # false (validates the JSON-RPC format)
request.version   # 2.0
request.params    # [1, 2]
request.id        # 'foo'
request.method    # 'sum'

How to build a JSON-RPC response:

response = JsonRPC::Response.new(request_id: 'foo', result: 3)
response.to_json # {:jsonrpc=>"2.0", :id=>"foo", :result=>3}

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/codingstones/jsonrpc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.