forked from zenterp/ripple-account-monitor
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add account listener with tests for connecting and receivin…
…g payment callback events
- Loading branch information
Steven Zeiler
committed
Jan 3, 2014
1 parent
0e8eb5d
commit 492aadc
Showing
6 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
WebSocket = require('ws') | ||
EventEmitter = require('events').EventEmitter | ||
|
||
function AccountListener(options) { | ||
this.accounts = options.accounts | ||
this.url = options.url || 'wss://s1.ripple.com' | ||
this.webSocket = new WebSocket(this.url) | ||
} | ||
|
||
AccountListener.prototype.__proto__ = EventEmitter.prototype | ||
|
||
AccountListener.prototype.connect = function() { | ||
try { | ||
this.webSocket.on('open', this.handlers.onOpen) | ||
this.webSocket.on('message', this.handlers.onMessage) | ||
this.webSocket.on('close', this.handlers.onClose) | ||
} catch(e) { | ||
delete this.webSocket | ||
this.webSocket = new WebSocket(this.url) | ||
this.connect() | ||
} | ||
accounts = JSON.stringify(this.accounts) | ||
this.webSocket.send('{"command":"subscribe","id":0,"accounts":'+accounts+'}') | ||
} | ||
|
||
AccountListener.prototype.handlers = (function(){ | ||
listener = this | ||
|
||
function onOpen() { | ||
console.log('onOpen') | ||
} | ||
function onClose(){ | ||
console.log('onClose'); | ||
} | ||
function onMessage(data){ | ||
try { | ||
payment = new Payment(data) | ||
listener.emit('payment', payment.toJSON()) | ||
} catch(e) { | ||
console.log(e) | ||
} | ||
} | ||
|
||
return { onOpen: onOpen, onClose: onClose, onMessage: onMessage } | ||
})() | ||
|
||
module.exports = AccountListener |
File renamed without changes.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
AccountListener = require('../lib/account_listener.js') | ||
Fixtures = require('./fixtures') | ||
Payment = require('../lib/payment') | ||
sinon = require('sinon') | ||
assert = require('assert') | ||
|
||
|
||
describe('AccountListener', function(){ | ||
it('#connect should listen for account activity', function(){ | ||
listener = new AccountListener({ | ||
accounts: ['r4EwBWxrx5HxYRyisfGzMto3AT8FZiYdWk'] | ||
}) | ||
|
||
desiredMessage = '{"command":"subscribe","id":0,"accounts":["r4EwBWxrx5HxYRyisfGzMto3AT8FZiYdWk"]}' | ||
|
||
listener.webSocket.send = sinon.spy() | ||
listener.connect() | ||
assert(listener.webSocket.send.calledWith(desiredMessage)) | ||
}) | ||
|
||
it('#connect should listen on multiple accounts', function(){ | ||
listener = new AccountListener({ | ||
accounts: [ | ||
'r4EwBWxrx5HxYRyisfGzMto3AT8FZiYdWk', | ||
'rHKueQebtVU9cEamhBbMV8AEViqKjXcBcB' | ||
] | ||
}) | ||
|
||
desiredMessage = '{"command":"subscribe","id":0,"accounts":["r4EwBWxrx5HxYRyisfGzMto3AT8FZiYdWk","rHKueQebtVU9cEamhBbMV8AEViqKjXcBcB"]}' | ||
|
||
listener.webSocket.send = sinon.spy() | ||
listener.connect() | ||
assert(listener.webSocket.send.calledWith(desiredMessage)) | ||
}) | ||
|
||
it('should notify of an incoming payment with an event', function(done){ | ||
callback = sinon.spy() | ||
payment = new Payment(Fixtures.IouToSameIou) | ||
|
||
listener.on('payment', function(payment){ | ||
assert.equal(payment.fromAddress, 'rHKueQebtVU9cEamhBbMV8AEViqKjXcBcB') | ||
assert.equal(payment.toCurrency, 'XAG') | ||
done() | ||
}) | ||
|
||
listener.webSocket.onmessage(Fixtures.IouToSameIou) | ||
}) | ||
}) |
This file contains 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