Skip to content

Instantly share code, notes, and snippets.

@pifantastic
Created August 10, 2011 21:17
Show Gist options
  • Save pifantastic/1138299 to your computer and use it in GitHub Desktop.
Save pifantastic/1138299 to your computer and use it in GitHub Desktop.
// Constructor
var client = new beard.Client({
followRedirects: true,
timeout: 60,
encoding: 'utf-8',
windowize: false
});
// Get/set any of those options at any time with:
client.option('followRedirects', false);
client.option({ followRedirects: false, timeout: 30 });
// Types of requests
client.get(url, params)
client.post(url, params)
client.json(url, params)
client.request(type, url, params)
client.download(url, params, encoding = 'binary')
// Chainy
new(beard.Client)
.download('http://virus.biz/doubleClickMePlz.exe')
.done(function(buffer) {
// Will execute on success.
// 'this' is the response object.
// 'buffer' is a buffer containing the downloaded bits
})
.done(function() {
// You can add as many done()'s and fail()'s as you want.
mail('[email protected]', "I've finished downloading your file");
});
.fail(function(error) {
// Will execute on failure.
// 'this' is the response object.
// 'error' is some sort of errorish thing.
})
.always(function() {
// Will execute no matter what.
// Will receive the same parameters as done/fail, depending
// on which happens.
});
// Unchainy
var client = new beard.Client();
// The params object will merged into the URL's existing parameters
var request = client.get('http://google.com/garbage.txt', { foo: 'bar' });
request.done(function(data) {
fs.fileWriteSync('/tmp/eyes_only/garbage.txt', data);
});
request.fail(function(error) {
console.log(error);
});
// Using beard's promises
function doAsyncThing() {
var promise = new beard.Promise();
asyncThing(function(error, success) {
if (error)
promise.reject(error);
else
promise.resolve(success);
});
return promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment