Skip to content

Instantly share code, notes, and snippets.

@s3u
Created April 24, 2012 03:32
Show Gist options
  • Save s3u/2476192 to your computer and use it in GitHub Desktop.
Save s3u/2476192 to your computer and use it in GitHub Desktop.

A table in the tables dir

create table parse.response
    on select get from 'http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address={^address}'
        using patch 'parse-response.js'

Patch in the same dir

// args.body would be an array of buffers
exports['parse response'] = function(args) {
    return {
        type: 'application/json',
        content: JSON.stringify({
            'name' : 'John Doe'
        })
    };
};

Try a select

select * from parse.response where address='foo'

This will return

{
    "name": "John Doe"
}

The default implementation of the patch response function is equivalent to the following

exports['parse response'] = function(args) {
    var str = '';
    _.each(args.body, function(buf) {
        str += buf.toString('UTF-8');
    });
    return {
        type: 'application/json',
        content: str
    };
}
@rbdixon
Copy link

rbdixon commented Jun 19, 2012

I'm not sure if this is considered a bug or not but I've observed that if one attempts to place the script in the tables directory but define the table itself on the console that the following traceback is generated:

Error: Can't set headers after they are sent.
    at ServerResponse.<anonymous> (http.js:536:11)
    at ServerResponse.setHeader (/Users/gt5392c/src/leadership/node_modules/express/node_modules/connect/lib/patch.js:62:20)
    at next (/Users/gt5392c/src/leadership/node_modules/express/node_modules/connect/lib/http.js:166:13)
    at pass (/Users/gt5392c/src/leadership/node_modules/express/lib/router/index.js:219:24)
    at nextRoute (/Users/gt5392c/src/leadership/node_modules/express/lib/router/index.js:209:7)
    at callbacks (/Users/gt5392c/src/leadership/node_modules/express/lib/router/index.js:274:11)
    at callbacks (/Users/gt5392c/src/leadership/node_modules/express/lib/router/index.js:277:9)
    at callbacks (/Users/gt5392c/src/leadership/node_modules/express/lib/router/index.js:277:9)
    at callbacks (/Users/gt5392c/src/leadership/node_modules/express/lib/router/index.js:277:9)
    at callbacks (/Users/gt5392c/src/leadership/node_modules/express/lib/router/index.js:277:9)

The workaround is to place the .ql and .js files in the tables directory, of course. It was just a wrinkle I wasn't expecting.

@s3u
Copy link
Author

s3u commented Jun 22, 2012

Actually, the console is supposed to warn that the patch is not locatable in this case. See ql-io/ql.io#417

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment