chatwork 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bbc0b45bd43456ece01cee8ee7cb111ddd153c401b5d2bf2c4f3fe994c6ded53
4
- data.tar.gz: da276d6fe8554b88c5627e0183e86070ac09b34dfb991a09beeaf7c58127cf06
3
+ metadata.gz: 3b0e86371dbc01a5f67b01eb15d6681dc2fb5cded54530727da8b9a07d20946c
4
+ data.tar.gz: e79d2f3a743ff131ec83bd81a6e61a16cca8b5c484eae0eb635e23a68b078902
5
5
  SHA512:
6
- metadata.gz: a37b6b08583b45874a91db8f2d5e9a82bb29d4ffbf0aa90606a94f78d0862bd7c3028dfaddcd3bb715f570bcd6a211bc542f2de8361d30eda1fdc91f45df2f46
7
- data.tar.gz: a84e438fb953c64d47cbd7c12925e01acdb52b6fc105bda7cbac45d5e7cdd97a275612626bfdb02ccd11064084388fb823677449676739fd5648af7a65066aee
6
+ metadata.gz: 45468727cbee93fd0192a3912462eda9a0a00a032a4a867a5bf23ab6ed3982496dcd96f2d61f85a4d5cc1f24cd20137e33100a37d41eab451de6873145d34f6b
7
+ data.tar.gz: 89ec1973fdb802b11daf722250246f6bca7b77f963fcfd73d15b7cc07d61c27e852938d9f9bf45334adb69386a3e3236df6d7a790f726fe2c6ff845ea8b35675
@@ -1,6 +1,12 @@
1
1
  # Change Log
2
2
  ## Unreleased
3
- [Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.9.0...master)
3
+ [Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.10.0...master)
4
+
5
+ ## v0.10.0
6
+ [Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.9.0...v0.10.0)
7
+
8
+ * Add file upload method
9
+ * https://github.com/asonas/chatwork-ruby/pull/55
4
10
 
5
11
  ## v0.9.0
6
12
  [Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.8.0...v0.9.0)
@@ -3,8 +3,16 @@
3
3
  require "bundler/setup"
4
4
  require "chatwork"
5
5
  require "dotenv"
6
+ require "faraday_curl"
6
7
 
7
8
  Dotenv.load
8
9
 
10
+ logger = Logger.new(STDOUT)
11
+
12
+ @client = ChatWork::Client.new(api_key: ENV["CHATWORK_API_TOKEN"])
13
+ connection = @client.instance_variable_get(:@conn)
14
+ connection.request :curl, logger, :debug
15
+ connection.response :logger, logger, bodies: true
16
+
9
17
  require "pry"
10
18
  Pry.start
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "bundler", "~> 1.3"
26
26
  spec.add_development_dependency "coveralls"
27
27
  spec.add_development_dependency "dotenv"
28
+ spec.add_development_dependency "faraday_curl"
28
29
  spec.add_development_dependency "onkcop", "0.53.0.0"
29
30
  spec.add_development_dependency "pry-byebug"
30
31
  spec.add_development_dependency "rake"
@@ -1,5 +1,6 @@
1
1
  require "chatwork/version"
2
2
  require "hashie"
3
+ require "faraday"
3
4
 
4
5
  module ChatWork
5
6
  autoload :BaseClient, "chatwork/base_client"
@@ -16,6 +17,7 @@ module ChatWork
16
17
  autoload :Me, "chatwork/me"
17
18
  autoload :Member, "chatwork/member"
18
19
  autoload :Message, "chatwork/message"
20
+ autoload :Multipart, "chatwork/multipart"
19
21
  autoload :MyStatus, "chatwork/my_status"
20
22
  autoload :MyTask, "chatwork/my_task"
21
23
  autoload :OAuthClient, "chatwork/oauth_client"
@@ -31,6 +33,10 @@ module ChatWork
31
33
  @client_id = nil
32
34
  @client_secret = nil
33
35
 
36
+ Faraday::Request.register_middleware(
37
+ chatwork_multipart: -> { ChatWork::Multipart },
38
+ )
39
+
34
40
  class << self
35
41
  # @!attribute [r] api_base
36
42
  # @return [String]
@@ -16,6 +16,7 @@ module ChatWork
16
16
  default_header.merge!(header)
17
17
 
18
18
  @conn = Faraday.new(api_base, headers: default_header) do |builder|
19
+ builder.request :chatwork_multipart
19
20
  builder.request :url_encoded
20
21
  builder.response :mashify
21
22
  builder.response :json
@@ -64,4 +64,39 @@ module ChatWork::Client::FileMethods
64
64
  def find_file(room_id:, file_id:, create_download_url: nil, &block)
65
65
  get("/rooms/#{room_id}/files/#{file_id}", create_download_url: boolean_to_integer(create_download_url), &block)
66
66
  end
67
+
68
+ # Upload a new file to room
69
+ #
70
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-files
71
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
72
+ #
73
+ # @param room_id [Integer]
74
+ # @param file [Faraday::UploadIO]
75
+ # @param message [String]
76
+ #
77
+ # @yield [response_body, response_header] if block was given, return response body and response header through block arguments
78
+ # @yieldparam response_body [Hashie::Mash] response body
79
+ # @yieldparam response_header [Hash<String, String>] response header (e.g. X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset)
80
+ #
81
+ # @return [Hashie::Mash]
82
+ #
83
+ # @example how to upload a file
84
+ # client = ChatWork::Client.new(api_key: "XXX")
85
+ # client.create_file(room_id: 11111111, file: Faraday::UploadIO.new("/path/to/file.txt", "text/plain"), message: "Test")
86
+ #
87
+ # @example response format
88
+ # {
89
+ # "file_id": 1234
90
+ # }
91
+ #
92
+ def create_file(room_id:, file:, message: nil, &block)
93
+ params = {
94
+ file: file,
95
+ message: message,
96
+ }
97
+
98
+ post("/rooms/#{room_id}/files", params, &block)
99
+ end
100
+
101
+ alias_method :upload_file, :create_file
67
102
  end
@@ -65,5 +65,36 @@ module ChatWork
65
65
  def self.find(room_id:, file_id:, create_download_url: nil, &block)
66
66
  ChatWork.client.find_file(room_id: room_id, file_id: file_id, create_download_url: create_download_url, &block)
67
67
  end
68
+
69
+ # Upload a new file to room
70
+ #
71
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-files
72
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
73
+ #
74
+ # @param room_id [Integer]
75
+ # @param file [Faraday::UploadIO]
76
+ # @param message [String]
77
+ #
78
+ # @yield [response_body, response_header] if block was given, return response body and response header through block arguments
79
+ # @yieldparam response_body [Hashie::Mash] response body
80
+ # @yieldparam response_header [Hash<String, String>] response header (e.g. X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset)
81
+ #
82
+ # @return [Hashie::Mash]
83
+ #
84
+ # @example how to upload a file
85
+ # ChatWork::File.create(room_id: 11111111, file: Faraday::UploadIO.new("/path/to/file.txt", "text/plain"), message: "Test")
86
+ #
87
+ # @example response format
88
+ # {
89
+ # "file_id": 1234
90
+ # }
91
+ #
92
+ def self.create(room_id:, file:, message: nil, &block)
93
+ ChatWork.client.create_file(room_id: room_id, file: file, message: message, &block)
94
+ end
95
+
96
+ class << self
97
+ alias_method :upload, :create
98
+ end
68
99
  end
69
100
  end
