Skip to content

Commit

Permalink
Updated README and added (the simple) Tumblr API
Browse files Browse the repository at this point in the history
  • Loading branch information
meltingice committed Oct 20, 2010
1 parent 3283f4d commit 2fa46b0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ JSONP-Fu is a project inspired by the latest design of <a href="http://meltingic

JSONP-Fu is a similar idea to oEmbed, but uses a different API and works a bit differently. Instead of throwing a whole URL at an API endpoint, we simply call the API with our parameters and use the JSON data thats returned.

Since all of the JSONP is managed by JSONP-Fu, it makes sure your header stays pretty and your DOM stays uncluttered. All JSONP callback functions are removed after they are called and all script tags added to the header are also removed once their data is retrieved.

Oh and by the way, JSONP-Fu passes JSLint with these options:

<pre>
Expand Down
18 changes: 14 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@
},
twitter: {},
plixi: {},
reddit: {}
reddit: {},
tumblr: {
username: 'test'
}
});

/*
jfu.ready('twitpic', function () {
this.users.show({username: 'meltingice'}, function (user) {
console.log(user);
});
});

/*
jfu.ready('facebook', function () {
this.api('/rlefevre/feed', function (resp) {
console.log(resp);
});
});
*/

jfu.ready('github', function () {
this.repos.watched({user: 'meltingice'}, function (user) {
console.log(user);
Expand Down Expand Up @@ -63,7 +67,7 @@
console.log(user);
});
});
*/

jfu.ready('reddit', function () {
this.front_page.hot({}, function (data) {
console.log(data);
Expand Down Expand Up @@ -93,6 +97,12 @@
console.log(results);
});
});

jfu.ready('tumblr', function () {
this.posts.show({}, function (posts) {
console.log(posts);
});
});
</script>
</head>
<body>
Expand Down
86 changes: 86 additions & 0 deletions jsonp-fu/lib/tumblr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*global jsonpfu */

/**
* Name: Tumblr
* Desc: Used to query the Tumblr JSONP API (http://develop.github.com)
* Usage:
* jfu.lib('tumblr').api_category.api_method(opts, callback);
*
* Example:
* jfu.lib('tumblr').posts.show({}, function (data) {
* console.log(data);
* });
*/

jsonpfu.extend('tumblr', function (opts, script) {

// API helper methods for validation and querying
var API = {
query: function (url, data, callback) {
var query_url,
route = /:([A-Za-z_]+)/gi,
matches, i,
username;

if (!data.username && !opts.username) {
throw "All Tumblr API calls require a username either from initialization or when called";
}

// Check for routing
if (route.test(url)) {
matches = url.match(route);
if (matches.length > 0) {
for (i = 0; i < matches.length; i++) {
if (data[matches[i].substr(1)]) {
url = url.replace(matches[i], data[matches[i].substr(1)]);
delete data[matches[i].substr(1)];
}
}
}
}

/*
* This is a bit of a crude way to check to see if all arguments are
* present but it works. Instead of throwing an error, we just silently
* return because some methods have multiple different routes.
*/
if (route.test(url)) {
return;
}

/*
* Specifying a username in an API call overrides the username
* given when initialized.
*/
if (data.username) {
username = data.username;
delete data.username;
} else {
username = opts.username;
}

query_url = 'http://' + username + '.tumblr.com/api/';
query_url += url;
query_url += '/json';

script.jsonp_query({
url: query_url,
data: data,
success: function (data) {
callback(data);
}
});

return true;
}
};

return {
posts: {
show: function (args, callback) {
API.query('read', args, callback);
}
}
};

});

0 comments on commit 2fa46b0

Please sign in to comment.