node.jscoffeescriptyahoo-pipeshubot

Specifying the callback for a GET request made by a hubot in node, coffee


Our local Hubot (“Sparky”) runs lots of plugin scripts and generally works well. I'm writing a plugin script that makes a GET call to Yahoo Pipes and is expecting JSONP as a result. However, I am unsure what to use for the _callback parameter. Code:

module.exports = (robot) ->
  robot.hear /\bkeyword\b/i, (msg) ->
    robot.http("http://pipes.yahoo.com/pipes/pipe.run")
      .query({
        _id: "legit-pipe-id-is-here",
        _render: "json",
        _callback: "?"
      })
      .get() (err, res, body) ->
        if body?
          data = JSON.parse(body)

The error that this gets is:

undefined:1
_({"count":10,"value":{"title":"correct title","description":"Pipes Output","lin
^
SyntaxError: Unexpected token _
  at Object.parse (native)
  at e:\node\sparky\scripts\plugin-name.coffee:26:11, <js>:11:23
  at IncomingMessage.<anonymous> (e:\node\sparky\node_modules\hubot\node_modules\scoped-http-client\lib\index.js:70:20)
  at IncomingMessage.EventEmitter.emit (events.js:117:20)
  at _stream_readable.js:920:16
  at process._tickCallback (node.js:415:13)

I've verified that the pipe works correctly when using jQuery's ajax function, but in that case jQuery sets its own callback.


Solution

  • I just realized that I don't need to use JSONP, so I don't need a _callback parameter. Regular JSON works fine when you're not in a browser, duh:

    module.exports = (robot) ->
      robot.hear /\bkeyword\b/i, (msg) ->
        robot.http("http://pipes.yahoo.com/pipes/pipe.run")
          .query({
            _id: "legit-pipe-id-is-here",
            _render: "json"
          })
          .get() (err, res, body) ->
            if body?
              data = JSON.parse(body)