Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: CoffeeScript  |  size: 1.40 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. exec     = require('child_process').exec
  2. moment   = require 'moment'
  3.  
  4. class Birthday
  5.   constructor: (@time) ->
  6.     @planetList = ['sun', 'moon', 'mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune']
  7.     @birthday = @formatEpoch @time
  8.  
  9.   create: (callback) ->
  10.     @getAllPlanetPositions(callback)
  11.  
  12.   formatEpoch: (time) ->
  13.     moment.unix(time).format('MM.DD.YYYY')
  14.  
  15.   getAllPlanetPositions: (callback) =>
  16.     for planet, index in @planetList
  17.       console.log @cachedBirthday
  18.       @[planet] = {}
  19.       @getPlanetPosition birthday, planet, index, (planetName, position) =>
  20.         @[planetName].degree = @getDegree(position)
  21.         @[planetName].sign   = @getSign(position)
  22.         callback() if index is 9
  23.  
  24.   getPlanetPosition: (birthday, planetName, planetIndex, callback) ->
  25.     # cmd = "bin/swetest -edirinclude/ -b #{birthday} -ut 19:00:00 -p #{planet}.."
  26.     # Fake the result for now:
  27.     cmd = "echo '238'"
  28.     exec cmd, (error, stdout, stderr) ->
  29.       callback(planetName, stdout)
  30.  
  31.   getSign: (planetPosition) ->
  32.     Math.floor planetPosition / 30
  33.  
  34.   getDegree: (planetPosition) ->
  35.     planetPosition % 30
  36.  
  37. thing = new Birthday 1335902670
  38. # If I log the result in the callback I eventually get the data but how do I say, save this object to a database or return it only when all the callbacks are finished?
  39. thing.create: () ->
  40.   console.log thing.moon.sign