Create a gist now

Instantly share code, notes, and snippets.

Simple GET Param to CouchDB app
_ = require 'underscore'
express = require 'express'
cradle = require 'cradle'
class Queue
# Class to store the queue documents to store in Couch.
constructor: ->
_.bindAll @, 'flush'
@reqs = []
@db = new(cradle.Connection)().database('analytics')
setInterval @flush, 1000
# If a value is a string representation of a float,
# let's convert it.
transform: (query) ->
ret = {}
for name, value of query
ret[name] = parseFloat(value) or value
ret
# Add a request to be flushed
push: (req) ->
serialized = _.extend {}, @transform(req.query),
timestamp: new Date()
'user-agent': req.headers['user-agent']
'referer': req.headers['referer']
ip: req.connection.remoteAddress
@reqs.push serialized
# Save pending requests to Couch,
# outputting to the console so it can be recovered
# in case something bad happens
flush: ->
if @reqs and @reqs.length
console.log @reqs
@db.save @reqs
@reqs = []
queue = new Queue()
app = express.createServer()
app.get '/tracking.gif', (req, res) ->
res.header 'Cache-Control', 'private, no-cache, proxy-revalidate'
res.send()
queue.push(req)
app.listen 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment