Permalink
Browse files

Basic debaking works [needs error checking]

  • Loading branch information...
1 parent 781bc1e commit 46c0b21c4e36c42aec5e1dc97fc7c5cf1ee7ed3d @brianloveswords brianloveswords committed Sep 19, 2012
Showing with 68 additions and 19 deletions.
  1. +39 −0 lib/bakery.js
  2. +29 −19 test/bakery.test.js
View
@@ -8,6 +8,8 @@
var util = require('util');
var streampng = require('streampng');
+var request = require('request');
+
var KEYWORD = 'openbadges';
function createChunk(url) {
@@ -39,5 +41,42 @@ exports.bake = function bake(options, callback) {
return png.out(callback);
};
+exports.getBakedData = function getBakedData(img, callback) {
+ var png = streampng(img);
+ var found = false;
+
+ function textListener(chunk) {
+ if (chunk.keyword !== 'openbadges')
+ return;
+ found = true;
+ png.removeListener('tEXt', textListener);
+ return callback(null, chunk.text);
+ }
+
+ function endListener() {
+ if (!found) {
+ var err = new Error('Image does not have any baked in data.')
+ err.code = IMAGE_UNBAKED;
+ return callback(err);
+ }
+ }
+
+ png.on('tEXt', textListener);
+ png.once('end', endListener);
+};
+
+
+exports.debake = function debake(image, callback) {
+ exports.getBakedData(image, function (err, url) {
+ if (err)
+ return callback(err);
+ request(url, function (err, response, body) {
+ if (err)
+ return callback(err);
+ callback(null, body);
+ });
+ });
+};
+
exports.createChunk = createChunk;
exports.KEYWORD = KEYWORD;
View
@@ -1,9 +1,11 @@
var test = require('tap').test;
-
var fs = require('fs');
var streampng = require('streampng');
-var bakery = require('../lib/bakery');
var pathutil = require('path');
+var urlutil = require('url');
+var oneshot = require('oneshot');
+
+var bakery = require('../lib/bakery');
var IMG_PATH = pathutil.join(__dirname, 'testimage.png');
var ASSERTION_URL = "http://example.org";
@@ -18,26 +20,23 @@ function getImageBuffer() {
return imgBuffer;
}
-function getBakedData(img, callback) {
- var png = streampng(img);
- var found = false;
- png.on('tEXt', function (chunk) {
- if (chunk.keyword !== 'openbadges')
- return;
- found = true;
- return callback(null, chunk.text);
- }).on('end', function () {
- if (!found)
- return callback(new Error('no baked data'));
- });
-}
-
function preheat(callback) {
var imgStream = getImageStream();
var options = {image: imgStream, url: ASSERTION_URL};
bakery.bake(options, callback);
}
+function broil(opts, callback) {
+ oneshot(opts, function (server, urlobj) {
+ var url = urlutil.format(urlobj);
+ var bakeOpts = { image: getImageStream(), url: url };
+ bakery.bake(bakeOpts, function (err, baked) {
+ if (err) throw err;
+ callback(baked);
+ });
+ });
+}
+
test('bakery.createChunk', function (t) {
var chunk = bakery.createChunk(ASSERTION_URL);
t.same(chunk.keyword, bakery.KEYWORD, 'uses the right keyword');
@@ -55,7 +54,7 @@ test('bakery.bake: takes a buffer', function (t) {
bakery.bake(options, function (err, baked) {
t.notOk(err, 'should not have an error');
t.ok(baked, 'should get back some data');
- getBakedData(baked, function (err, url) {
+ bakery.getBakedData(baked, function (err, url) {
t.notOk(err, 'there should be a matching tEXt chunk');
t.same(url, ASSERTION_URL, 'should be able to find the url');
t.end();
@@ -72,7 +71,7 @@ test('bakery.bake: takes a stream', function (t) {
bakery.bake(options, function (err, baked) {
t.notOk(err, 'should not have an error');
t.ok(baked, 'should get back some data');
- getBakedData(baked, function (err, url) {
+ bakery.getBakedData(baked, function (err, url) {
t.notOk(err, 'there should be a matching tEXt chunk');
t.same(url, ASSERTION_URL, 'should be able to find the url');
t.end();
@@ -90,4 +89,15 @@ test('bakery.bake: do not bake something twice', function (t) {
t.end();
})
});
-});
+});
+
+test('bakery.debake: should work', function (t) {
+ var opts = { body: '{"band": "grapeful dread"}'};
+ broil(opts, function (baked) {
+ bakery.debake(baked, function (err, contents) {
+ t.notOk(err, 'should not have an error');
+ t.same(contents, opts.body);
+ t.end();
+ });
+ });
+});

0 comments on commit 46c0b21

Please sign in to comment.