Skip to content

Instantly share code, notes, and snippets.

@jrburke

jrburke/gs.html

Created Apr 21, 2010
Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<title>Get Satisfaction API</title>
<style type="text/css">
#transferUrl {
display: block;
width: 100%;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script>
var gsApiUrl = 'http://api.getsatisfaction.com/',
couchUrl = 'http://127.0.0.1:5984/gsdata';
//Saves data to the couch. Assumes the data as a _id property
//that sets the URL for it in the couch, and it also assumes
//there is not a document of that name already in the couch.
function saveDoc(data, method) {
$.ajax({
type: method || "POST",
url: couchUrl,
dataType: 'json',
processData: 'false',
data: JSON.stringify(data),
success: function (result, textStatus, xhr) {
console.log("Success!");
console.log(result);
},
error: function (xhr, textStatus, error) {
console.error(error);
}
});
}
/**
* Top level method to do the data transfer.
* @param {String} gsPath a path for the Get Satisfaction URL.
* Do not include the http://domain.name/ part.
*/
function transfer(gsPath) {
//Fetch the API. Use JSONP version of the API.
var url = gsApiUrl + gsPath;
url += (url.indexOf("?") === -1 ? "?" : "&") + 'callback=?';
$.getJSON(url, function (data) {
//data is the JSON data
//Now inject it into the couch.
//Set the ID/url for this document in the couch.
data._id = gsPath;
//Make sure the document does not already exist. If it does,
//then grab its rev so we can update it.
$.ajax({
url: couchUrl + '/' + encodeURIComponent(gsPath),
dataType: 'json',
success: function (result, textStatus, xhr) {
if (result._rev) {
data._rev = result._rev;
saveDoc(data);
} else {
saveDoc(data);
}
},
error: function (xhr, textStatus, error) {
if (xhr.status === 404) {
saveDoc(data);
} else {
console.error(error);
}
}
});
});
}
$(function () {
$("#transferForm").bind("submit", function (evt) {
var url = $.trim(document.getElementById("transferUrl").value);
if (url) {
transfer(url);
}
evt.preventDefault();
});
});
</script>
</head>
<body>
Transfers data from Get Satisfaction into a CouchDB database. To get this to work:
<ul>
<li>Go to <a href="http://127.0.0.1:5984/_utils">http://127.0.0.1:5984/_utils</a> and create a new database called "gsdata".</li>
<li>Create a new document in that database, call it "ui".</li>
<li>Save that document.</li>
<li>Click the <b>Upload Attachment</b> button to upload this file to that <b>ui</b> document.</li>
<li>Then go to this URL to run it: <a href="http://127.0.0.1:5984/gsdata/ui/gs.html">http://127.0.0.1:5984/gsdata/ui/gs.html</a>
</ul>
<hr>
<form id="transferForm">
<div>Enter in Get Satisfaction URL to fetch:</div>
<input id="transferUrl" type="text" value="products/mozilla_thunderbird/topics.json?sort=recently_active&page=1&limit=30">
<input type="submit" name="Go" value="Go">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment