diff --git a/api b/api index 4da3605..3a3bf8b 160000 --- a/api +++ b/api @@ -1 +1 @@ -Subproject commit 4da360531810f3a62870d14599a0b87abb767e54 +Subproject commit 3a3bf8b8b366934a543cf031f47b16ee0ee1cb1e diff --git a/bin/console b/bin/console index 1bad6ee..6b59ce3 100755 --- a/bin/console +++ b/bin/console @@ -3,8 +3,15 @@ require "bundler/setup" require "chatwork" require "dotenv" +require "faraday_curl" Dotenv.load +logger = Logger.new(STDOUT) + +@client = ChatWork::Client.new(api_key: ENV["CHATWORK_API_TOKEN"]) +connection = @client.instance_variable_get(:@conn) +connection.request(:curl, logger, :debug) + require "pry" Pry.start diff --git a/chatwork.gemspec b/chatwork.gemspec index 5226589..7225c5f 100644 --- a/chatwork.gemspec +++ b/chatwork.gemspec @@ -25,6 +25,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "coveralls" spec.add_development_dependency "dotenv" + spec.add_development_dependency "faraday_curl" spec.add_development_dependency "onkcop", "0.53.0.0" spec.add_development_dependency "pry-byebug" spec.add_development_dependency "rake" diff --git a/lib/chatwork.rb b/lib/chatwork.rb index e0b5267..bd17cf1 100644 --- a/lib/chatwork.rb +++ b/lib/chatwork.rb @@ -1,5 +1,6 @@ require "chatwork/version" require "hashie" +require "faraday" module ChatWork autoload :BaseClient, "chatwork/base_client" diff --git a/lib/chatwork/base_client.rb b/lib/chatwork/base_client.rb index 3ada4a9..fb6492c 100644 --- a/lib/chatwork/base_client.rb +++ b/lib/chatwork/base_client.rb @@ -16,6 +16,7 @@ def initialize(api_base:, api_version: "", header:) default_header.merge!(header) @conn = Faraday.new(api_base, headers: default_header) do |builder| + builder.request :multipart builder.request :url_encoded builder.response :mashify builder.response :json diff --git a/lib/chatwork/client/file_methods.rb b/lib/chatwork/client/file_methods.rb index 2bc97fb..a768381 100644 --- a/lib/chatwork/client/file_methods.rb +++ b/lib/chatwork/client/file_methods.rb @@ -64,4 +64,39 @@ def get_files(room_id:, account_id:, &block) def find_file(room_id:, file_id:, create_download_url: nil, &block) get("/rooms/#{room_id}/files/#{file_id}", create_download_url: boolean_to_integer(create_download_url), &block) end + + # Upload a new file to room + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-files + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf + # + # @param room_id [Integer] + # @param file [Faraday::UploadIO] + # @param message [String] + # + # @yield [response_body, response_header] if block was given, return response body and response header through block arguments + # @yieldparam response_body [Hashie::Mash] response body + # @yieldparam response_header [Hash] response header (e.g. X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset) + # + # @return [Hashie::Mash] + # + # @example how to upload a file + # client = ChatWork::Client.new(api_key: "XXX") + # client.create_file(room_id: 11111111, file: Faraday::UploadIO.new("/path/to/file.txt", "text/plain"), message: "Test") + # + # @example response format + # { + # "file_id": 1234 + # } + # + def create_file(room_id:, file:, message: nil, &block) + params = { + file: file, + } + params[:message] = message if message + + post("/rooms/#{room_id}/files", params, &block) + end + + alias_method :upload_file, :create_file end diff --git a/lib/chatwork/file.rb b/lib/chatwork/file.rb index 1a9b0ec..9b8971e 100644 --- a/lib/chatwork/file.rb +++ b/lib/chatwork/file.rb @@ -65,5 +65,36 @@ def self.get(room_id:, account_id:, &block) def self.find(room_id:, file_id:, create_download_url: nil, &block) ChatWork.client.find_file(room_id: room_id, file_id: file_id, create_download_url: create_download_url, &block) end + + # Upload a new file to room + # + # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-files + # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf + # + # @param room_id [Integer] + # @param file [Faraday::UploadIO] + # @param message [String] + # + # @yield [response_body, response_header] if block was given, return response body and response header through block arguments + # @yieldparam response_body [Hashie::Mash] response body + # @yieldparam response_header [Hash] response header (e.g. X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset) + # + # @return [Hashie::Mash] + # + # @example how to upload a file + # ChatWork::File.create(room_id: 11111111, file: Faraday::UploadIO.new("/path/to/file.txt", "text/plain"), message: "Test") + # + # @example response format + # { + # "file_id": 1234 + # } + # + def self.create(room_id:, file:, message: nil, &block) + ChatWork.client.create_file(room_id: room_id, file: file, message: message, &block) + end + + class << self + alias_method :upload, :create + end end end diff --git a/spec/data/upload.txt b/spec/data/upload.txt new file mode 100644 index 0000000..5539005 --- /dev/null +++ b/spec/data/upload.txt @@ -0,0 +1 @@ +Hello ChatWork! diff --git a/spec/lib/chatwork/client/file_methods_spec.rb b/spec/lib/chatwork/client/file_methods_spec.rb index 124297e..0c6476b 100644 --- a/spec/lib/chatwork/client/file_methods_spec.rb +++ b/spec/lib/chatwork/client/file_methods_spec.rb @@ -34,4 +34,18 @@ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files/{file_id}" end end + + describe "#create_file", type: :api do + subject { client.create_file(room_id: room_id, file: file, message: message, &block) } + + before do + stub_chatwork_request(:post, "/rooms/#{room_id}/files", "/rooms/{room_id}/files") + end + + let(:room_id) { 123 } + let(:file) { Faraday::UploadIO.new("#{spec_dir}/data/upload.txt", "text/plain") } + let(:message) { "I attached comment to chat." } + + it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/files" + end end diff --git a/spec/lib/chatwork/file_spec.rb b/spec/lib/chatwork/file_spec.rb index 0a99654..b147c40 100644 --- a/spec/lib/chatwork/file_spec.rb +++ b/spec/lib/chatwork/file_spec.rb @@ -34,4 +34,18 @@ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files/{file_id}" end end + + describe ".create", type: :api do + subject { ChatWork::File.create(room_id: room_id, file: file, message: message, &block) } + + before do + stub_chatwork_request(:post, "/rooms/#{room_id}/files", "/rooms/{room_id}/files") + end + + let(:room_id) { 123 } + let(:file) { Faraday::UploadIO.new("#{spec_dir}/data/upload.txt", "text/plain") } + let(:message) { "I attached comment to chat." } + + it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/files" + end end