Skip to content

Instantly share code, notes, and snippets.

@bohde
Created June 4, 2011 17:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bohde/1008081 to your computer and use it in GitHub Desktop.
Save bohde/1008081 to your computer and use it in GitHub Desktop.
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