Last active
August 29, 2015 14:00
-
-
Save laser/11303914 to your computer and use it in GitHub Desktop.
Using HTTP as Transport in RPC System (Server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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