From a56efc23a0cb0fbfc773330dad7b7cf6b7e987c6 Mon Sep 17 00:00:00 2001 From: meltingice Date: Tue, 19 Oct 2010 21:37:01 -0400 Subject: [PATCH] Fix to the jsonp_query() function and addition of the Plixi API --- index.html | 11 ++- jsonp-fu/jsonp-fu.js | 7 +- jsonp-fu/lib/plixi.js | 171 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 jsonp-fu/lib/plixi.js diff --git a/index.html b/index.html index 87f2d5e..ee19414 100644 --- a/index.html +++ b/index.html @@ -16,7 +16,8 @@ wordpress: { blog_url: 'http://blog.meltingice.net' }, - twitter: {} + twitter: {}, + plixi: {} }); /* jfu.ready('twitpic', function () { @@ -30,7 +31,7 @@ console.log(resp); }); }); - */ + jfu.ready('github', function () { this.repos.watched({user: 'meltingice'}, function (user) { @@ -52,6 +53,12 @@ this.statuses.show({id: 27877534322}, function (status) { console.log(status); }); + });*/ + + jfu.ready('plixi', function () { + this.user.show({user: 'meltingice'}, function (user) { + console.log(user); + }); }); diff --git a/jsonp-fu/jsonp-fu.js b/jsonp-fu/jsonp-fu.js index 0b10bda..27c7328 100644 --- a/jsonp-fu/jsonp-fu.js +++ b/jsonp-fu/jsonp-fu.js @@ -143,7 +143,12 @@ var jsonpfu = {}, } options.url += query_string.substr(1); - options.url += '&callback=' + options.callback; + + if (query_string.length > 0) { + options.url += '&'; + } + + options.url += 'callback=' + options.callback; /* * Make JSONP call, then remove script from header once diff --git a/jsonp-fu/lib/plixi.js b/jsonp-fu/lib/plixi.js new file mode 100644 index 0000000..c5adeea --- /dev/null +++ b/jsonp-fu/lib/plixi.js @@ -0,0 +1,171 @@ +/*global jsonpfu */ + +/** + * Name: Plixi + * Desc: Used to query the Plixi JSONP API (http://groups.google.com/group/plixi/web) + * Usage: + * jfu.lib('plixi').api_category.api_method(opts, callback); + * + * Example: + * jfu.lib('plixi').user.show({user: 'meltingice'}, function (data) { + * console.log(data); + * }); + */ + +jsonpfu.extend('plixi', 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; + + // Check for routing + if (route.test(url)) { + matches = url.match(route); + if (matches.length > 0) { + for (i = 0; i < matches.length; i++) { + 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; + } + + query_url = 'http://api.plixi.com/api/tpapi.svc/jsonp/' + url; + + script.jsonp_query({ + url: query_url, + data: data, + success: function (data) { + callback(data); + } + }); + + return true; + } + }; + + return { + user: { + show: function (args, callback) { + API.query('users/:user', args, callback); + }, + + comments: function (args, callback) { + API.query('users/:id/comments', args, callback); + }, + + friends: function (args, callback) { + API.query('users/:id/friends', args, callback); + }, + + favorites: function (args, callback) { + API.query('users/:id/favorites', args, callback); + }, + + photos: function (args, callback) { + API.query('users/:id/photos', args, callback); + }, + + photo_iterator: { + next: function (args, callback) { + API.query('users/:user_id/photos/:photo_id/next', args, callback); + }, + + previous: function (args, callback) { + API.query('users/:user_id/photos/:photo_id/previous', args, callback); + } + }, + + has_favorited: function (args, callback) { + API.query('users/:user_id/favorites/:photo_id', args, callback); + }, + + feed: function (args, callback) { + API.query('users/:id/feed', args, callback); + } + }, + + photo: { + show: function (args, callback) { + API.query('photos/:id', args, callback); + }, + + comments: function (args, callback) { + API.query('photos/:id/comments', args, callback); + }, + + favorizers: function (args, callback) { + API.query('photos/:id/favorizers', args, callback); + }, + + tags: function (args, callback) { + API.query('photos/:id/tags', args, callback); + }, + + viewers: function (args, callback) { + API.query('photos/:id/viewers', args, callback); + }, + + meta_from_url: function (args, callback) { + API.query('metadatafromurl', args, callback); + } + }, + + photos: { + iterator: { + next: function (args, callback) { + API.query('photos/:id/next', args, callback); + }, + + previous: function (args, callback) { + API.query('photos/:id/previous', args, callback); + } + }, + + by_location: function (args, callback) { + API.query('photos/bylocation', args, callback); + }, + + by_venue: function (args, callback) { + API.query('photos/byvenue', args, callback); + }, + + list: function (args, callback) { + API.query('photos', args, callback); + }, + + query_count: function (args, callback) { + API.query('querycount', args, callback); + } + }, + + leaderboard: { + viewed: function (args, callback) { + API.query('leaderboard/uploadedtoday/viewed', args, callback); + }, + + commented: function (args, callback) { + API.query('leaderboard/uploadedtoday/commented', args, callback); + }, + + voted: function (args, callback) { + API.query('leaderboard/uploadedtoday/voted', args, callback); + } + }, + + socialfeed: function (args, callback) { + API.query('socialfeed', args, callback); + } + }; + +}); \ No newline at end of file