Skip to content

Instantly share code, notes, and snippets.

@goldeneggg
Created May 20, 2015 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldeneggg/47e63e03cc53669dece8 to your computer and use it in GitHub Desktop.
Save goldeneggg/47e63e03cc53669dece8 to your computer and use it in GitHub Desktop.
asanaの新規タスク情報を取得するスクリプト
require "json"
require "net/http"
require "uri"
require "pathname"
require "pp"
class AsanaEvents
BASE_URI = "https://app.asana.com/api/1.0"
SYNC_SAVE_PATH = "#{ENV['HOME']}/.asana_events_api/sync.txt"
def initialize(asana_project_id)
@asana_project_id = asana_project_id
Pathname.new(SYNC_SAVE_PATH).parent.mkpath
end
def execute
resp = get(request_path, current_sync_param)
case resp
when Net::HTTPSuccess
save_current_sync(JSON.parse(resp.body))
when Net::HTTPPreconditionFailed
save_current_sync(JSON.parse(resp.body))
raise "Sync_token is invalid or too old status: #{resp.code}, body: #{resp.body}"
else
raise "Invalid HTTP status: #{resp.code}, body: #{resp.body}"
end
resp_json = JSON.parse(resp.body)
select_targets(resp_json)
# XXX 取得した情報を他サービスに飛ばすとかしたい場合
# targets = select_targets(resp_json)
# targets.map do |target|
# post_other_service(target)
# end
end
def current_sync_param
sync = nil
if File.exist?(SYNC_SAVE_PATH)
sync = File.open(SYNC_SAVE_PATH, "r") do |f|
f.read
end
end
{"sync" => sync}
end
private
def request_path
"/projects/#{@asana_project_id}/events"
end
def save_current_sync(res_json)
File.open(SYNC_SAVE_PATH, "w") do |f|
f.write(res_json["sync"])
end
end
def get(request_path, param_hash)
validate_api_key
uri = URI.parse("#{BASE_URI}#{request_path}")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
#https.set_debug_output $stderr
queries = param_hash.map do |k, v|
URI.encode(k.to_s) + "=" + URI.encode(v.to_s)
end
query = queries.join("&")
req = Net::HTTP::Get.new(uri.path + "?" + query)
req.basic_auth(ENV["ASANA_API_KEY"], "")
resp = https.start do |h|
h.request(req)
end
resp
rescue => e
raise e
end
def validate_api_key
raise "ASANA_API_KEY env value is required" if !ENV.has_key?("ASANA_API_KEY")
end
def select_targets(resp_json)
resp_json["data"].select do |data|
data["type"] == "task" && data["action"] == "added"
end
end
end
if ARGV.size < 1
$stderr.puts "1 argument(project_id) is required"
pp ARGV
exit 1
end
project_id = ARGV[0]
ae = AsanaEvents.new(project_id)
res = ae.execute
pp res
@goldeneggg
Copy link
Author

  • 環境変数 ASANA_API_KEY に各自のAPIキーを設定する
$ export ASANA_API_KEY=<YOUR API KEY>
  • プロジェクトIDを引数に指定して実行する。プロジェクトIDはasanaで該当プロジェクトを開いた際のURLのhttps://app.asana.com/0/<ここがプロジェクトID>/...の部分
$ ruby asanaevents.rb <プロジェクトID>
  • 実行結果例

    • 対象イベントがある場合
    [{"user"=>{"id"=>11111111111111, "name"=>"xxxxxxx@hogemail.com"},
      "created_at"=>"2015-05-20T02:43:05.848Z",
      "type"=>"task",
      "action"=>"added",
      "resource"=>{"id"=>22222222222222, "name"=>"テストのタスク2"},
      "parent"=>{"id"=>99999999999999, "name"=>"ふがチーム"}},
     {"user"=>{"id"=>11111111111111, "name"=>"xxxxxxx@hogemail.com"},
      "created_at"=>"2015-05-20T02:43:11.284Z",
      "type"=>"task",
      "action"=>"added",
      "resource"=>{"id"=>33333333333333333, "name"=>""},
      "parent"=>{"id"=>99999999999999, "name"=>"ふがチーム"}}]
    
    • 対象イベントが無い場合
    []
    
  • APIレスポンスとして返ってくるsyncパラメータは$HOME/.asana_events_api/sync.txtに自動保存される。次回実行時はこのファイルに記載されているsyncパラメータを使用する仕組み

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment