Skip to content

Commit

Permalink
http server example
Browse files Browse the repository at this point in the history
allows to use both positional and named parameters
  • Loading branch information
darkdarkdragon committed Dec 9, 2015
1 parent 8d8a850 commit 76866ab
Show file tree
Hide file tree
Showing 10 changed files with 456 additions and 2 deletions.
38 changes: 38 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -20,6 +20,7 @@
"babel-runtime": "^5.5.4",
"bignumber.js": "^2.0.3",
"https-proxy-agent": "^1.0.0",
"jayson": "^1.2.2",
"lodash": "^3.1.0",
"ripple-address-codec": "^2.0.1",
"ripple-binary-codec": "^0.1.0",
Expand Down Expand Up @@ -65,7 +66,8 @@
"test": "istanbul test _mocha",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"lint": "if ! [ -f eslintrc ]; then curl -o eslintrc 'https://raw.githubusercontent.com/ripple/javascript-style-guide/es6/eslintrc'; echo 'parser: babel-eslint' >> eslintrc; fi; eslint -c eslintrc src/",
"perf": "./scripts/perf_test.sh"
"perf": "./scripts/perf_test.sh",
"start": "babel-node scripts/http.js"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions scripts/ci.sh
Expand Up @@ -34,6 +34,7 @@ unittest() {

integrationtest() {
mocha test/integration/integration-test.js
mocha test/integration/http-integration-test.js
}

doctest() {
Expand Down
16 changes: 16 additions & 0 deletions scripts/http.js
@@ -0,0 +1,16 @@
'use strict';

const createHTTPServer = require('../src/index').createHTTPServer;
const port = 5990;
const serverUrl = 'wss://s1.ripple.com';


function main() {
const server = createHTTPServer({server: serverUrl}, port);
server.start().then(() => {
console.log('Server started on port ' + String(port));
});
}


main();
85 changes: 85 additions & 0 deletions src/http.js
@@ -0,0 +1,85 @@
/* eslint-disable new-cap */
'use strict';

const assert = require('assert-diff');
const _ = require('lodash');
const jayson = require('jayson');

const RippleAPI = require('./api').RippleAPI;


function createHTTPServer(options, httpPort) {
const rippleAPI = new RippleAPI(options);

const methodNames = _.filter(_.keys(RippleAPI.prototype), k => {
return typeof RippleAPI.prototype[k] === 'function'
&& k !== 'connect'
&& k !== 'disconnect'
&& k !== 'constructor'
&& k !== 'RippleAPI';
});

function applyPromiseWithCallback(fnName, callback, args_) {
try {
let args = args_;
if (!_.isArray(args_)) {
const fnParameters = jayson.Utils.getParameterNames(rippleAPI[fnName]);
args = fnParameters.map(name => args_[name]);
const defaultArgs = _.omit(args_, fnParameters);
assert(_.size(defaultArgs) <= 1,
'Function must have no more than one default argument');
if (_.size(defaultArgs) > 0) {
args.push(defaultArgs[_.keys(defaultArgs)[0]]);
}
}
Promise.resolve(rippleAPI[fnName].apply(rippleAPI, args))
.then(res => callback(null, res))
.catch(err => {
callback({code: 99, message: err.message, data: {name: err.name}});
});
} catch (err) {
callback({code: 99, message: err.message, data: {name: err.name}});
}
}

const methods = {};
_.forEach(methodNames, fn => {
methods[fn] = jayson.Method((args, cb) => {
applyPromiseWithCallback(fn, cb, args);
}, {collect: true});
});

const server = jayson.server(methods);
let httpServer = null;

return {
server: server,
start: function() {
if (httpServer !== null) {
return Promise.reject('Already started');
}
return new Promise((resolve) => {
rippleAPI.connect().then(() => {
httpServer = server.http();
httpServer.listen(httpPort, resolve);
});
});
},
stop: function() {
if (httpServer === null) {
return Promise.reject('Not started');
}
return new Promise((resolve) => {
rippleAPI.disconnect();
httpServer.close(() => {
httpServer = null;
resolve();
});
});
}
};
}

module.exports = {
createHTTPServer
};
5 changes: 4 additions & 1 deletion src/index.js
Expand Up @@ -2,5 +2,8 @@

module.exports = {
RippleAPI: require('./api').RippleAPI,
RippleAPIBroadcast: require('./broadcast').RippleAPIBroadcast
// Broadcast api is experimental
RippleAPIBroadcast: require('./broadcast').RippleAPIBroadcast,
// HTTP server is experimental
createHTTPServer: require('./http').createHTTPServer
};
74 changes: 74 additions & 0 deletions test/integration/fixtures/get-transaction.json
@@ -0,0 +1,74 @@
{
"jsonrpc": "2.0",
"id": "2",
"result": {
"type": "order",
"address": "rK5j9n8baXfL4gzUoZsfxBvvsv97P5swaV",
"sequence": 7973823,
"id": "4EB6B76237DEEE99F1EA16FAACED2D1E69C5F9CB54F727A4ECA51A08AD3AF466",
"specification": {
"direction": "buy",
"quantity": {
"currency": "USD",
"value": "0.000709756467",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
},
"totalPrice": {
"currency": "JPY",
"value": "0.086630181788",
"counterparty": "r94s8px6kSw1uZ1MV98dhSRTvc6VMPoPcN"
}
},
"outcome": {
"result": "tesSUCCESS",
"timestamp": "2015-12-03T00:53:21.000Z",
"fee": "0.010001",
"balanceChanges": {
"rK5j9n8baXfL4gzUoZsfxBvvsv97P5swaV": [
{
"currency": "XRP",
"value": "-0.010001"
}
]
},
"orderbookChanges": {
"rK5j9n8baXfL4gzUoZsfxBvvsv97P5swaV": [
{
"direction": "buy",
"quantity": {
"currency": "USD",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"value": "0.000709260645"
},
"totalPrice": {
"currency": "JPY",
"counterparty": "r94s8px6kSw1uZ1MV98dhSRTvc6VMPoPcN",
"value": "0.086665436143"
},
"sequence": 7973725,
"status": "cancelled",
"makerExchangeRate": "0.008183892870852266"
},
{
"direction": "buy",
"quantity": {
"currency": "USD",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"value": "0.000709756467"
},
"totalPrice": {
"currency": "JPY",
"counterparty": "r94s8px6kSw1uZ1MV98dhSRTvc6VMPoPcN",
"value": "0.086630181788"
},
"sequence": 7973823,
"status": "created",
"makerExchangeRate": "0.008192946757712049"
}
]
},
"ledgerVersion": 17445469,
"indexInLedger": 2
}
}
}
72 changes: 72 additions & 0 deletions test/integration/fixtures/get-transactions.json
@@ -0,0 +1,72 @@
{
"jsonrpc": "2.0",
"id": "3",
"result": [
{
"type": "order",
"address": "rpP2JgiMyTF5jR5hLG3xHCPi1knBb1v9cM",
"sequence": 3372206,
"id": "848397FA686BD4A59F91EC1F4DE717360470EE8BD67CAA01D5FD333EDA8D97B3",
"specification": {
"direction": "buy",
"quantity": {
"currency": "JPY",
"value": "27865.90216965619",
"counterparty": "rJRi8WW24gt9X85PHAxfWNPCizMMhqUQwg"
},
"totalPrice": {
"currency": "XRP",
"value": "40000"
}
},
"outcome": {
"result": "tesSUCCESS",
"fee": "0.011",
"balanceChanges": {
"rpP2JgiMyTF5jR5hLG3xHCPi1knBb1v9cM": [
{
"currency": "XRP",
"value": "-0.011"
}
]
},
"orderbookChanges": {
"rpP2JgiMyTF5jR5hLG3xHCPi1knBb1v9cM": [
{
"direction": "buy",
"quantity": {
"currency": "JPY",
"counterparty": "rJRi8WW24gt9X85PHAxfWNPCizMMhqUQwg",
"value": "27880.6855384734"
},
"totalPrice": {
"currency": "XRP",
"value": "40000"
},
"sequence": 3372204,
"status": "cancelled",
"makerExchangeRate": "0.697017138461835"
},
{
"direction": "buy",
"quantity": {
"currency": "JPY",
"counterparty": "rJRi8WW24gt9X85PHAxfWNPCizMMhqUQwg",
"value": "27865.90216965619"
},
"totalPrice": {
"currency": "XRP",
"value": "40000"
},
"sequence": 3372206,
"status": "created",
"makerExchangeRate": "0.6966475542414048"
}
]
},
"ledgerVersion": 17533547,
"indexInLedger": 12
}
}
]
}
6 changes: 6 additions & 0 deletions test/integration/fixtures/index.js
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
getTransaction: require('./get-transaction'),
getTransactions: require('./get-transactions')
};

0 comments on commit 76866ab

Please sign in to comment.