Skip to content
This repository
Browse code

Add OffTheGrid list

  • Loading branch information...
commit 1762414dd5aae1d30bc59744c9e19153a0735fe2 1 parent 3b9ffaf
Neal Wiggins aoiwelle authored

Showing 1 changed file with 56 additions and 0 deletions. Show diff stats Hide diff stats

  1. +56 0 src/scripts/offthegrid.coffee
56 src/scripts/offthegrid.coffee
... ... @@ -0,0 +1,56 @@
  1 +# Description:
  2 +# Search what food trucks are at which offthegrid location for the current day.
  3 +#
  4 +# Dependencies:
  5 +# None
  6 +#
  7 +# Configuration:
  8 +# FACEBOOK_ACCESS_TOKEN
  9 +#
  10 +# Commands:
  11 +# hubot offthegrid
  12 +#
  13 +# Author:
  14 +# aoiwelle
  15 +otg_id = '129511477069092'
  16 +ROW_STRING = '\n' + ('=' for x in [1..40]).join('') + '\n'
  17 +class TruckEvent
  18 + constructor: (eventListing, msg, callback) ->
  19 + @name = eventListing.name
  20 + @start = new Date(eventListing.start_time)
  21 + @id = eventListing.id
  22 + url = "https://graph.facebook.com/#{@id}"
  23 + msg.http(url)
  24 + .query(access_token: process.env.FACEBOOK_ACCESS_TOKEN)
  25 + .get() (err, res, body) =>
  26 + descr = JSON.parse(body).description unless err
  27 + @description = descr
  28 + callback()
  29 + start:
  30 + @start
  31 + description:
  32 + @description
  33 +module.exports = (robot) ->
  34 + robot.respond /offthegrid/i, (msg) ->
  35 + d = new Date()
  36 + msg.http("https://graph.facebook.com/#{otg_id}/events")
  37 + .query(access_token: process.env.FACEBOOK_ACCESS_TOKEN)
  38 + .get() (err, res, body) ->
  39 + return msg.send "Sorry, Facebook or OTG don't like you. ERROR:#{err}" if err
  40 + return msg.send "Unable to get list of events: #{res.statusCode + ':\n' + body}" if res.statusCode != 200
  41 + graph_data = JSON.parse(body)
  42 + outstandingCallbacks = 0
  43 + this.testme = {}
  44 + callback = () ->
  45 + outstandingCallbacks -= 1
  46 + if outstandingCallbacks == 0
  47 + items = testme[d.toDateString()]
  48 + descriptionString = (item.description.replace(/^\s*/g,'') for item in items)
  49 + descriptionString = descriptionString.join('\n'+ROW_STRING+'\n')
  50 + msg.send "Today:\n#{descriptionString}"
  51 + for event_data in graph_data.data
  52 + outstandingCallbacks += 1
  53 + event = new TruckEvent(event_data, msg, callback)
  54 + unless testme[event.start.toDateString()]
  55 + testme[event.start.toDateString()] = []
  56 + testme[event.start.toDateString()].push event

0 comments on commit 1762414

Please sign in to comment.
Something went wrong with that request. Please try again.