Skip to content

Instantly share code, notes, and snippets.

@laser
Last active August 29, 2015 14:00
Show Gist options
  • Save laser/11303914 to your computer and use it in GitHub Desktop.
Save laser/11303914 to your computer and use it in GitHub Desktop.
Using HTTP as Transport in RPC System (Server)
#!/usr/bin/env ruby
require 'sinatra'
require './calculator'
calculator = Calculator.new
post '/calc' do
req = JSON.parse(request.body.read)
# call a method on the calculator instance, passing along params
args = req['params'].unshift(req['method'])
result = calculator.send *args
# HTTP stuff
status 200
headers 'Content-Type' => 'application/json'
# make sure to encode response as JSON
JSON.generate({
'id' => req['id'],
'result' => result,
'jsonrpc' => '2.0'
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment