Last active
August 29, 2015 14:10
-
-
Save kevinswiber/d131a63966314b318774 to your computer and use it in GitHub Desktop.
Using revolt with refine.
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 select = require('refine').select; | |
| var revolt = require('revolt'); | |
| revolt() | |
| .get('http://zetta-cloud-2.herokuapp.com') | |
| .flatMap(deserialize) | |
| .flatMap(function(entity) { | |
| var detroitLink = select(entity.links) | |
| .where('rel').contains('http://rels.zettajs.io/peer') | |
| .and('title').equals('Detroit')[0]; | |
| return revolt() | |
| .get(detroitLink.href) | |
| .flatMap(deserialize) | |
| }) | |
| .subscribe(function(detroit) { | |
| console.log(detroit.entities.length); | |
| }); | |
| function deserialize(env) { | |
| return revolt.buffer(env.response) | |
| .map(function(data) { | |
| return JSON.parse(data.toString()); | |
| }) | |
| }; |
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 select = require('refine').select; | |
| var revolt = require('revolt'); | |
| var client = revolt() | |
| .use(function(handle) { | |
| handle('response', function(env, next) { | |
| revolt.buffer(env.response) | |
| .subscribe(function(data) { | |
| env.entity = JSON.parse(data.toString()); | |
| next(env); | |
| }); | |
| }); | |
| }); | |
| client | |
| .get('http://zetta-cloud-2.herokuapp.com') | |
| .flatMap(function(env) { | |
| var detroitLink = select(env.entity.links) | |
| .where('rel').contains('http://rels.zettajs.io/peer') | |
| .and('title').equals('Detroit')[0]; | |
| return client | |
| .get(detroitLink.href) | |
| }) | |
| .subscribe(function(env) { | |
| console.log(env.entity.entities.length); | |
| }); |
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 select = require('refine').select; | |
| var revolt = require('revolt'); | |
| var gzip = require('revolt-gzip'); | |
| var client = revolt() | |
| .use(gzip) | |
| .use(function(handle) { | |
| handle('response', function(pipeline) { | |
| return pipeline.flatMap(function(env) { | |
| if (env.response.body) { | |
| return revolt.buffer(env.response.body) | |
| .map(function(data) { | |
| env.response.body = JSON.parse(data.toString()); | |
| return env; | |
| }); | |
| } else { | |
| return pipeline; | |
| } | |
| }); | |
| }); | |
| }); | |
| client | |
| .get('http://localhost:3000') | |
| .flatMap(function(env) { | |
| var entity = env.response.body; | |
| var serverLink = select(entity.links) | |
| .where('rel').contains('http://rels.zettajs.io/server')[0]; | |
| return client | |
| .get(serverLink.href) | |
| }) | |
| .flatMap(function(env) { | |
| var entity = env.response.body; | |
| var heartbeat = entity.entities.filter(function(device) { | |
| return device.properties.type === 'heartbeat'; | |
| })[0]; | |
| var link = select(heartbeat.links).where('rel').contains('self')[0]; | |
| return client.get(link.href); | |
| }) | |
| .flatMap(function(env) { | |
| var entity = env.response.body; | |
| var pulse = select(entity.links) | |
| .where('rel').contains('http://rels.zettajs.io/object-stream') | |
| .and('title').equals('pulse')[0]; | |
| return client.get(pulse.href); | |
| }) | |
| .subscribe(function(env) { | |
| env.response.on('message', function(msg) { | |
| console.log(msg); | |
| }); | |
| }); |
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 Rx = require('rx'); | |
| var revolt = require('revolt'); | |
| var gzip = require('revolt-gzip'); | |
| var jsonParser = require('revolt-json-parser'); | |
| var client = revolt() | |
| .use(gzip) | |
| .use(jsonParser) | |
| client | |
| .get('http://localhost:3000') | |
| .flatMap(function(env) { | |
| var entity = env.response.body; | |
| var serverLink = entity.links.filter(function(link) { | |
| return link.rel.indexOf('http://rels.zettajs.io/server') > -1; | |
| })[0]; | |
| return client | |
| .get(serverLink.href) | |
| }) | |
| .flatMap(function(env) { | |
| var entity = env.response.body; | |
| var heartbeat = entity.entities.filter(function(device) { | |
| return device.properties.type === 'heartbeat'; | |
| })[0]; | |
| var link = heartbeat.links.filter(function(link) { | |
| return link.rel.indexOf('self') > -1; | |
| })[0]; | |
| return client.get(link.href); | |
| }) | |
| .flatMap(function(env) { | |
| var entity = env.response.body; | |
| var pulse = entity.links.filter(function(link) { | |
| return link.rel.indexOf('http://rels.zettajs.io/object-stream') > -1 | |
| && link.title === 'pulse'; | |
| })[0]; | |
| return client.get(pulse.href); | |
| }) | |
| .flatMap(function(env) { | |
| return Rx.Observable.create(function(observer) { | |
| env.response.on('message', function(msg) { | |
| observer.onNext(msg); | |
| }); | |
| env.response.on('close', function() { | |
| observer.onCompleted(); | |
| }); | |
| env.response.on('error', function(err) { | |
| observer.onError(err); | |
| }); | |
| }); | |
| }) | |
| .subscribe(function(msg) { | |
| console.log(msg); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment