Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Added using XML document directly from XHR object in RSS module.
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 committed Mar 31, 2015
1 parent bbbb349 commit 7e1db77
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
10 changes: 6 additions & 4 deletions src/js/modules/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
* @param {String} method
* @param {String} url
* @param {Object} data
* @param {Boolean} [plainXhr] Should return plain XHR in promise instead of data?
* @returns {Promise}
*/
request: function(method, url, data) {
request: function(method, url, data, plainXhr) {
var promise = new Promise(),
xhr;

Expand All @@ -47,7 +48,7 @@
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
promise.resolve(xhr.responseText);
promise.resolve(plainXhr ? xhr : xhr.responseText);
} else {
promise.reject(xhr);
}
Expand All @@ -65,10 +66,11 @@
*
* @param {String} url
* @param {Object} data
* @param {Boolean} [plainXhr]
* @returns {Promise}
*/
'get': function(url, data) {
return this.request('GET', url, data);
'get': function(url, data, plainXhr) {
return this.request('GET', url, data, plainXhr);
},

/**
Expand Down
18 changes: 9 additions & 9 deletions src/js/modules/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
* Understands RSS structure
* Currently it allow only one channel
*
* @param {String} str XML string
* @param {String|Document} xml XML string or document
* @constructor
*/
function RSS(str) {
this.data = this.parse(str);
function RSS(xml) {
this.data = this.parse(xml);
}

/**
* Parse XML string to structure
* Parse XML string or document to structure
*
* @param {String} str
* @param {String|Document} xml
* @returns {Object}
*/
RSS.prototype.parse = function(str) {
var result = parseXML(str, {
RSS.prototype.parse = function(xml) {
var result = (typeof xml === 'string' ? parseXML.fromString : parseXML)(xml, {
rss: {
channel: {
item: {
Expand Down Expand Up @@ -71,8 +71,8 @@
RSS.fromURL = function(url) {
var promise = new Promise();

request.get(url).then(function(data) {
promise.resolve(new RSS(data));
request.get(url, null, true).then(function(xhr) {
promise.resolve(new RSS(xhr.responseXML));
}, function(xhr) {
promise.reject(xhr);
});
Expand Down
15 changes: 13 additions & 2 deletions src/js/modules/xml-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,27 @@
return result;
}

/**
* Expose function to parse XML document to object
*
* @param {Document} xmlDoc
* @param {Object} [definition]
* @returns {Object}
*/
module.exports = function parseXML(xmlDoc, definition) {
return domListToObject([ xmlDoc.documentElement ], definition);
};

/**
* Expose function to parse XML string to object
*
* @param {String} str
* @param {Object} [definition]
* @returns {Object}
*/
module.exports = function parseXML(str, definition) {
module.exports.fromString = function parseXMLFromString(str, definition) {
var xml = (new DOMParser()).parseFromString(str, 'text/xml');

return domListToObject([ xml.documentElement ], definition);
return module.exports(xml, definition);
};
}());
4 changes: 2 additions & 2 deletions test/spec/modules/xml-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
};

expect(parseXML(xmlFile)).toEqual(expectedResult);
expect(parseXML.fromString(xmlFile)).toEqual(expectedResult);
});

it('should parse XML file to JSON with definition', function() {
Expand All @@ -44,7 +44,7 @@
]
}
}
}, obj = parseXML(xmlFile2, {
}, obj = parseXML.fromString(xmlFile2, {
xml: {
item: {
'@type': 'array'
Expand Down

0 comments on commit 7e1db77

Please sign in to comment.