Project Y

Crazy dog writing code for fun

FeedMe - RSS reader

September11


K so this is the code for that FeedMe Class:

FeedMe.js

var FeedMe = new Class({
    Implements : [Options, Events],
    options : {
        template : '
<div class="item">#link##title#
<div class="item-desc">#desc#</div>
</div>
',
        container : 'FeedMeContainer'
    },
    initialize : function(options) {
        this.setOptions(options)
    },
    eat : function(url) {
        this.fireEvent('start');
        new Request({
            url : url,
            onSuccess : this._eat.bind(this),
            onFailure : function(response) {this.fireEvent('failure', response)}.bind(this),
            onException : function(headerName, error) {this.fireEvent('exception', [headerName, error])}.bind(this)
        }).send();
    },
    _eat : function(responseText, responseXml) {
        this.fireEvent('success', responseXml);
        //console.log(responseText+' '+responseXml)
        //try {
            this._digest(responseXml);
 
    },
    _digest : function(responseXml) {
        this.fireEvent('processing');
        this.channel = new FeedMe.Channel(responseXml);
        this.outputContainer = $(this.options.container);
        ["title","link","description","pubDate","copyright"].each(function(item) {
            //console.log('chan-'+item);
            this.outputContainer.getElement('.chan-'+item).set('html',this.channel.get(item));
        }.bind(this));
        var img = '', image = this.channel.image, src = image.get('src');
        if (src != null) {
            var title = image.get('description');
            img = '<img title="'+title+'" src="'+src+'" alt="'+title+'" />';
        }
        this.outputContainer.getElement('.chan-image-link').set('html', img);
 
        this.itemsHtml = '';
        this.channel.items.each(function(item) {
            this.itemsHtml += this.options.template.replace(
            '#link#', '
<div class="item-link">'+item.get('link')+'</div>
').replace(
            '#title#', '
<div class="item-title">'+item.get('title')+'</div>
').replace(
            '#desc#', '
<div class="item-desc">'+item.get('description')+'</div>
')
        }.bind(this));
        this.outputContainer.getElement('.chan-items').set('html', this.itemsHtml);
        this.fireEvent('done');
    }
});
 
FeedMe.Node = new Class({
    initialize : function(element) {
        this.element = element;
    },
    get : function(prop) {
        if (this.element) {
            if (prop != 'value') return this.element.get(prop);
            else this.element.childNodes[0].nodeValue;
        }
        return null;
    }
});
FeedMe.BaseItem = new Class({
    get : function(prop) {
        var elm = this.element.getElement(prop);
        if (elm != null) return elm.childNodes[0].nodeValue;
        return '';
    }
});
FeedMe.Item = new Class({
    Extends : FeedMe.BaseItem,
    initialize : function(element) {
        this.element = element;
        this.category = new FeedMe.Node(this.element.getElement('category'));
        this.enclosure = new FeedMe.Node(this.element.getElement('enclosure'));
        this.guid = new FeedMe.Node(this.element.getElement('guid'));
        this.source = new FeedMe.Node(this.element.getElement('source'));
    }
 
});
FeedMe.Channel = new Class({
    Extends : FeedMe.BaseItem,
    initialize : function(xmlDoc) {
        this.xmlDoc = xmlDoc;
        this.element = this.xmlDoc.getElement('channel');
        this.items = new Array();
        this.xmlDoc.getElements('item').each(function(item) {
            this.items.push(new FeedMe.Item(item));
        }, this);
        this.category = new FeedMe.Node(this.element.getElement('category'));
        this.image = new FeedMe.Node(this.element.getElement('image'));
    }
 
});

mootools.ext.js just a quick extension for show and hide functionality

Element.implement({
	visible: function() {
    	return this.style.display != 'none';
  	},
	toggle: function() {
		if (this.visible())this.hide();
		else this.show();
		return this;
	},
	hide: function() {
		this.style.display = 'none';
		return this;
	},
	show: function() {
		this.style.display = '';
		return this;
	},
	smoothScrollTo: function(options){
		var options = $extend({},options||{elementOffset:0});
		try {
			var scrollY = (window.pageYOffset)?window.pageYOffset:document.documentElement.scrollTop;
			if ((!options.minPageOffset &amp;&amp; !options.maxPageOffset) ||
			(options.minPageOffset &amp;&amp; scrollY &gt; options.minPageOffset) ||
			(options.maxPageOffset &amp;&amp; scrollY &lt; options.maxPageOffset))
			if (!__globalSmoothScroll) __globalSmoothScroll = new Fx.Scroll(window);
			__globalSmoothScroll.toElement(this);
 
		} catch(e) {Logger.log('smoothScrollTo: '+e.message);}
		return this;
	}
});

Test html file:

<html>
	<head>
		<title>FeedMe</title>
		<script src="/js/mootools.core.js" type="text/javascript"></script>
		<script src="/js/mootools.more.js" type="text/javascript"></script>
		<script src="/js/mootools.ext.js" type="text/javascript"></script>
		<script src="/js/feedme.js" type="text/javascript"></script>
		<link rel="stylesheet" href="/css/layout.css" />
		<script type="text/javascript">
		var feedMe = null;
		window.addEvent('domready', function() {
			feedMe = new FeedMe().addEvents({
				start : function() {showStatus('fetching..', 'start');$('FeedMeContainer').hide();},
				success : showStatus.pass('eating..','success'),
				processing : showStatus.pass('digesting..','processing'),
				done : function() {showStatus('dropping kids..','done');showResult.delay(1000);}
			});
			$('sendit').addEvent('click', function() {
				var url = $('rssurl').value;
				if (url == '' || !/http/.test(url)) {
					alert('come on foo!');
					return;
				}
				feedMe.eat("/bites/proxy.php?mimetype=text/xml&url="+encodeURIComponent(url));
			})
		});
		function showStatus(msg, className) {return $('feedingStatus').set('html',msg).set('class', className).show();}
		function showResult(){
			$('FeedMeContainer').show();
			$('feedingStatus').hide();
		};
		</script>
		</head>
	<body>
	<div class="container">
	<input type="text" value="http://news.google.com/news?ned=us&hl=en&q=dog&output=rss" id="rssurl" /> <input type="button" id="sendit" value="Feed Me!!"/>
	<div id="feedingStatus"></div>
	<div id="FeedMeContainer" class="rss" style="display:none;">
		<div id="chan-title"></div>
		<div id="chan-link"></div>
		<div id="chan-description"></div>
		<a href="javascript:void(0);" id="chan-image-link"></a>
		<div id="chan-items"></div>
		<div id="chan-pubDate"></div>
		<div id="chan-copyright"></div>
	</div>
	</div>
	</body>
</html>

You will need a proxy to run outside script within your website or you’ll run into cross-domain issues

Damn I gotta go run with the Boss now I hate when he does that to me.

Later fools!

posted under Bites
4 Comments to

“FeedMe - RSS reader”

  1. On September 12th, 2008 at 8:00 am Rodrigo Dlugokenski Says:

    Loved it…!

    Thanks. Kisses =*

    I will be spending days writing these, since i’m starter in javascript. I loved the ’simplicity with power’ of mootools, but I’m very slow yet, because i need to read docs all the times I write a code.

    Thanks for help. With some tweaks this will fit perfectly my needs.
    You have my hand if you need some php help ;)

    Beers :P

  2. On September 17th, 2008 at 2:49 pm brad Says:

    Hi thanks for this,

    but I have one problem I cannot get it to work.. i do not understand the proxy part so I might be doing it worng, do I have to change anything in the proxy.php file?

    also when my page is fetching the data i get the error:

    this.element is null;
    this.category = new FeedMe.Node(this.element.getElement(’category’));

    can you help please? thanks

  3. On September 17th, 2008 at 3:01 pm Zoey Says:

    Brad,
    Do you have an example of it running that I can take a look at?
    The error you are getting is most likely due to the fact that the proxy is returning an empty result so I wouldn’t worry about that.
    I’ll post the proxy code that I use in a few try that instead of yours.

    Peace

  4. On September 17th, 2008 at 3:12 pm Project Y » Blog Archive » Proxy is not working….. Says:

    [...] proxy don’t work do you? So I guess peeps like doggy style code and are trying to use the FeedMe Rss Reader. Since there are some proxy issues this is the code of the one I use: <?php // PHP Proxy example [...]

Email will not be published

Website example

Your Comment:

 
Bones
Previous Next
Latest on Tue, 05:12

Kititaxog: mm... informative ))

Yvon: The people in such cases, said so - Avos will be alive, maybe pomrem.

Rhino: A coding dog?? That's nothing. You should see a coding rhino!!

Dudeee: that foo is funny he must have been drunk!!

Zoey: what up with this wall thing is it site wide?

» Leave a bone