Skip to content

Instantly share code, notes, and snippets.

Created January 10, 2012 02:05
What would you like to do?
#an example of using zlib to decompress a return from stackoverflow's api
#written in coffeescript and nodejs
zlib = require 'zlib'
http = require 'http'
module.exports = (robot) ->
robot.respond /(stack|ss) ?(find:.*) ?(tags:.*)/i, (msg)->
query = msg.match[2]
query2 = msg.match[3]
query = query.replace /find:/i, ""
query2 = query2.replace /tags:/i, ""
query = encodeURIComponent query
query2 = encodeURIComponent query2
if query.length == 0
stackscript msg, "/1.1/searchtagged=jquery&order=desc&sort=votes"
stackscript msg, "/1.1/search?tagged=#{query2}&intitle=#{query}&order=desc&sort=votes"
stackscript = (msg, url) ->
request = http.get(
host: ""
port: 80
path: url
headers: 'accept-encoding': 'gzip'
request.on "response", (response) ->
switch response.headers["content-encoding"]
when "gzip"
content = ""
gunzip = zlib.createGunzip()
gunzip.on 'data', (chunk) ->
content += chunk
gunzip.on 'end', () ->
returnData = JSON.parse(content)
for data in returnData.questions[0...5]
msg.send "\n#{data.title} \n Score #{data.score} \n URL:{data.question_answers_url} \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment