Created
May 20, 2015 02:45
-
-
Save goldeneggg/47e63e03cc53669dece8 to your computer and use it in GitHub Desktop.
asanaの新規タスク情報を取得するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ASANA_API_KEY
に各自のAPIキーを設定するhttps://app.asana.com/0/<ここがプロジェクトID>/...
の部分実行結果例
APIレスポンスとして返ってくる
sync
パラメータは$HOME/.asana_events_api/sync.txt
に自動保存される。次回実行時はこのファイルに記載されているsync
パラメータを使用する仕組み