#! /usr/bin/env ruby
Main {
#
name "asana2flowdock"
#
description <<-__
asana2flowdock relays asana events into flowdock awesomely
__
#
config <<-__
asana:
token: YOUR_ASANA_API_TOKEN
workspace: YOUR_ASANA_WORKSPACE_OR_ORGANIZATION_NAME
flowdock:
token: YOUR_FLOWDOCK_API_TOKEN
__
#
db {
create_table('state') do
primary_key 'id'
String 'key'
String 'value'
index :key, :unique=>true
index :value
end unless table_exists?('state')
create_table('digests') do
primary_key 'id'
String 'digest'
index :digest, :unique=>true
end unless table_exists?('digests')
}
#
option('--quiet', '-q')
#
daemonizes!
#
def run
#
lock!
#
trap('INT'){ exit!(0) }
#
loop do
begin
process_stories!
rescue => e
STDERR.puts("#{ e.message }(#{ e.class })\n#{ Array(e.backtrace).join(10.chr) }")
end
sleep(rand(STDIN.tty? ? 10 : 30))
end
end
def process_stories!
#
asana = Asana.api_for config.get(:asana, :token)
#asana.debug = true
#asana.logger = logger
#
workspace = asana.my.workspaces.detect{|workspace| workspace.name == config.get(:asana, :workspace)}
unless workspace
workspaces = asana.my.workspaces.map{|workspace| workspace.name}
abort "unable to find your workspace=#{ config.get(:asana, :workspace).inspect } in workspaces=#{ workspaces.join(', ') }"
end
#
last_run_time = last_run_time!
#
logger.info "last_run_time: #{ last_run_time }"
#
started_at = Time.now
window = (last_run_time.to_f ... started_at.to_f)
#
workspace.projects.each do |project|
project.tasks(:modified_since => last_run_time.iso8601).each do |task|
next if task.name =~ /:\s*$/ # skip sections...
next if task.name.strip.empty? # skip shite
batch = Hash.new
task.stories(:opt_fields => 'id,created_at,type,text,created_by.name,created_by.email').map do |story|
created_at = Coerce.time(story[:created_at])
next unless window.include?(created_at.to_f)
created_by = story[:created_by]
next unless created_by
batch[task] ||= {}
batch[task][created_by] ||= []
batch[task][created_by].push(story)
end
#
batch.each do |task, created_by_stories|
created_by_stories.each do |created_by, stories|
#
url = "https://app.asana.com/0/#{ project.id }/#{ task.id }/f"
email = created_by[:email]
#
from = Map.new
from[:name] = created_by[:name]
from[:address] = created_by[:email]
#
flow = Flowdock::Flow.new(
:api_token => config.get(:flowdock, :token),
:source => "asana",
:project => project.name,
:from => from
)
#
activity = []
br = "
"
hr = "