Skip to content

Commit

Permalink
Merge a6f45ea into 73b9fae
Browse files Browse the repository at this point in the history
  • Loading branch information
sue445 committed Jul 18, 2018
2 parents 73b9fae + a6f45ea commit bb54bd7
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api
Submodule api updated 1 files
+44 −0 RAML/api-ja.raml
7 changes: 7 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions chatwork.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/chatwork.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "chatwork/version"
require "hashie"
require "faraday"

module ChatWork
autoload :BaseClient, "chatwork/base_client"
Expand Down
1 change: 1 addition & 0 deletions lib/chatwork/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions lib/chatwork/client/file_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>] 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
31 changes: 31 additions & 0 deletions lib/chatwork/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>] 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
1 change: 1 addition & 0 deletions spec/data/upload.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello ChatWork!
14 changes: 14 additions & 0 deletions spec/lib/chatwork/client/file_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/lib/chatwork/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bb54bd7

Please sign in to comment.