Skip to content

Instantly share code, notes, and snippets.

@jrmoran
Created February 28, 2011 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrmoran/847018 to your computer and use it in GitHub Desktop.
Save jrmoran/847018 to your computer and use it in GitHub Desktop.
### Getting entries from my blog (Posterous)
getBlogPosts = ->
req = $.ajax 'http://query.yahooapis.com/v1/public/yql',
jsonp:'callback'
dataType:'jsonp'
data:
q: "select channel.item.title, channel.item.pubDate,
channel.item.link from xml where
url = 'http://blog.jrmoran.com/rss' limit 6"
format: 'json'
req.success (res) ->
if res.query.count > 0
# comprehension will iterate over each entry, parse them to html
# and add them to the array `htmlstr`
htmlstr = for post in res.query.results.rss
item = post.channel.item
date = item.pubDate.substring 0,16
"<li>
<a href = ' #{ item.link } '> #{ item.title} </a><br>
<span class = 'date' > #{ date } </span>
</li>"
$('#recent-posts').append htmlstr.join('')
### Getting entries from my blog (Octopress)
# `targetNode` is a jQuery object
loadBlogPosts: (targetNode)->
# return an html string representation of an entry
renderEntry= (entry)->
"<li>
<a href = ' #{ entry.url } '> #{ entry.title} </a><br>
<span class = 'date' > #{ entry.date } </span>
</li>"
# Retrieving posts from My blog using YQL
#
# * `callback` used by YQL to reference the callback function
# * `JSONP` for cross-domain request
# * `q` YQL query
#
req = $.ajax 'http://query.yahooapis.com/v1/public/yql',
jsonp:'callback'
dataType:'jsonp'
data:
q: "select entry.title.content, entry.link, entry.updated from xml where
url = 'http://blog.jrmoran.com/atom.xml' limit 6"
format: 'json'
# Processing response
req.success (data) ->
if data.query.count > 0
# comprehension will iterate over each entry, parse them to html
# and add them to the array `htmlstr`
htmlstr = for result in data.query.results.feed
renderEntry
url : result.entry.link.href
title: result.entry.title
date : result.entry.updated.substring 0, 10
targetNode.append htmlstr.join ''
// Retrieving posts from My blog using YQL
// @requires jQuery 1.5.1
var req = $.ajax('http://query.yahooapis.com/v1/public/yql', {
// YQL uses this parameter to reference the callback function
jsonp:'callback',
// This is a cross-domain request so JSONP must be used
dataType:'jsonp',
data: {
// this is the actual YQL query and the format of the response
q: "select channel.item.title, channel.item.pubDate, " +
"channel.item.link from xml where " +
"url = 'http://blog.jrmoran.com/rss' limit 5",
format: 'json'
}
});
// Working with the response
req.success(function(res){
//work with the response here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment