Skip to content

Instantly share code, notes, and snippets.

@kota
Last active August 29, 2015 14:14
Show Gist options
  • Save kota/449fda006f8cf9c90351 to your computer and use it in GitHub Desktop.
Save kota/449fda006f8cf9c90351 to your computer and use it in GitHub Desktop.
# encoding: utf-8
#
# usage: ChatworkAPI.new.assign_task_to_all_members('room_id','task body')
#
require 'net/https'
require 'uri'
require 'json'
class ChatworkAPI
BASE_URL = 'https://api.chatwork.com/v1'
API_TOKEN = 'YOUR_API_TOKEN'
def assign_task_to_all_members(room_id, task_body)
members = get_room_members(room_id)
task = post_task(room_id, task_body, members.map{ |m| m['account_id'] })
end
def get_status
get('/my/status')
end
def get_rooms
rooms = get('/rooms')
rooms.map { |r| "id=#{r['room_id']}, name=#{r['name']}" }
end
def get_room(room_id)
room = get("/rooms/#{room_id}")
puts room
room
end
def get_room_members(room_id)
members = get("/rooms/#{room_id}/members")
puts members
members
end
def post_task(room_id, body, ids)
task = post("/rooms/#{room_id}/tasks", { body: body, to_ids: ids.join(',') })
puts task
task
end
private
def https
https = Net::HTTP.new('api.chatwork.com', 443)
https.use_ssl = true
https
end
def header
{ 'X-ChatWorkToken' => API_TOKEN }
end
def get(path)
response = https.get("/v1#{path}", header)
JSON.parse(response.body)
end
def post(path, params)
query_string = params.map{|k,v| URI.encode(k.to_s) + "=" + URI.encode(v.to_s) }.join("&")
response = https.post("/v1#{path}", query_string, header)
JSON.parse(response.body)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment