From beb6789c54ea8bae6e0b263a7a033aed06660c43 Mon Sep 17 00:00:00 2001 From: sue445 Date: Mon, 8 Jan 2018 23:52:25 +0900 Subject: [PATCH 01/24] Move executables: bin -> exe Latest bundler using `exe/` instead of `bin/` https://github.com/bundler/bundler/blob/v1.16.1/lib/bundler/templates/newgem/newgem.gemspec.tt#L34-L35 --- chatwork.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatwork.gemspec b/chatwork.gemspec index f0f14e0..7fc6f35 100644 --- a/chatwork.gemspec +++ b/chatwork.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |spec| spec.license = "MIT" spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] From 3a83030457914f0fc4db8e79716ce8d579c96994 Mon Sep 17 00:00:00 2001 From: sue445 Date: Mon, 8 Jan 2018 23:56:29 +0900 Subject: [PATCH 02/24] Add bin/console --- .env.example | 4 ++++ .gitignore | 1 + README.md | 8 ++++++++ bin/console | 10 ++++++++++ chatwork.gemspec | 1 + 5 files changed, 24 insertions(+) create mode 100644 .env.example create mode 100755 bin/console diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..c25ec13 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +CHATWORK_API_TOKEN= +CHATWORK_ACCESS_TOKEN= +CHATWORK_CLIENT_ID= +CHATWORK_CLIENT_SECRET= diff --git a/.gitignore b/.gitignore index adb50aa..937c612 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ test/version_tmp tmp .ruby-gemset .ruby-version +.env diff --git a/README.md b/README.md index 5e5cea9..63b0d8c 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,14 @@ ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!") $ CHATWORK_CLIENT_ID=xxx CHATWORK_CLIENT_SECRET=xxx REFRESH_TOKEN=xxx ruby refresh_access_token.rb ``` +## Development +```bash +cp .env.example .env +vi .env + +./bin/console +``` + ## Contributing 1. Fork it diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..1bad6ee --- /dev/null +++ b/bin/console @@ -0,0 +1,10 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "chatwork" +require "dotenv" + +Dotenv.load + +require "pry" +Pry.start diff --git a/chatwork.gemspec b/chatwork.gemspec index 7fc6f35..5a2727f 100644 --- a/chatwork.gemspec +++ b/chatwork.gemspec @@ -22,6 +22,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "activesupport" spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "coveralls" + spec.add_development_dependency "dotenv" spec.add_development_dependency "onkcop", "0.52.1.0" spec.add_development_dependency "pry-byebug" spec.add_development_dependency "rake" From 53790780ab9349c94bd62b5e87e381e9d95a7fb6 Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 01:47:39 +0900 Subject: [PATCH 03/24] Refactor: use plain class methods instead of meta programing I believe this is easier to understand than before --- lib/chatwork.rb | 3 +-- lib/chatwork/contacts.rb | 14 +++------- lib/chatwork/entity.rb | 33 ----------------------- lib/chatwork/entity_methods.rb | 25 +++++++++++++++++ lib/chatwork/me.rb | 14 +++------- lib/chatwork/member.rb | 10 +++---- lib/chatwork/message.rb | 18 ++++--------- lib/chatwork/my_task.rb | 19 +++---------- lib/chatwork/operations.rb | 49 ---------------------------------- lib/chatwork/room.rb | 16 +++-------- lib/chatwork/task.rb | 22 +++++---------- 11 files changed, 53 insertions(+), 170 deletions(-) delete mode 100644 lib/chatwork/entity.rb create mode 100644 lib/chatwork/entity_methods.rb delete mode 100644 lib/chatwork/operations.rb diff --git a/lib/chatwork.rb b/lib/chatwork.rb index f1448b2..1c48bf3 100644 --- a/lib/chatwork.rb +++ b/lib/chatwork.rb @@ -7,13 +7,12 @@ module ChatWork autoload :ChatWorkError, "chatwork/chatwork_error" autoload :Client, "chatwork/client" autoload :Contacts, "chatwork/contacts" - autoload :Entity, "chatwork/entity" + autoload :EntityMethods, "chatwork/entity_methods" autoload :Me, "chatwork/me" autoload :Member, "chatwork/member" autoload :Message, "chatwork/message" autoload :MyTask, "chatwork/my_task" autoload :OAuthClient, "chatwork/oauth_client" - autoload :Operations, "chatwork/operations" autoload :Room, "chatwork/room" autoload :Task, "chatwork/task" autoload :Token, "chatwork/token" diff --git a/lib/chatwork/contacts.rb b/lib/chatwork/contacts.rb index af49c4d..8759db3 100644 --- a/lib/chatwork/contacts.rb +++ b/lib/chatwork/contacts.rb @@ -1,6 +1,6 @@ module ChatWork - class Contacts < Entity - install_class_operations :_get + module Contacts + extend EntityMethods # Get the list of your contacts # @@ -22,15 +22,7 @@ class Contacts < Entity # } # ] def self.get - _get - end - - def self.path - "/contacts" - end - - def path - "/contacts" + _get("/contacts") end end end diff --git a/lib/chatwork/entity.rb b/lib/chatwork/entity.rb deleted file mode 100644 index 9ccee99..0000000 --- a/lib/chatwork/entity.rb +++ /dev/null @@ -1,33 +0,0 @@ -module ChatWork - class Entity - class << self - include Operations - def convert(hash) - # not implement - # converter = ResponseConverter.new - # converter.convert(hash) - hash - end - - def hash_compact(hash) - hash.reject { |_k, v| v.nil? } - end - end - - attr_reader :attributes - - def initialize(attributes) - @attributes = attributes - end - - private - - # not implement - def update_attributes(attributes) - raise "unexpected object" if attributes["object"] != @attributes["object"] - new_object = ResponseConverter.new.convert(attributes) - @attributes = new_object.attributes - self - end - end -end diff --git a/lib/chatwork/entity_methods.rb b/lib/chatwork/entity_methods.rb new file mode 100644 index 0000000..48fe345 --- /dev/null +++ b/lib/chatwork/entity_methods.rb @@ -0,0 +1,25 @@ +module ChatWork + module EntityMethods + private + + def _get(path, params = {}, &block) + ChatWork.client.get(path, hash_compact(params), &block) + end + + def _post(path, params = {}, &block) + ChatWork.client.post(path, hash_compact(params), &block) + end + + def _put(path, params = {}, &block) + ChatWork.client.put(path, hash_compact(params), &block) + end + + def _delete(path, params = {}, &block) + ChatWork.client.delete(path, hash_compact(params), &block) + end + + def hash_compact(hash) + hash.reject { |_k, v| v.nil? } + end + end +end diff --git a/lib/chatwork/me.rb b/lib/chatwork/me.rb index dbedba5..b65b1d2 100644 --- a/lib/chatwork/me.rb +++ b/lib/chatwork/me.rb @@ -1,6 +1,6 @@ module ChatWork - class Me < Entity - install_class_operations :_get + module Me + extend EntityMethods # Get your account information # @@ -31,15 +31,7 @@ class Me < Entity # "login_mail": "account@example.com" # } def self.get - _get - end - - def self.path - "/me" - end - - def path - "/me" + _get("/me") end end end diff --git a/lib/chatwork/member.rb b/lib/chatwork/member.rb index 03ff70f..a8978bf 100644 --- a/lib/chatwork/member.rb +++ b/lib/chatwork/member.rb @@ -1,6 +1,6 @@ module ChatWork - class Member < Entity - install_class_operations :_get + module Member + extend EntityMethods # Get the list of all chat members associated with the specified chat # @@ -24,11 +24,7 @@ class Member < Entity # } # ] def self.get(room_id:) - _get(room_id: room_id) - end - - def self.path - "/rooms/%d/members" + _get("/rooms/#{room_id}/members") end end end diff --git a/lib/chatwork/message.rb b/lib/chatwork/message.rb index c9bf857..6f67402 100644 --- a/lib/chatwork/message.rb +++ b/lib/chatwork/message.rb @@ -1,6 +1,6 @@ module ChatWork - class Message < Entity - install_class_operations :_create, :_get + module Message + extend EntityMethods # Get all messages associated with the specified chat (returns up to 100 entries). # @@ -28,7 +28,7 @@ class Message < Entity # } # ] def self.get(room_id:, force: nil) - params = { room_id: room_id } + params = {} case force when 1, true @@ -37,7 +37,7 @@ def self.get(room_id:, force: nil) params[:force] = 0 end - _get(params) + _get("/rooms/#{room_id}/messages", params) end # Add new message to the chat @@ -54,15 +54,7 @@ def self.get(room_id:, force: nil) # "message_id": "1234" # } def self.create(room_id:, body:) - _create(room_id: room_id, body: body) - end - - def self.path - "/rooms/%d/messages" - end - - def path - "/rooms/%d/messages" + _post("/rooms/#{room_id}/messages", body: body) end end end diff --git a/lib/chatwork/my_task.rb b/lib/chatwork/my_task.rb index 45399c6..d9d5f56 100644 --- a/lib/chatwork/my_task.rb +++ b/lib/chatwork/my_task.rb @@ -1,6 +1,6 @@ module ChatWork - class MyTask < Entity - install_class_operations :_get + module MyTask + extend EntityMethods # Get the list of all unfinished tasks # @@ -34,20 +34,7 @@ class MyTask < Entity # } # ] def self.get(assigned_by_account_id: nil, status: nil) - params = { - assigned_by_account_id: assigned_by_account_id, - status: status, - } - - _get(hash_compact(params)) - end - - def self.path - "/my/tasks" - end - - def path - "/my/tasks" + _get("/my/tasks", assigned_by_account_id: assigned_by_account_id, status: status) end end end diff --git a/lib/chatwork/operations.rb b/lib/chatwork/operations.rb deleted file mode 100644 index f088e06..0000000 --- a/lib/chatwork/operations.rb +++ /dev/null @@ -1,49 +0,0 @@ -module ChatWork - module Operations - ACCEPT_PARAMS_ID = %i[file_id task_id message_id].freeze - - attr_accessor :assign_path - - def install_class_operations(*operations) - define_create if operations.include?(:_create) - define_get if operations.include?(:_get) - end - - def define_get - instance_eval do - def _get(params = {}, &block) - @assign_path = parse_if_hash_key_exists(path, params, :room_id) - attach_nested_resource_id(params) - convert(ChatWork.client.get(@assign_path, params, &block)) - end - end - end - - def define_create - instance_eval do - def _create(params = {}, &block) - @assign_path = parse_if_hash_key_exists(path, params, :room_id) - attach_nested_resource_id(params) - convert(ChatWork.client.post(@assign_path, params, &block)) - end - end - end - - private - - def parse_if_hash_key_exists(string, hash, key) - if hash.include?(key) - string % hash.delete(key) - else - string - end - end - - def attach_nested_resource_id(params) - ACCEPT_PARAMS_ID.each do |id_name| - next unless params.include? id_name - @assign_path += "/#{params.delete(id_name)}" - end - end - end -end diff --git a/lib/chatwork/room.rb b/lib/chatwork/room.rb index ed1dd8d..aed9a26 100644 --- a/lib/chatwork/room.rb +++ b/lib/chatwork/room.rb @@ -1,6 +1,6 @@ module ChatWork - class Room < Entity - install_class_operations :_create, :_get + module Room + extend EntityMethods # Get the list of all chats on your account # @@ -27,7 +27,7 @@ class Room < Entity # } # ] def self.get - _get + _get("/rooms") end # rubocop:disable Metrics/ParameterLists @@ -61,17 +61,9 @@ def self.create(description: nil, icon_preset: nil, members_admin_ids:, members_ params[:members_member_ids] = Array(members_member_ids).join(",") if members_member_ids params[:members_readonly_ids] = Array(members_readonly_ids).join(",") if members_readonly_ids - _create(hash_compact(params)) + _post("/rooms", params) end # rubocop:enable Metrics/ParameterLists - - def self.path - "/rooms" - end - - def path - "/rooms" - end end end diff --git a/lib/chatwork/task.rb b/lib/chatwork/task.rb index 3bc0860..85168d3 100644 --- a/lib/chatwork/task.rb +++ b/lib/chatwork/task.rb @@ -1,6 +1,6 @@ module ChatWork - class Task < Entity - install_class_operations :_get, :_create + module Task + extend EntityMethods # Get the list of tasks associated with the specified chat # @@ -35,13 +35,7 @@ class Task < Entity # } # ] def self.get(room_id:, account_id:, assigned_by_account_id: nil, status: nil) - params = { - room_id: room_id, - account_id: account_id, - assigned_by_account_id: assigned_by_account_id, - status: status, - } - _get(hash_compact(params)) + _get("/rooms/#{room_id}/tasks", account_id: account_id, assigned_by_account_id: assigned_by_account_id, status: status) end # Add a new task to the chat @@ -61,16 +55,12 @@ def self.get(room_id:, account_id:, assigned_by_account_id: nil, status: nil) # } def self.create(room_id:, body:, to_ids:, limit: nil) params = { - room_id: room_id, - body: body, - to_ids: Array(to_ids).join(","), + body: body, + to_ids: Array(to_ids).join(","), } params[:limit] = limit.to_i if limit - _create(hash_compact(params)) - end - def self.path - "/rooms/%d/tasks" + _post("/rooms/#{room_id}/tasks", params) end end end From bea4f374b40d4cb4adc42f1fa3f06a01fa1816d9 Mon Sep 17 00:00:00 2001 From: sue445 Date: Tue, 9 Jan 2018 00:11:12 +0900 Subject: [PATCH 04/24] Impl ChatWork::MyStatus.get --- lib/chatwork.rb | 1 + lib/chatwork/my_status.rb | 24 ++++++++++++++++++++++++ spec/lib/chatwork/my_status_spec.rb | 13 +++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 lib/chatwork/my_status.rb create mode 100644 spec/lib/chatwork/my_status_spec.rb diff --git a/lib/chatwork.rb b/lib/chatwork.rb index 1c48bf3..964f49f 100644 --- a/lib/chatwork.rb +++ b/lib/chatwork.rb @@ -11,6 +11,7 @@ module ChatWork autoload :Me, "chatwork/me" autoload :Member, "chatwork/member" autoload :Message, "chatwork/message" + autoload :MyStatus, "chatwork/my_status" autoload :MyTask, "chatwork/my_task" autoload :OAuthClient, "chatwork/oauth_client" autoload :Room, "chatwork/room" diff --git a/lib/chatwork/my_status.rb b/lib/chatwork/my_status.rb new file mode 100644 index 0000000..45488e1 --- /dev/null +++ b/lib/chatwork/my_status.rb @@ -0,0 +1,24 @@ +module ChatWork + module MyStatus + extend EntityMethods + + # Get the number of: unread messages, unread To messages, and unfinished tasks. + # + # @see http://developer.chatwork.com/ja/endpoint_my.html#GET-my-status + # + # @return [Hash] + # + # @example response format + # { + # "unread_room_num": 2, + # "mention_room_num": 1, + # "mytask_room_num": 3, + # "unread_num": 12, + # "mention_num": 1, + # "mytask_num": 8 + # } + def self.get + _get("/my/status") + end + end +end diff --git a/spec/lib/chatwork/my_status_spec.rb b/spec/lib/chatwork/my_status_spec.rb new file mode 100644 index 0000000..630cf9d --- /dev/null +++ b/spec/lib/chatwork/my_status_spec.rb @@ -0,0 +1,13 @@ +describe ChatWork::MyStatus do + describe ".get", type: :api do + subject do + ChatWork::MyStatus.get + end + + before do + stub_chatwork_request(:get, "/my/status") + end + + it_behaves_like :a_chatwork_api, :get, "/my/status" + end +end From 7b36a8f04ca5262837666aab80502b53c6f1888d Mon Sep 17 00:00:00 2001 From: sue445 Date: Tue, 9 Jan 2018 00:22:18 +0900 Subject: [PATCH 05/24] Impl ChatWork::Room.find --- lib/chatwork/room.rb | 29 +++++++++++++++++++++++++++++ spec/lib/chatwork/room_spec.rb | 12 ++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/chatwork/room.rb b/lib/chatwork/room.rb index aed9a26..3412f88 100644 --- a/lib/chatwork/room.rb +++ b/lib/chatwork/room.rb @@ -65,5 +65,34 @@ def self.create(description: nil, icon_preset: nil, members_admin_ids:, members_ end # rubocop:enable Metrics/ParameterLists + + # Get chat name, icon, and Type (my, direct, or group) + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id + # + # @param room_id [Integer] + # + # @return [Hash] + # + # @example response format + # { + # "room_id": 123, + # "name": "Group Chat Name", + # "type": "group", + # "role": "admin", + # "sticky": false, + # "unread_num": 10, + # "mention_num": 1, + # "mytask_num": 0, + # "message_num": 122, + # "file_num": 10, + # "task_num": 17, + # "icon_path": "https://example.com/ico_group.png", + # "last_update_time": 1298905200, + # "description": "room description text" + # } + def self.find(room_id:) + _get("/rooms/#{room_id}") + end end end diff --git a/spec/lib/chatwork/room_spec.rb b/spec/lib/chatwork/room_spec.rb index 792a11f..e4a4ccc 100644 --- a/spec/lib/chatwork/room_spec.rb +++ b/spec/lib/chatwork/room_spec.rb @@ -47,4 +47,16 @@ it_behaves_like :a_chatwork_api, :post, "/rooms" end end + + describe ".find", type: :api do + subject { ChatWork::Room.find(room_id: room_id) } + + let(:room_id) { 123 } + + before do + stub_chatwork_request(:get, "/rooms/#{room_id}", "/rooms/{room_id}") + end + + it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}" + end end From 8f8eee8219f4f046e5f749d3349f2580da9cbf69 Mon Sep 17 00:00:00 2001 From: sue445 Date: Tue, 9 Jan 2018 01:34:18 +0900 Subject: [PATCH 06/24] Impl ChatWork::Room.update --- lib/chatwork/room.rb | 13 +++++++++++++ spec/lib/chatwork/room_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/chatwork/room.rb b/lib/chatwork/room.rb index 3412f88..be89b45 100644 --- a/lib/chatwork/room.rb +++ b/lib/chatwork/room.rb @@ -94,5 +94,18 @@ def self.create(description: nil, icon_preset: nil, members_admin_ids:, members_ def self.find(room_id:) _get("/rooms/#{room_id}") end + + # Change the title and icon type of the specified chat + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id + # + # @param room_id [Integer] + # @param description [String] Description of the group chat + # @param icon_preset [String] Type of the group chat icon (group, check, document, meeting, event, project, business, + # study, security, star, idea, heart, magcup, beer, music, sports, travel) + # @param name [String] Title of the group chat. + def self.update(room_id:, description: nil, icon_preset: nil, name: nil) + _put("/rooms/#{room_id}", description: description, icon_preset: icon_preset, name: name) + end end end diff --git a/spec/lib/chatwork/room_spec.rb b/spec/lib/chatwork/room_spec.rb index e4a4ccc..efbc0cb 100644 --- a/spec/lib/chatwork/room_spec.rb +++ b/spec/lib/chatwork/room_spec.rb @@ -59,4 +59,26 @@ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}" end + + describe ".update", type: :api do + subject do + ChatWork::Room.update( + room_id: room_id, + description: description, + icon_preset: icon_preset, + name: name, + ) + end + + let(:room_id) { 123 } + let(:description) { "group chat description" } + let(:icon_preset) { "meeting" } + let(:name) { "Website renewal project" } + + before do + stub_chatwork_request(:put, "/rooms/#{room_id}", "/rooms/{room_id}") + end + + it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}" + end end From beb69c1e33e45bf4d27e68eb3eff958460dd3f2f Mon Sep 17 00:00:00 2001 From: sue445 Date: Tue, 9 Jan 2018 01:46:04 +0900 Subject: [PATCH 07/24] Impl ChatWork::Room.destroy --- lib/chatwork/base_client.rb | 3 ++- lib/chatwork/chatwork_error.rb | 3 --- lib/chatwork/room.rb | 10 ++++++++++ spec/lib/chatwork/room_spec.rb | 18 ++++++++++++++++++ spec/support/contexts/api_context.rb | 2 +- spec/support/matchers/match_example.rb | 2 +- spec/support/utils/raml_parser.rb | 11 +++++++++-- 7 files changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/chatwork/base_client.rb b/lib/chatwork/base_client.rb index bea92b5..aad01fe 100644 --- a/lib/chatwork/base_client.rb +++ b/lib/chatwork/base_client.rb @@ -20,7 +20,8 @@ def initialize(api_base, api_version, header) def handle_response(response) case response.status when 204 - ChatWork::ChatWorkError.from_response(response.status, response.body, response.headers) + # HTTP status 204 doesn't return json + response.body when 200..299 begin JSON.parse(response.body) diff --git a/lib/chatwork/chatwork_error.rb b/lib/chatwork/chatwork_error.rb index 3ece01f..ec56e8c 100644 --- a/lib/chatwork/chatwork_error.rb +++ b/lib/chatwork/chatwork_error.rb @@ -2,9 +2,6 @@ module ChatWork class ChatWorkError < StandardError def self.from_response(status, body, headers) - # HTTP status 204 don't have body. - return APIError.new(status, "") if status == 204 - hash = begin JSON.parse(body) diff --git a/lib/chatwork/room.rb b/lib/chatwork/room.rb index be89b45..85e5192 100644 --- a/lib/chatwork/room.rb +++ b/lib/chatwork/room.rb @@ -107,5 +107,15 @@ def self.find(room_id:) def self.update(room_id:, description: nil, icon_preset: nil, name: nil) _put("/rooms/#{room_id}", description: description, icon_preset: icon_preset, name: name) end + + # Leave/Delete a group chat + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#DELETE-rooms-room_id + # + # @param room_id [Integer] + # @param action_type [String] leave from a room or delete a room (leave, delete) + def self.destroy(room_id:, action_type:) + _delete("/rooms/#{room_id}", action_type: action_type) + end end end diff --git a/spec/lib/chatwork/room_spec.rb b/spec/lib/chatwork/room_spec.rb index efbc0cb..3cd7997 100644 --- a/spec/lib/chatwork/room_spec.rb +++ b/spec/lib/chatwork/room_spec.rb @@ -81,4 +81,22 @@ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}" end + + describe ".destroy", type: :api do + subject do + ChatWork::Room.destroy( + room_id: room_id, + action_type: action_type, + ) + end + + let(:room_id) { 123 } + let(:action_type) { "leave" } + + before do + stub_chatwork_request(:delete, "/rooms/#{room_id}", "/rooms/{room_id}", 204) + end + + it_behaves_like :a_chatwork_api, :delete, "/rooms/{room_id}", 204 + end end diff --git a/spec/support/contexts/api_context.rb b/spec/support/contexts/api_context.rb index 81abb9b..8e05441 100644 --- a/spec/support/contexts/api_context.rb +++ b/spec/support/contexts/api_context.rb @@ -28,7 +28,7 @@ def stub_chatwork_request(expected_verb, expected_path, resource_path = nil, sta query_example = RamlParser.find_query_parameter_example(expected_verb, resource_path) unless query_example.empty? case expected_verb - when :get + when :get, :delete query_string = "?" + query_example.to_query when :post, :put request_options[:headers]["Content-Type"] = "application/x-www-form-urlencoded" diff --git a/spec/support/matchers/match_example.rb b/spec/support/matchers/match_example.rb index e3c4c08..5882c86 100644 --- a/spec/support/matchers/match_example.rb +++ b/spec/support/matchers/match_example.rb @@ -8,6 +8,6 @@ description do expected_example = RamlParser.find_response_example(verb, resource, status) - "match #{expected_example}" + "match '#{expected_example}'" end end diff --git a/spec/support/utils/raml_parser.rb b/spec/support/utils/raml_parser.rb index 750aa0b..0d36bd0 100644 --- a/spec/support/utils/raml_parser.rb +++ b/spec/support/utils/raml_parser.rb @@ -10,7 +10,7 @@ def self.find_response_example(verb, path, status = 200) return nil unless resource response_json = resource.dig("responses", status, "body", "application/json", "example") - return JSON.parse(response_json) if response_json + return parse_response(response_json) if response_json return nil unless resource["is"] @@ -19,7 +19,7 @@ def self.find_response_example(verb, path, status = 200) next unless trait response_json = trait.dig("responses", status, "body", "application/json", "example") - return JSON.parse(response_json) if response_json + return parse_response(response_json) if response_json end nil @@ -76,4 +76,11 @@ def self.raml @raml = YAML.safe_load(yaml_data) end + + def self.parse_response(response_json) + JSON.parse(response_json) + rescue JSON::ParserError + response_json + end + private_class_method :parse_response end From f07cc8e896f3e95ea991a6a5f5b86ecae72baddb Mon Sep 17 00:00:00 2001 From: sue445 Date: Wed, 10 Jan 2018 00:00:09 +0900 Subject: [PATCH 08/24] Impl ChatWork::Member.update_all --- lib/chatwork/member.rb | 28 +++++++++++++++++++++++++++ spec/lib/chatwork/member_spec.rb | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/lib/chatwork/member.rb b/lib/chatwork/member.rb index a8978bf..c3f22a1 100644 --- a/lib/chatwork/member.rb +++ b/lib/chatwork/member.rb @@ -26,5 +26,33 @@ module Member def self.get(room_id:) _get("/rooms/#{room_id}/members") end + + # Change associated members of group chat at once + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-members + # + # @param room_id [Integer] + # @param members_admin_ids [Array, String] List of user IDs who will be given administrator permission for the group chat. + # At least one user must be specified as an administrator. + # @param members_member_ids [Array, String] List of user IDs who will be given member permission for the group chat. + # @param members_readonly_ids [Array, String] List of user IDs who will be given read-only permission for the group chat. + # + # @return [Hash] + # + # @example response format + # { + # "admin": [123, 542, 1001], + # "member": [10, 103], + # "readonly": [6, 11] + # } + def self.update_all(room_id:, members_admin_ids:, members_member_ids: nil, members_readonly_ids: nil) + params = { + members_admin_ids: Array(members_admin_ids).join(","), + } + params[:members_member_ids] = Array(members_member_ids).join(",") if members_member_ids + params[:members_readonly_ids] = Array(members_readonly_ids).join(",") if members_readonly_ids + + _put("/rooms/#{room_id}/members", params) + end end end diff --git a/spec/lib/chatwork/member_spec.rb b/spec/lib/chatwork/member_spec.rb index 7caeb7e..34405d4 100644 --- a/spec/lib/chatwork/member_spec.rb +++ b/spec/lib/chatwork/member_spec.rb @@ -10,4 +10,37 @@ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/members" end + + describe ".update_all", type: :api do + subject do + ChatWork::Member.update_all( + room_id: room_id, + members_admin_ids: members_admin_ids, + members_member_ids: members_member_ids, + members_readonly_ids: members_readonly_ids, + ) + end + + let(:room_id) { 123 } + + before do + stub_chatwork_request(:put, "/rooms/#{room_id}/members", "/rooms/{room_id}/members") + end + + context "with String" do + let(:members_admin_ids) { "123,542,1001" } + let(:members_member_ids) { "21,344" } + let(:members_readonly_ids) { "15,103" } + + it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/members" + end + + context "with Array" do + let(:members_admin_ids) { [123, 542, 1001] } + let(:members_member_ids) { [21, 344] } + let(:members_readonly_ids) { [15, 103] } + + it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/members" + end + end end From 0f75376024ec35e64af0d9858effd62379867c38 Mon Sep 17 00:00:00 2001 From: sue445 Date: Wed, 10 Jan 2018 00:40:32 +0900 Subject: [PATCH 09/24] Impl ChatWork::Message.read --- lib/chatwork/message.rb | 18 ++++++++++++++++++ spec/lib/chatwork/message_spec.rb | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/chatwork/message.rb b/lib/chatwork/message.rb index 6f67402..14d8e87 100644 --- a/lib/chatwork/message.rb +++ b/lib/chatwork/message.rb @@ -56,5 +56,23 @@ def self.get(room_id:, force: nil) def self.create(room_id:, body:) _post("/rooms/#{room_id}/messages", body: body) end + + # Mark messages as read + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-read + # + # @param room_id [Integer] + # @param message_id [String] + # + # @return [Hash] + # + # @example response format + # { + # "unread_num": 461, + # "mention_num": 0 + # } + def self.read(room_id:, message_id: nil) + _put("/rooms/#{room_id}/messages/read", message_id: message_id) + end end end diff --git a/spec/lib/chatwork/message_spec.rb b/spec/lib/chatwork/message_spec.rb index 1e3d6cc..029e4f9 100644 --- a/spec/lib/chatwork/message_spec.rb +++ b/spec/lib/chatwork/message_spec.rb @@ -33,4 +33,17 @@ it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/messages" end + + describe ".read", type: :api do + subject { ChatWork::Message.read(room_id: room_id, message_id: message_id) } + + let(:room_id) { 123 } + let(:message_id) { "101" } + + before do + stub_chatwork_request(:put, "/rooms/#{room_id}/messages/read", "/rooms/{room_id}/messages/read") + end + + it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/read" + end end From 390a1a5aae2dda70a1492fdb06677103266a80e0 Mon Sep 17 00:00:00 2001 From: sue445 Date: Wed, 10 Jan 2018 01:33:48 +0900 Subject: [PATCH 10/24] Impl ChatWork::Message.unread --- lib/chatwork/message.rb | 20 +++++++++++++++++++- spec/lib/chatwork/message_spec.rb | 13 +++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/chatwork/message.rb b/lib/chatwork/message.rb index 14d8e87..4032b7d 100644 --- a/lib/chatwork/message.rb +++ b/lib/chatwork/message.rb @@ -60,7 +60,7 @@ def self.create(room_id:, body:) # Mark messages as read # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-read - # + # # @param room_id [Integer] # @param message_id [String] # @@ -74,5 +74,23 @@ def self.create(room_id:, body:) def self.read(room_id:, message_id: nil) _put("/rooms/#{room_id}/messages/read", message_id: message_id) end + + # Mark messages as unread + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-unread + # + # @param room_id [Integer] + # @param message_id [String] + # + # @return [Hash] + # + # @example response format + # { + # "unread_num": 3, + # "mention_num": 0 + # } + def self.unread(room_id:, message_id:) + _put("/rooms/#{room_id}/messages/unread", message_id: message_id) + end end end diff --git a/spec/lib/chatwork/message_spec.rb b/spec/lib/chatwork/message_spec.rb index 029e4f9..f0df516 100644 --- a/spec/lib/chatwork/message_spec.rb +++ b/spec/lib/chatwork/message_spec.rb @@ -46,4 +46,17 @@ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/read" end + + describe ".unread", type: :api do + subject { ChatWork::Message.unread(room_id: room_id, message_id: message_id) } + + let(:room_id) { 123 } + let(:message_id) { "101" } + + before do + stub_chatwork_request(:put, "/rooms/#{room_id}/messages/unread", "/rooms/{room_id}/messages/unread") + end + + it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/unread" + end end From 838030757f220ad2d8ce6912f74881d5d0c9d46b Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 00:52:08 +0900 Subject: [PATCH 11/24] Impl ChatWork::Message.find --- lib/chatwork/message.rb | 25 +++++++++++++++++++++++++ spec/lib/chatwork/message_spec.rb | 13 +++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/chatwork/message.rb b/lib/chatwork/message.rb index 4032b7d..ba75e15 100644 --- a/lib/chatwork/message.rb +++ b/lib/chatwork/message.rb @@ -92,5 +92,30 @@ def self.read(room_id:, message_id: nil) def self.unread(room_id:, message_id:) _put("/rooms/#{room_id}/messages/unread", message_id: message_id) end + + # Get information about the specified message + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages-message_id + # + # @param room_id [Integer] + # @param message_id [String] + # + # @return [Hash] + # + # @example response format + # { + # "message_id": "5", + # "account": { + # "account_id": 123, + # "name": "Bob", + # "avatar_image_url": "https://example.com/ico_avatar.png" + # }, + # "body": "Hello Chatwork!", + # "send_time": 1384242850, + # "update_time": 0 + # } + def self.find(room_id:, message_id:) + _get("/rooms/#{room_id}/messages/#{message_id}") + end end end diff --git a/spec/lib/chatwork/message_spec.rb b/spec/lib/chatwork/message_spec.rb index f0df516..532ac1e 100644 --- a/spec/lib/chatwork/message_spec.rb +++ b/spec/lib/chatwork/message_spec.rb @@ -59,4 +59,17 @@ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/unread" end + + describe ".find", type: :api do + subject { ChatWork::Message.find(room_id: room_id, message_id: message_id) } + + let(:room_id) { 123 } + let(:message_id) { "101" } + + before do + stub_chatwork_request(:get, "/rooms/#{room_id}/messages/#{message_id}", "/rooms/{room_id}/messages/{message_id}") + end + + it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/messages/{message_id}" + end end From 009cca05c690381b9cc934436b6113425f415853 Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 02:17:35 +0900 Subject: [PATCH 12/24] Impl ChatWork::Message.update --- lib/chatwork/message.rb | 18 ++++++++++++++++++ spec/lib/chatwork/message_spec.rb | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/chatwork/message.rb b/lib/chatwork/message.rb index ba75e15..c2c15b0 100644 --- a/lib/chatwork/message.rb +++ b/lib/chatwork/message.rb @@ -117,5 +117,23 @@ def self.unread(room_id:, message_id:) def self.find(room_id:, message_id:) _get("/rooms/#{room_id}/messages/#{message_id}") end + + # Update the specified message + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-message_id + # + # @param room_id [Integer] + # @param message_id [String] + # @param body [String] message body + # + # @return [Hash] + # + # @example response format + # { + # "message_id": "1234" + # } + def self.update(room_id:, message_id:, body:) + _put("/rooms/#{room_id}/messages/#{message_id}", body: body) + end end end diff --git a/spec/lib/chatwork/message_spec.rb b/spec/lib/chatwork/message_spec.rb index 532ac1e..a9ca28d 100644 --- a/spec/lib/chatwork/message_spec.rb +++ b/spec/lib/chatwork/message_spec.rb @@ -72,4 +72,18 @@ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/messages/{message_id}" end + + describe ".update", type: :api do + subject { ChatWork::Message.update(room_id: room_id, message_id: message_id, body: body) } + + let(:room_id) { 123 } + let(:message_id) { "101" } + let(:body) { "Hello ChatWork!" } + + before do + stub_chatwork_request(:put, "/rooms/#{room_id}/messages/#{message_id}", "/rooms/{room_id}/messages/{message_id}") + end + + it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/{message_id}" + end end From cff7ed5e191ec02e3b6bb20ade62ee48ca84065c Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 02:19:26 +0900 Subject: [PATCH 13/24] Impl ChatWork::Message.destroy --- lib/chatwork/message.rb | 17 +++++++++++++++++ spec/lib/chatwork/message_spec.rb | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/chatwork/message.rb b/lib/chatwork/message.rb index c2c15b0..3769c1d 100644 --- a/lib/chatwork/message.rb +++ b/lib/chatwork/message.rb @@ -135,5 +135,22 @@ def self.find(room_id:, message_id:) def self.update(room_id:, message_id:, body:) _put("/rooms/#{room_id}/messages/#{message_id}", body: body) end + + # Destroy the specified message + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-message_id + # + # @param room_id [Integer] + # @param message_id [String] + # + # @return [Hash] + # + # @example response format + # { + # "message_id": "1234" + # } + def self.destroy(room_id:, message_id:) + _delete("/rooms/#{room_id}/messages/#{message_id}") + end end end diff --git a/spec/lib/chatwork/message_spec.rb b/spec/lib/chatwork/message_spec.rb index a9ca28d..6d5783a 100644 --- a/spec/lib/chatwork/message_spec.rb +++ b/spec/lib/chatwork/message_spec.rb @@ -86,4 +86,17 @@ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/{message_id}" end + + describe ".destroy", type: :api do + subject { ChatWork::Message.destroy(room_id: room_id, message_id: message_id) } + + let(:room_id) { 123 } + let(:message_id) { "101" } + + before do + stub_chatwork_request(:delete, "/rooms/#{room_id}/messages/#{message_id}", "/rooms/{room_id}/messages/{message_id}") + end + + it_behaves_like :a_chatwork_api, :delete, "/rooms/{room_id}/messages/{message_id}" + end end From 7587b89e4ab373cb0505297d03e500484c6e9b8e Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 08:29:44 +0900 Subject: [PATCH 14/24] Impl ChatWork::Task.find --- lib/chatwork/task.rb | 31 +++++++++++++++++++++++++++++++ spec/lib/chatwork/task_spec.rb | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/chatwork/task.rb b/lib/chatwork/task.rb index 85168d3..2371425 100644 --- a/lib/chatwork/task.rb +++ b/lib/chatwork/task.rb @@ -62,5 +62,36 @@ def self.create(room_id:, body:, to_ids:, limit: nil) _post("/rooms/#{room_id}/tasks", params) end + + # Get information about the specified task + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks-task_id + # + # @param room_id [Integer] + # @param task_id [Integer] + # + # @return [Hash] + # + # @example response format + # { + # "task_id": 3, + # "account": { + # "account_id": 123, + # "name": "Bob", + # "avatar_image_url": "https://example.com/abc.png" + # }, + # "assigned_by_account": { + # "account_id": 456, + # "name": "Anna", + # "avatar_image_url": "https://example.com/def.png" + # }, + # "message_id": "13", + # "body": "buy milk", + # "limit_time": 1384354799, + # "status": "open" + # } + def self.find(room_id:, task_id:) + _get("/rooms/#{room_id}/tasks/#{task_id}") + end end end diff --git a/spec/lib/chatwork/task_spec.rb b/spec/lib/chatwork/task_spec.rb index cd9cb2f..1b39c49 100644 --- a/spec/lib/chatwork/task_spec.rb +++ b/spec/lib/chatwork/task_spec.rb @@ -56,4 +56,22 @@ it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/tasks" end end + + describe ".find", type: :api do + subject do + ChatWork::Task.find( + room_id: room_id, + task_id: task_id, + ) + end + + let(:room_id) { 123 } + let(:task_id) { 3 } + + before do + stub_chatwork_request(:get, "/rooms/#{room_id}/tasks/#{task_id}", "/rooms/{room_id}/tasks/{task_id}") + end + + it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/tasks/{task_id}" + end end From 2a53103fb7cb1b388c152d9f6a2d63a97f03c0f7 Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 08:39:21 +0900 Subject: [PATCH 15/24] Add doc --- lib/chatwork/task.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/chatwork/task.rb b/lib/chatwork/task.rb index 2371425..d060ee3 100644 --- a/lib/chatwork/task.rb +++ b/lib/chatwork/task.rb @@ -9,6 +9,7 @@ module Task # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks # # @param room_id [Integer] + # @param account_id [Integer] # @param assigned_by_account_id [Integer] Account ID of the person who assigned task # @param status [String] Task status (open, done) # From 7bff923b47237fb10201c553d28d861a678151c8 Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 08:39:01 +0900 Subject: [PATCH 16/24] Impl ChatWork::File.get --- lib/chatwork.rb | 1 + lib/chatwork/file.rb | 33 +++++++++++++++++++++++++++++++++ spec/lib/chatwork/file_spec.rb | 14 ++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 lib/chatwork/file.rb create mode 100644 spec/lib/chatwork/file_spec.rb diff --git a/lib/chatwork.rb b/lib/chatwork.rb index 964f49f..987b118 100644 --- a/lib/chatwork.rb +++ b/lib/chatwork.rb @@ -8,6 +8,7 @@ module ChatWork autoload :Client, "chatwork/client" autoload :Contacts, "chatwork/contacts" autoload :EntityMethods, "chatwork/entity_methods" + autoload :File, "chatwork/file" autoload :Me, "chatwork/me" autoload :Member, "chatwork/member" autoload :Message, "chatwork/message" diff --git a/lib/chatwork/file.rb b/lib/chatwork/file.rb new file mode 100644 index 0000000..842c028 --- /dev/null +++ b/lib/chatwork/file.rb @@ -0,0 +1,33 @@ +module ChatWork + module File + extend EntityMethods + + # Get the list of files associated with the specified chat + # + # @param room_id [Integer] + # @param account_id [Integer] + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files + # + # @return [Array] + # + # @example response format + # [ + # { + # "file_id": 3, + # "account": { + # "account_id": 123, + # "name": "Bob", + # "avatar_image_url": "https://example.com/ico_avatar.png" + # }, + # "message_id": "22", + # "filename": "README.md", + # "filesize": 2232, + # "upload_time": 1384414750 + # } + # ] + def self.get(room_id:, account_id:) + _get("/rooms/#{room_id}/files", account_id: account_id) + end + end +end diff --git a/spec/lib/chatwork/file_spec.rb b/spec/lib/chatwork/file_spec.rb new file mode 100644 index 0000000..9485297 --- /dev/null +++ b/spec/lib/chatwork/file_spec.rb @@ -0,0 +1,14 @@ +describe ChatWork::File do + describe ".get", type: :api do + subject { ChatWork::File.get(room_id: room_id, account_id: account_id) } + + before do + stub_chatwork_request(:get, "/rooms/#{room_id}/files", "/rooms/{room_id}/files") + end + + let(:room_id) { 123 } + let(:account_id) { 101 } + + it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files" + end +end From 350161490c4d84d4f28663764a3594ac67645a15 Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 23:35:14 +0900 Subject: [PATCH 17/24] Impl EntityMethods#boolean_to_integer --- chatwork.gemspec | 1 + lib/chatwork/entity_methods.rb | 11 +++++++++++ lib/chatwork/message.rb | 11 +---------- spec/lib/chatwork/entity_methods_spec.rb | 21 +++++++++++++++++++++ spec/spec_helper.rb | 1 + 5 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 spec/lib/chatwork/entity_methods_spec.rb diff --git a/chatwork.gemspec b/chatwork.gemspec index 5a2727f..b70a4d2 100644 --- a/chatwork.gemspec +++ b/chatwork.gemspec @@ -28,6 +28,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rake" spec.add_development_dependency "rspec" spec.add_development_dependency "rspec-its" + spec.add_development_dependency "rspec-parameterized" spec.add_development_dependency "rubocop", "0.52.1" spec.add_development_dependency "rubocop-rspec", "1.21.0" spec.add_development_dependency "webmock" diff --git a/lib/chatwork/entity_methods.rb b/lib/chatwork/entity_methods.rb index 48fe345..91db6aa 100644 --- a/lib/chatwork/entity_methods.rb +++ b/lib/chatwork/entity_methods.rb @@ -21,5 +21,16 @@ def _delete(path, params = {}, &block) def hash_compact(hash) hash.reject { |_k, v| v.nil? } end + + def boolean_to_integer(value) + case value + when true + 1 + when false + 0 + else + value + end + end end end diff --git a/lib/chatwork/message.rb b/lib/chatwork/message.rb index 3769c1d..5539ef1 100644 --- a/lib/chatwork/message.rb +++ b/lib/chatwork/message.rb @@ -28,16 +28,7 @@ module Message # } # ] def self.get(room_id:, force: nil) - params = {} - - case force - when 1, true - params[:force] = 1 - when 0, false - params[:force] = 0 - end - - _get("/rooms/#{room_id}/messages", params) + _get("/rooms/#{room_id}/messages", force: boolean_to_integer(force)) end # Add new message to the chat diff --git a/spec/lib/chatwork/entity_methods_spec.rb b/spec/lib/chatwork/entity_methods_spec.rb new file mode 100644 index 0000000..1e78b45 --- /dev/null +++ b/spec/lib/chatwork/entity_methods_spec.rb @@ -0,0 +1,21 @@ +describe ChatWork::EntityMethods do + include ChatWork::EntityMethods + + describe "#boolean_to_integer" do + subject { boolean_to_integer(value) } + + using RSpec::Parameterized::TableSyntax + + where(:value, :expected) do + false | 0 + true | 1 + 0 | 0 + 1 | 1 + nil | nil + end + + with_them do + it { should eq expected } + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ef678e1..02d9c9d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,7 @@ require "chatwork" +require "rspec-parameterized" require "rspec/its" require "webmock/rspec" require "pry" From 912f615c5fb1cbad9329d8fe9150b0c6696cf895 Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 23:28:13 +0900 Subject: [PATCH 18/24] Impl ChatWork::File.find --- lib/chatwork/file.rb | 28 ++++++++++++++++++++++++++++ spec/lib/chatwork/file_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/chatwork/file.rb b/lib/chatwork/file.rb index 842c028..a1dfe6f 100644 --- a/lib/chatwork/file.rb +++ b/lib/chatwork/file.rb @@ -29,5 +29,33 @@ module File def self.get(room_id:, account_id:) _get("/rooms/#{room_id}/files", account_id: account_id) end + + # Get information about the specified file + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files-file_id + # + # @param room_id [Integer] + # @param file_id [Integer] + # @param create_download_url [Boolean] whether or not to create a download link. + # If set to true, download like will be created for 30 seconds + # + # @return [Array] + # + # @example response format + # { + # "file_id":3, + # "account": { + # "account_id":123, + # "name":"Bob", + # "avatar_image_url": "https://example.com/ico_avatar.png" + # }, + # "message_id": "22", + # "filename": "README.md", + # "filesize": 2232, + # "upload_time": 1384414750 + # } + def self.find(room_id:, file_id:, create_download_url: nil) + _get("/rooms/#{room_id}/files/#{file_id}", create_download_url: boolean_to_integer(create_download_url)) + end end end diff --git a/spec/lib/chatwork/file_spec.rb b/spec/lib/chatwork/file_spec.rb index 9485297..f078d00 100644 --- a/spec/lib/chatwork/file_spec.rb +++ b/spec/lib/chatwork/file_spec.rb @@ -11,4 +11,27 @@ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files" end + + describe ".find", type: :api do + subject { ChatWork::File.find(room_id: room_id, file_id: file_id, create_download_url: create_download_url) } + + before do + stub_chatwork_request(:get, "/rooms/#{room_id}/files/#{file_id}", "/rooms/{room_id}/files/{file_id}") + end + + let(:room_id) { 123 } + let(:file_id) { 101 } + + context "when force is Integer" do + let(:create_download_url) { 1 } + + it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files/{file_id}" + end + + context "when force is boolean" do + let(:create_download_url) { true } + + it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files/{file_id}" + end + end end From 779ec4d378a48f0c38fc0639ce79c49aaffc3ed7 Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 23:41:35 +0900 Subject: [PATCH 19/24] Impl ChatWork::IncomingRequest.get --- lib/chatwork.rb | 1 + lib/chatwork/incoming_request.rb | 31 ++++++++++++++++++++++ spec/lib/chatwork/incoming_request_spec.rb | 11 ++++++++ 3 files changed, 43 insertions(+) create mode 100644 lib/chatwork/incoming_request.rb create mode 100644 spec/lib/chatwork/incoming_request_spec.rb diff --git a/lib/chatwork.rb b/lib/chatwork.rb index 987b118..f813c33 100644 --- a/lib/chatwork.rb +++ b/lib/chatwork.rb @@ -9,6 +9,7 @@ module ChatWork autoload :Contacts, "chatwork/contacts" autoload :EntityMethods, "chatwork/entity_methods" autoload :File, "chatwork/file" + autoload :IncomingRequest, "chatwork/incoming_request" autoload :Me, "chatwork/me" autoload :Member, "chatwork/member" autoload :Message, "chatwork/message" diff --git a/lib/chatwork/incoming_request.rb b/lib/chatwork/incoming_request.rb new file mode 100644 index 0000000..61039e0 --- /dev/null +++ b/lib/chatwork/incoming_request.rb @@ -0,0 +1,31 @@ +module ChatWork + module IncomingRequest + extend EntityMethods + + # You can get the list of contact approval request you received + # + # (*This method returns up to 100 entries. We are planning to implement pagination to support larger number of data retrieval) + # + # @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#GET-incoming_requests + # + # @return [Array] + # + # @example response format + # [ + # { + # "request_id": 123, + # "account_id": 363, + # "message": "hogehoge", + # "name": "John Smith", + # "chatwork_id": "tarochatworkid", + # "organization_id": 101, + # "organization_name": "Hello Company", + # "department": "Marketing", + # "avatar_image_url": "https://example.com/abc.png" + # } + # ] + def self.get + _get("/incoming_requests") + end + end +end diff --git a/spec/lib/chatwork/incoming_request_spec.rb b/spec/lib/chatwork/incoming_request_spec.rb new file mode 100644 index 0000000..24ecdb7 --- /dev/null +++ b/spec/lib/chatwork/incoming_request_spec.rb @@ -0,0 +1,11 @@ +describe ChatWork::IncomingRequest do + describe ".get", type: :api do + subject { ChatWork::IncomingRequest.get } + + before do + stub_chatwork_request(:get, "/incoming_requests") + end + + it_behaves_like :a_chatwork_api, :get, "/incoming_requests" + end +end From 7a06884b1ebc558c6394cd7de7211ea1e53b9ca5 Mon Sep 17 00:00:00 2001 From: sue445 Date: Thu, 11 Jan 2018 23:59:31 +0900 Subject: [PATCH 20/24] Impl ChatWork::IncomingRequest.update --- lib/chatwork/incoming_request.rb | 23 ++++++++++++++++++++++ spec/lib/chatwork/incoming_request_spec.rb | 12 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/chatwork/incoming_request.rb b/lib/chatwork/incoming_request.rb index 61039e0..b8d44cf 100644 --- a/lib/chatwork/incoming_request.rb +++ b/lib/chatwork/incoming_request.rb @@ -27,5 +27,28 @@ module IncomingRequest def self.get _get("/incoming_requests") end + + # You can approve a contact approval request you received + # + # @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#PUT-incoming_requests-request_id + # + # @param request_id [Integer] + # + # @return [Hash] + # + # @example response format + # { + # "account_id": 363, + # "room_id": 1234, + # "name": "John Smith", + # "chatwork_id": "tarochatworkid", + # "organization_id": 101, + # "organization_name": "Hello Company", + # "department": "Marketing", + # "avatar_image_url": "https://example.com/abc.png" + # } + def self.update(request_id:) + _put("/incoming_requests/#{request_id}") + end end end diff --git a/spec/lib/chatwork/incoming_request_spec.rb b/spec/lib/chatwork/incoming_request_spec.rb index 24ecdb7..de04f62 100644 --- a/spec/lib/chatwork/incoming_request_spec.rb +++ b/spec/lib/chatwork/incoming_request_spec.rb @@ -8,4 +8,16 @@ it_behaves_like :a_chatwork_api, :get, "/incoming_requests" end + + describe ".update", type: :api do + subject { ChatWork::IncomingRequest.update(request_id: request_id) } + + let(:request_id) { 123 } + + before do + stub_chatwork_request(:put, "/incoming_requests/#{request_id}", "/incoming_requests/{request_id}") + end + + it_behaves_like :a_chatwork_api, :put, "/incoming_requests/{request_id}" + end end From 14518e964de3e321f6a91d436cb32580bdd933cf Mon Sep 17 00:00:00 2001 From: sue445 Date: Fri, 12 Jan 2018 00:02:06 +0900 Subject: [PATCH 21/24] Impl ChatWork::IncomingRequest.destroy --- lib/chatwork/incoming_request.rb | 9 +++++++++ spec/lib/chatwork/incoming_request_spec.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/chatwork/incoming_request.rb b/lib/chatwork/incoming_request.rb index b8d44cf..3a52d37 100644 --- a/lib/chatwork/incoming_request.rb +++ b/lib/chatwork/incoming_request.rb @@ -50,5 +50,14 @@ def self.get def self.update(request_id:) _put("/incoming_requests/#{request_id}") end + + # You can decline a contact approval request you received + # + # @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#DELETE-incoming_requests-request_id + # + # @param request_id [Integer] + def self.destroy(request_id:) + _delete("/incoming_requests/#{request_id}") + end end end diff --git a/spec/lib/chatwork/incoming_request_spec.rb b/spec/lib/chatwork/incoming_request_spec.rb index de04f62..e4926b6 100644 --- a/spec/lib/chatwork/incoming_request_spec.rb +++ b/spec/lib/chatwork/incoming_request_spec.rb @@ -20,4 +20,16 @@ it_behaves_like :a_chatwork_api, :put, "/incoming_requests/{request_id}" end + + describe ".destroy", type: :api do + subject { ChatWork::IncomingRequest.destroy(request_id: request_id) } + + let(:request_id) { 123 } + + before do + stub_chatwork_request(:delete, "/incoming_requests/#{request_id}", "/incoming_requests/{request_id}", 204) + end + + it_behaves_like :a_chatwork_api, :delete, "/incoming_requests/{request_id}", 204 + end end From 881f2502f93af46ff9f9fb018bf4a72e4adb7c6c Mon Sep 17 00:00:00 2001 From: sue445 Date: Fri, 12 Jan 2018 00:06:34 +0900 Subject: [PATCH 22/24] Add alias to IncomingRequest --- lib/chatwork/incoming_request.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/chatwork/incoming_request.rb b/lib/chatwork/incoming_request.rb index 3a52d37..cd6bddf 100644 --- a/lib/chatwork/incoming_request.rb +++ b/lib/chatwork/incoming_request.rb @@ -59,5 +59,10 @@ def self.update(request_id:) def self.destroy(request_id:) _delete("/incoming_requests/#{request_id}") end + + class << self + alias_method :approve, :update + alias_method :decline, :destroy + end end end From 4937fcd149b3c2e0c2355d19b7c2157d5e9ca8e9 Mon Sep 17 00:00:00 2001 From: sue445 Date: Fri, 12 Jan 2018 00:11:15 +0900 Subject: [PATCH 23/24] Add english reference url --- lib/chatwork/contacts.rb | 1 + lib/chatwork/file.rb | 2 ++ lib/chatwork/incoming_request.rb | 3 +++ lib/chatwork/me.rb | 1 + lib/chatwork/member.rb | 2 ++ lib/chatwork/message.rb | 7 +++++++ lib/chatwork/my_status.rb | 1 + lib/chatwork/my_task.rb | 1 + lib/chatwork/room.rb | 5 +++++ lib/chatwork/task.rb | 3 +++ 10 files changed, 26 insertions(+) diff --git a/lib/chatwork/contacts.rb b/lib/chatwork/contacts.rb index 8759db3..de6ce93 100644 --- a/lib/chatwork/contacts.rb +++ b/lib/chatwork/contacts.rb @@ -5,6 +5,7 @@ module Contacts # Get the list of your contacts # # @see http://developer.chatwork.com/ja/endpoint_contacts.html#GET-contacts + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @return [Array] # diff --git a/lib/chatwork/file.rb b/lib/chatwork/file.rb index a1dfe6f..0755824 100644 --- a/lib/chatwork/file.rb +++ b/lib/chatwork/file.rb @@ -8,6 +8,7 @@ module File # @param account_id [Integer] # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @return [Array] # @@ -33,6 +34,7 @@ def self.get(room_id:, account_id:) # Get information about the specified file # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files-file_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param file_id [Integer] diff --git a/lib/chatwork/incoming_request.rb b/lib/chatwork/incoming_request.rb index cd6bddf..c9f16e3 100644 --- a/lib/chatwork/incoming_request.rb +++ b/lib/chatwork/incoming_request.rb @@ -7,6 +7,7 @@ module IncomingRequest # (*This method returns up to 100 entries. We are planning to implement pagination to support larger number of data retrieval) # # @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#GET-incoming_requests + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @return [Array] # @@ -31,6 +32,7 @@ def self.get # You can approve a contact approval request you received # # @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#PUT-incoming_requests-request_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param request_id [Integer] # @@ -54,6 +56,7 @@ def self.update(request_id:) # You can decline a contact approval request you received # # @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#DELETE-incoming_requests-request_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param request_id [Integer] def self.destroy(request_id:) diff --git a/lib/chatwork/me.rb b/lib/chatwork/me.rb index b65b1d2..c7b57bb 100644 --- a/lib/chatwork/me.rb +++ b/lib/chatwork/me.rb @@ -5,6 +5,7 @@ module Me # Get your account information # # @see http://developer.chatwork.com/ja/endpoint_me.html#GET-me + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @return [Hash] # diff --git a/lib/chatwork/member.rb b/lib/chatwork/member.rb index c3f22a1..c580ea9 100644 --- a/lib/chatwork/member.rb +++ b/lib/chatwork/member.rb @@ -5,6 +5,7 @@ module Member # Get the list of all chat members associated with the specified chat # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-members + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @@ -30,6 +31,7 @@ def self.get(room_id:) # Change associated members of group chat at once # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-members + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param members_admin_ids [Array, String] List of user IDs who will be given administrator permission for the group chat. diff --git a/lib/chatwork/message.rb b/lib/chatwork/message.rb index 5539ef1..264b34d 100644 --- a/lib/chatwork/message.rb +++ b/lib/chatwork/message.rb @@ -7,6 +7,7 @@ module Message # If the parameter is not set, it returns the next 100 entries from previous call. # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param force [Boolean, Integer] Flag which forces to get 100 newest entries regardless of previous calls. @@ -34,6 +35,7 @@ def self.get(room_id:, force: nil) # Add new message to the chat # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-messages + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param body [String] message body @@ -51,6 +53,7 @@ def self.create(room_id:, body:) # Mark messages as read # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-read + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param message_id [String] @@ -69,6 +72,7 @@ def self.read(room_id:, message_id: nil) # Mark messages as unread # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-unread + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param message_id [String] @@ -87,6 +91,7 @@ def self.unread(room_id:, message_id:) # Get information about the specified message # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages-message_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param message_id [String] @@ -112,6 +117,7 @@ def self.find(room_id:, message_id:) # Update the specified message # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-message_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param message_id [String] @@ -130,6 +136,7 @@ def self.update(room_id:, message_id:, body:) # Destroy the specified message # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-messages-message_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param message_id [String] diff --git a/lib/chatwork/my_status.rb b/lib/chatwork/my_status.rb index 45488e1..3785a5f 100644 --- a/lib/chatwork/my_status.rb +++ b/lib/chatwork/my_status.rb @@ -5,6 +5,7 @@ module MyStatus # Get the number of: unread messages, unread To messages, and unfinished tasks. # # @see http://developer.chatwork.com/ja/endpoint_my.html#GET-my-status + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @return [Hash] # diff --git a/lib/chatwork/my_task.rb b/lib/chatwork/my_task.rb index d9d5f56..5097f37 100644 --- a/lib/chatwork/my_task.rb +++ b/lib/chatwork/my_task.rb @@ -7,6 +7,7 @@ module MyTask # (*This method returns up to 100 entries. We are planning to implement pagination to support larger number of data retrieval) # # @see http://developer.chatwork.com/ja/endpoint_my.html#GET-my-tasks + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param assigned_by_account_id [Integer] Account ID of the person who assigned task # @param status [String] Task status (open, done) diff --git a/lib/chatwork/room.rb b/lib/chatwork/room.rb index 85e5192..5244f2f 100644 --- a/lib/chatwork/room.rb +++ b/lib/chatwork/room.rb @@ -5,6 +5,7 @@ module Room # Get the list of all chats on your account # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @return [Array] # @@ -35,6 +36,7 @@ def self.get # Create a new group chat # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param description [String] Description of the group chat # @param icon_preset [String] Type of the group chat icon (group, check, document, meeting, event, project, business, @@ -69,6 +71,7 @@ def self.create(description: nil, icon_preset: nil, members_admin_ids:, members_ # Get chat name, icon, and Type (my, direct, or group) # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @@ -98,6 +101,7 @@ def self.find(room_id:) # Change the title and icon type of the specified chat # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param description [String] Description of the group chat @@ -111,6 +115,7 @@ def self.update(room_id:, description: nil, icon_preset: nil, name: nil) # Leave/Delete a group chat # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#DELETE-rooms-room_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param action_type [String] leave from a room or delete a room (leave, delete) diff --git a/lib/chatwork/task.rb b/lib/chatwork/task.rb index d060ee3..b8569fe 100644 --- a/lib/chatwork/task.rb +++ b/lib/chatwork/task.rb @@ -7,6 +7,7 @@ module Task # (*This method returns up to 100 entries. We are planning to implement pagination to support larger number of data retrieval) # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param account_id [Integer] @@ -42,6 +43,7 @@ def self.get(room_id:, account_id:, assigned_by_account_id: nil, status: nil) # Add a new task to the chat # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-tasks + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param body [String] Task description @@ -67,6 +69,7 @@ def self.create(room_id:, body:, to_ids:, limit: nil) # Get information about the specified task # # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks-task_id + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf # # @param room_id [Integer] # @param task_id [Integer] From 79a5f2271f1e4a94d182fd57d859038223bbcb3c Mon Sep 17 00:00:00 2001 From: sue445 Date: Fri, 12 Jan 2018 00:43:39 +0900 Subject: [PATCH 24/24] Refactor: class -> module --- lib/chatwork/token.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chatwork/token.rb b/lib/chatwork/token.rb index 87cc6d1..057ff09 100644 --- a/lib/chatwork/token.rb +++ b/lib/chatwork/token.rb @@ -1,5 +1,5 @@ module ChatWork - class Token + module Token # refresh access_token with refresh_token # # @param refresh_token [String]