Created
September 24, 2012 08:05
-
-
Save sagar-ganatra/3774867 to your computer and use it in GitHub Desktop.
This file contains 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
//Define an Observer class | |
function Observer(name, updateFn) { | |
this.name = name; | |
this.updateFn = updateFn; | |
} | |
//Create an instance of Observer class | |
observerObj = new Observer("observer1", function(msg) { console.log(msg); }); | |
//Create an instance of the Subject and provide the observer object | |
tObject = new Twitter("sagarganatra",observerObj); |
This file contains 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
/* | |
* Define a generic class Subject with two methods | |
* 'addObserver' and 'notifyObserver' | |
*/ | |
function Subject() { | |
this.observerList = []; | |
} | |
Subject.prototype.addObserver = function(observerObj) { | |
this.observerList.push(observerObj) | |
} | |
Subject.prototype.notifyObserver = function(msg) { | |
for (var i = 0, length = this.observerList.length; i < length; i++) { | |
this.observerList[i]['updateFn'](msg); | |
} | |
} |
This file contains 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
/* | |
* Define a Twitter class which extends the Subject class | |
*/ | |
function Twitter(handlerName,observerObj) { | |
//Call the parent class - Subject | |
Subject.call(this); | |
//Handler name is the username for which tweets has to be retrieved | |
this.handlerName = handlerName; | |
//add the observer object | |
this.addObserver(observerObj); | |
//fetch the status messages | |
this.init(); | |
} | |
Twitter.prototype = Object.create(Subject.prototype); | |
//Define the init method that will fetch the status messages | |
Twitter.prototype.init = function() { | |
parent = this; | |
$.ajax({ | |
url: 'http://twitter.com/statuses/user_timeline/' + parent.handlerName + '.json?callback=?', | |
success: function(data) { | |
//iterate over the list of tweets | |
$.each(data,function(index,value) | |
{ | |
var message = value.text; | |
//ignore the reply messages i.e. messages that start with '@' | |
if(message[0] != '@') | |
{ | |
//Check whether the status message contains a link | |
if((index=message.indexOf('http://')) > 0) | |
{ | |
substr1 = message.substr(index); | |
nextIndex = substr1.indexOf(' '); | |
substr2 = (nextIndex > 0)? substr1.substr(0,substr1.indexOf(' ')):substr1.substr(0); | |
pos1 = message.indexOf(substr2); | |
pos2 = pos1 + substr2.length; | |
newMessage = [message.slice(0,pos1),'<a href='+ substr2+ ' target="_blank" >' + substr2 + '</a>',message.slice(pos2)].join(''); | |
//notify the Observer of the new message | |
parent.notifyObserver(newMessage); | |
} | |
else { | |
parent.notifyObserver(message); | |
} | |
} | |
}); | |
}, | |
//if there is an error, throw an Error message | |
error: function(data) { | |
parent.notifyObserver("Error"); | |
}, | |
dataType: 'json' | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment