Created
June 2, 2013 04:46
-
-
Save alucky0707/5692647 to your computer and use it in GitHub Desktop.
Node.jsでJSON-RPCする ref: http://qiita.com/items/68d7f319cece7c2d4891
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
var | |
async = require('async'), | |
ideone = require('./jsonrpc').createClient('http://ideone.com/api/1/service.json'); | |
var | |
user = 'your user', | |
pass = 'your pass', | |
lang = {'python': 4, 'ruby': 17}['ruby'], | |
src = 'puts "Hello, World!"', | |
input = ''; | |
/* | |
ideone.testFunction(user, pass)(function(err, res) { | |
console.log(res); | |
}); | |
*/ | |
async.waterfall([ | |
function(next) { | |
console.log('createSubmission'); | |
ideone.createSubmission(user, pass, src, lang, input, true, false)(next); | |
}, | |
function loop(data, next) { | |
var | |
link = data.link; | |
console.log('getSubmissionStatus: %s', link); | |
ideone.getSubmissionStatus(user, pass, link)(function(err, res) { | |
console.log(res); | |
if(err){ | |
next(err); | |
return; | |
} | |
if(res.status == 0){ | |
next(null, link); | |
return; | |
} | |
setTimeout(function(){ | |
loop(data, next); | |
}, 100); | |
}); | |
}, | |
function(link, next) { | |
console.log('getSubmissionDetails'); | |
ideone.getSubmissionDetails(user, pass, link, false, false, true, true, true)(next); | |
}, | |
], function(err, result) { | |
if(err) { | |
console.log(err); | |
return; | |
} | |
console.log(result); | |
}); |
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
var | |
request = require('request'); | |
function JsonRpcClient(url) { | |
return Proxy.create({ | |
get: function(_, method) { | |
var | |
requester = function() { | |
var | |
args = Array.prototype.slice.call(arguments, 0); | |
return call.bind(this, method, args); | |
}; | |
requester.call = function(params, callback) { | |
call(method, params, callback); | |
}; | |
return requester; | |
} | |
}); | |
function call(method, params, callback) { | |
var | |
body = JSON.stringify({ | |
jsonrpc: '2.0', | |
id: +(new Date), | |
method: method, | |
params: params, | |
}); | |
request(url, { | |
method: 'POST', | |
body: body, | |
header: { | |
'Content-Length': Buffer.byteLength(body, encoding='utf8'), | |
}, | |
json: true, | |
}, function(err, res, body) { | |
if(body && body.error) { | |
err = new Error(body.error.msg); | |
err.data = body.error; | |
} | |
callback(err, body && body.result); | |
}); | |
} | |
} | |
module.exports = { | |
createClient : JsonRpcClient, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment