FeedValidator.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. Ext.define('FV.lib.FeedValidator', {
  2. singleton: true,
  3. /**
  4. * @cfg {String} url The url to validate feeds on
  5. */
  6. url: 'feed-proxy.php',
  7. /**
  8. * Validates a given feed's formating by fetching it and ensuring it is well formed
  9. * @param {FV.model.Feed} feed The feed to validate
  10. */
  11. validate: function(feed, options) {
  12. options = options || {};
  13. Ext.applyIf(options, {
  14. scope: this,
  15. success: Ext.emptyFn,
  16. failure: Ext.emptyFn
  17. });
  18. Ext.Ajax.request({
  19. url: this.url,
  20. params: {
  21. feed: feed.get('url')
  22. },
  23. scope: this,
  24. success: function(response) {
  25. if (this.checkResponse(response, feed)) {
  26. options.success.call(options.scope, feed);
  27. }
  28. },
  29. failure: function() {
  30. options.failure.call(options.scope);
  31. }
  32. });
  33. },
  34. /**
  35. * @private
  36. * Validates that a response contains a well-formed feed
  37. * @param {Object} response The response object
  38. */
  39. checkResponse: function(response, feed) {
  40. var dq = Ext.DomQuery,
  41. url = feed.get('url'),
  42. xml, channel, title;
  43. try {
  44. xml = response.responseXML;
  45. channel = xml.getElementsByTagName('channel')[0];
  46. if (channel) {
  47. title = dq.selectValue('title', channel, url);
  48. return true;
  49. }
  50. } catch(e) {
  51. }
  52. return false;
  53. }
  54. });