Displaying Friendfeed Items: hello.js

// Read my friendfeed items and spew them onto the page

goog.provide('lahosken.ffhello.send');

goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.net.Jsonp');

// Ask friendfeed to send us some awesome data via JSON request
lahosken.ffhello.send = function() {
  var jsonp = new goog.net.Jsonp(
    'http://friendfeed.com/api/feed/user/lahosken');
  var payload = { 'format': 'json' };
  jsonp.send(payload, 
             lahosken.ffhello.handleResponse_, 
             lahosken.ffhello.handleError_);
};

// This utility function returns a paragraph node based on one 
// "entry" from the friendfeed stream.  Kind of an ugly mess of
// if/else; might be a smart way to straighten it up...
lahosken.ffhello.createDomlet_ = function(entry) {
  var p = goog.dom.createDom('p');
  if (entry.service.id == 'twitter') { 
    if (entry.title[0] == '@') { return null; }
    goog.dom.appendChild(p, goog.dom.createTextNode(entry.title + ' '));
    goog.dom.appendChild(p, 
    goog.dom.createDom('a', {'href': 'http://twitter.com/lahosken'}, '>'));
  } else if (entry.service.id == 'blog') {
    goog.dom.appendChild(p, goog.dom.createTextNode(entry.title + ' '));
    goog.dom.appendChild(p, 
    goog.dom.createDom('a', {'href': entry.link}, '>'));
  } else if (entry.service.id == 'googlereader') {
    goog.dom.appendChild(p, goog.dom.createDom('b', {}, 'Link: '));
    goog.dom.appendChild(p, goog.dom.createTextNode(entry.title + ' '));
    goog.array.forEach(entry.comments, function(comment) {
      if (comment.user && comment.user.nickname && comment.body &&
          comment.user.nickname == 'lahosken') {
        goog.dom.appendChild(p, goog.dom.createDom('b', {}, 
                                                   comment.body + ' '));
      }});
    goog.dom.appendChild(p, goog.dom.createDom('a', {'href': entry.link}, '>'));
  } else {
    goog.dom.appendChild(p, goog.dom.createTextNode(entry.title + ' ')); 
    goog.dom.appendChild(p, 
          goog.dom.createDom('a', {'href': entry.link}, '>'));
  }
  return p;
};

// Callback function: if we get a response from friendfeed, this function
// handles it.  A friendfeed response should look like an object that
// has a field 'entries', where 'entries' is a list of objects.  Each 
// entry corresponds to one friendfeed item.
// 
// This function creates a bunch of DOM and inserts it into the document 
// at a place marked by the page author with id "putff".
lahosken.ffhello.handleResponse_ = function(response) {
    if (!response || !response.entries || !response.entries.length) { return; }
    var put = goog.dom.getElement('putff'); // find place to insert DOM
    if (!put) { return; } // if not found, give up

    // for each entry, create a "domlet", a little bit of DOM, or null, 
    //                 if there was nothing useful.
    var domlets = goog.array.map(response.entries, 
				 lahosken.ffhello.createDomlet_);

    // add the domlets to the document:
    goog.array.reduce(domlets, function(parent, domlet) {
      if (domlet) { goog.dom.appendChild(parent, domlet); }
      return parent;
    }, put);
};

// Something went wrong; we didn't get a good response from Friendfeed.
// Fall back gracefully.  OK, fall back anyhow...
lahosken.ffhello.handleError_ = function(payload) {
    // Give up or try again?  I don't know.  Flip a coin.
    if (Math.random() > 0.5) {
        return;
    }
    lahosken.ffhello.send_();
};

// When we compile our code, the compiler will scrunch our function names
// down to space-saving things like "g"...and that name might change each 
// time we compile.  We want page authors to be able to invoke the send
// function, so export it as "FFFetch":
goog.exportSymbol('FFFetch', lahosken.ffhello.send);

[^]

comment? | | home |