Skip to content

Instantly share code, notes, and snippets.

@levidehaan
Created January 10, 2012 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save levidehaan/1586373 to your computer and use it in GitHub Desktop.
Save levidehaan/1586373 to your computer and use it in GitHub Desktop.
#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"
else
stackscript msg, "/1.1/search?tagged=#{query2}&intitle=#{query}&order=desc&sort=votes"
stackscript = (msg, url) ->
request = http.get(
host: "api.stackoverflow.com"
port: 80
path: url
headers: 'accept-encoding': 'gzip'
)
request.on "response", (response) ->
switch response.headers["content-encoding"]
when "gzip"
content = ""
gunzip = zlib.createGunzip()
response.pipe(gunzip)
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: http://stackoverflow.com#{data.question_answers_url} \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment