mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 20:25:48 +00:00
[FEATURE] ripple.txt client
This commit is contained in:
@@ -12,6 +12,7 @@ exports.Meta = require('./meta').Meta;
|
||||
exports.SerializedObject = require('./serializedobject').SerializedObject;
|
||||
exports.RippleError = require('./rippleerror').RippleError;
|
||||
exports.Message = require('./message');
|
||||
exports.Rippletxt = require('./rippletxt');
|
||||
exports.binformat = require('./binformat');
|
||||
exports.utils = require('./utils');
|
||||
exports.Server = require('./server').Server;
|
||||
|
||||
72
src/js/ripple/rippletxt.js
Normal file
72
src/js/ripple/rippletxt.js
Normal file
@@ -0,0 +1,72 @@
|
||||
var request = require('superagent');
|
||||
|
||||
|
||||
function RippleTxt(opts) {
|
||||
this.txts = {};
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Gets the ripple.txt file for the given domain
|
||||
*
|
||||
*/
|
||||
RippleTxt.prototype.get = function (domain, fn) {
|
||||
var self = this;
|
||||
|
||||
if (self.txts[domain]) return fn(null, self.txts[domain]);
|
||||
|
||||
var urls = [
|
||||
'https://ripple.'+domain+'/ripple.txt',
|
||||
'https://www.'+domain+'/ripple.txt',
|
||||
'https://'+domain+'/ripple.txt',
|
||||
'http://ripple.'+domain+'/ripple.txt',
|
||||
'http://www.'+domain+'/ripple.txt',
|
||||
'http://'+domain+'/ripple.txt'
|
||||
].reverse();
|
||||
|
||||
next();
|
||||
function next () {
|
||||
if (!urls.length) return fn(new Error("No ripple.txt found"));
|
||||
var url = urls.pop();
|
||||
|
||||
request.get(url, function(err, resp) {
|
||||
if (err || !resp.text) return next();
|
||||
|
||||
var sections = self.parse(resp.text);
|
||||
self.txts[domain] = sections;
|
||||
fn(null, sections);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* parse a ripple.txt file
|
||||
*
|
||||
*/
|
||||
RippleTxt.prototype.parse = function (txt) {
|
||||
|
||||
txt = txt.replace('\r\n', '\n');
|
||||
txt = txt.replace('\r', '\n');
|
||||
txt = txt.split('\n');
|
||||
|
||||
var currentSection = "", sections = {};
|
||||
for (var i = 0, l = txt.length; i < l; i++) {
|
||||
var line = txt[i];
|
||||
if (!line.length || line[0] === '#') {
|
||||
continue;
|
||||
} else if (line[0] === '[' && line[line.length-1] === ']') {
|
||||
currentSection = line.slice(1, line.length-1);
|
||||
sections[currentSection] = [];
|
||||
} else {
|
||||
line = line.replace(/^\s+|\s+$/g, '');
|
||||
if (sections[currentSection]) {
|
||||
sections[currentSection].push(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sections;
|
||||
}
|
||||
|
||||
module.exports = RippleTxt;
|
||||
@@ -1 +1,18 @@
|
||||
var assert = require('assert');
|
||||
var RippleTxt = require('../src/js/ripple/rippletxt');
|
||||
|
||||
|
||||
describe('Vault Client', function() {
|
||||
|
||||
describe('Ripple Txt', function() {
|
||||
|
||||
it('should get the context of a ripple.txt file from a given domain', function(done){
|
||||
var rt = new RippleTxt();
|
||||
rt.get("ripple.com", function(err, resp){
|
||||
assert.ifError(err);
|
||||
assert.equal(typeof resp, 'object');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user