@@ -0,0 +1,27 @@
1
+ module ChatWork
2
+ class Multipart < ::Faraday::Request::Multipart
3
+ self.mime_type = "multipart/form-data".freeze
4
+
5
+ def create_multipart(env, params)
6
+ original_body = super(env, params)
7
+
8
+ # replace from Faraday::Parts::EpiloguePart to ChatWork::Multipart::EpiloguePart
9
+ parts = original_body.instance_variable_get(:@parts)
10
+ parts.pop
11
+ parts << EpiloguePart.new(env.request.boundary)
12
+
13
+ new_body = ::Faraday::CompositeReadIO.new(*parts)
14
+ env.request_headers[Faraday::Env::ContentLength] = new_body.length.to_s
15
+ new_body
16
+ end
17
+ end
18
+
19
+ class EpiloguePart < ::Faraday::Parts::EpiloguePart
20
+ def initialize(boundary)
21
+ # NOTE: ChatWork doesn't accepts ending "\r\n\r\n" of payload...
22
+ # @part = "--#{boundary}--\r\n\r\n"
23
+ @part = "--#{boundary}--\r\n"
24
+ @io = StringIO.new(@part)
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module ChatWork
2
- VERSION = "0.9.0".freeze
2
+ VERSION = "0.10.0".freeze
3
3
  end
@@ -0,0 +1 @@
1
+ Hello ChatWork!
@@ -34,4 +34,18 @@ describe ChatWork::Client::FileMethods do
34
34
  it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files/{file_id}"
35
35
  end
36
36
  end
37
+
38
+ describe "#create_file", type: :api do
39
+ subject { client.create_file(room_id: room_id, file: file, message: message, &block) }
40
+
41
+ before do
42
+ stub_chatwork_request(:post, "/rooms/#{room_id}/files", "/rooms/{room_id}/files")
43
+ end
44
+
45
+ let(:room_id) { 123 }
46
+ let(:file) { Faraday::UploadIO.new("#{spec_dir}/data/upload.txt", "text/plain") }
47
+ let(:message) { "I attached comment to chat." }
48
+
49
+ it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/files"
50
+ end
37
51
  end
@@ -34,4 +34,18 @@ describe ChatWork::File do
34
34
  it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files/{file_id}"
35
35
  end
36
36
  end
37
+
38
+ describe ".create", type: :api do
39
+ subject { ChatWork::File.create(room_id: room_id, file: file, message: message, &block) }
40
+
41
+ before do
42
+ stub_chatwork_request(:post, "/rooms/#{room_id}/files", "/rooms/{room_id}/files")
43
+ end
44
+
45
+ let(:room_id) { 123 }
46
+ let(:file) { Faraday::UploadIO.new("#{spec_dir}/data/upload.txt", "text/plain") }
47
+ let(:message) { "I attached comment to chat." }
48
+
49
+ it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/files"
50
+ end
37
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chatwork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - asonas
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-07-08 00:00:00.000000000 Z
12
+ date: 2018-08-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -109,6 +109,20 @@ dependencies:
109
109
  - - ">="
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: faraday_curl
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
112
126
  - !ruby/object:Gem::Dependency
113
127
  name: onkcop
114
128
  requirement: !ruby/object:Gem::Requirement
@@ -294,6 +308,7 @@ files:
294
308
  - lib/chatwork/me.rb
295
309
  - lib/chatwork/member.rb
296
310
  - lib/chatwork/message.rb
311
+ - lib/chatwork/multipart.rb
297
312
  - lib/chatwork/my_status.rb
298
313
  - lib/chatwork/my_task.rb
299
314
  - lib/chatwork/oauth_client.rb
@@ -302,6 +317,7 @@ files:
302
317
  - lib/chatwork/task.rb
303
318
  - lib/chatwork/token.rb
304
319
  - lib/chatwork/version.rb
320
+ - spec/data/upload.txt
305
321
  - spec/lib/chatwork/chatwork_error_spec.rb
306
322
  - spec/lib/chatwork/client/contacts_methods_spec.rb
307
323
  - spec/lib/chatwork/client/file_methods_spec.rb
@@ -361,6 +377,7 @@ signing_key:
361
377
  specification_version: 4
362
378
  summary: Ruby bindings of ChatWork API
363
379
  test_files:
380
+ - spec/data/upload.txt
364
381
  - spec/lib/chatwork/chatwork_error_spec.rb
365
382
  - spec/lib/chatwork/client/contacts_methods_spec.rb
366
383
  - spec/lib/chatwork/client/file_methods_spec.rb