chatwork 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  2. data/.env.example +4 -0
  3. data/.gitignore +1 -0
  4. data/.gitmodules +3 -0
  5. data/.rubocop.yml +10 -0
  6. data/.travis.yml +26 -0
  7. data/.yardopts +2 -0
  8. data/CHANGELOG.md +33 -1
  9. data/Gemfile +13 -1
  10. data/README.md +15 -2
  11. data/Rakefile +2 -2
  12. data/bin/console +10 -0
  13. data/chatwork.gemspec +20 -9
  14. data/lib/chatwork.rb +32 -36
  15. data/lib/chatwork/base_client.rb +8 -11
  16. data/lib/chatwork/chatwork_error.rb +6 -16
  17. data/lib/chatwork/client.rb +2 -2
  18. data/lib/chatwork/contacts.rb +24 -8
  19. data/lib/chatwork/entity_methods.rb +36 -0
  20. data/lib/chatwork/file.rb +63 -0
  21. data/lib/chatwork/incoming_request.rb +71 -0
  22. data/lib/chatwork/me.rb +33 -8
  23. data/lib/chatwork/member.rb +55 -4
  24. data/lib/chatwork/message.rb +147 -6
  25. data/lib/chatwork/my_status.rb +25 -0
  26. data/lib/chatwork/my_task.rb +36 -8
  27. data/lib/chatwork/oauth_client.rb +3 -3
  28. data/lib/chatwork/room.rb +119 -6
  29. data/lib/chatwork/task.rb +96 -4
  30. data/lib/chatwork/token.rb +2 -2
  31. data/lib/chatwork/version.rb +1 -1
  32. data/spec/lib/chatwork/chatwork_error_spec.rb +10 -8
  33. data/spec/lib/chatwork/client_spec.rb +14 -14
  34. data/spec/lib/chatwork/contacts_spec.rb +11 -0
  35. data/spec/lib/chatwork/entity_methods_spec.rb +21 -0
  36. data/spec/lib/chatwork/file_spec.rb +37 -0
  37. data/spec/lib/chatwork/incoming_request_spec.rb +35 -0
  38. data/spec/lib/chatwork/me_spec.rb +39 -0
  39. data/spec/lib/chatwork/member_spec.rb +40 -9
  40. data/spec/lib/chatwork/message_spec.rb +102 -0
  41. data/spec/lib/chatwork/my_status_spec.rb +13 -0
  42. data/spec/lib/chatwork/my_task_spec.rb +19 -0
  43. data/spec/lib/chatwork/oauth_client_spec.rb +7 -7
  44. data/spec/lib/chatwork/room_spec.rb +102 -0
  45. data/spec/lib/chatwork/task_spec.rb +77 -0
  46. data/spec/lib/chatwork/token_spec.rb +26 -7
  47. data/spec/lib/chatwork_spec.rb +26 -19
  48. data/spec/lib/support/utils/raml_parser_spec.rb +96 -0
  49. data/spec/spec_helper.rb +32 -6
  50. data/spec/support/contexts/api_context.rb +43 -0
  51. data/spec/support/examples/a_chatwork_api.rb +12 -0
  52. data/spec/support/matchers/match_example.rb +16 -0
  53. data/spec/support/utils/raml_parser.rb +86 -0
  54. metadata +214 -10
  55. data/lib/chatwork/entity.rb +0 -29
  56. data/lib/chatwork/operations.rb +0 -48
  57. data/spec/shared_oauth_stubs.rb +0 -49
  58. data/spec/shared_stubs.rb +0 -39
@@ -1,13 +1,41 @@
1
1
  module ChatWork
2
- class MyTask < Entity
3
- install_class_operations :get
2
+ module MyTask
3
+ extend EntityMethods
4
4
 
5
- def self.path
6
- "/my/tasks"
7
- end
8
-
9
- def path
10
- "/my/tasks"
5
+ # Get the list of all unfinished tasks
6
+ #
7
+ # (*This method returns up to 100 entries. We are planning to implement pagination to support larger number of data retrieval)
8
+ #
9
+ # @see http://developer.chatwork.com/ja/endpoint_my.html#GET-my-tasks
10
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
11
+ #
12
+ # @param assigned_by_account_id [Integer] Account ID of the person who assigned task
13
+ # @param status [String] Task status (open, done)
14
+ #
15
+ # @return [Array<Hashie::Mash>]
16
+ #
17
+ # @example response format
18
+ # [
19
+ # {
20
+ # "task_id": 3,
21
+ # "room": {
22
+ # "room_id": 5,
23
+ # "name": "Group Chat Name",
24
+ # "icon_path": "https://example.com/ico_group.png"
25
+ # },
26
+ # "assigned_by_account": {
27
+ # "account_id": 456,
28
+ # "name": "Anna",
29
+ # "avatar_image_url": "https://example.com/def.png"
30
+ # },
31
+ # "message_id": "13",
32
+ # "body": "buy milk",
33
+ # "limit_time": 1384354799,
34
+ # "status": "open"
35
+ # }
36
+ # ]
37
+ def self.get(assigned_by_account_id: nil, status: nil)
38
+ _get("/my/tasks", assigned_by_account_id: assigned_by_account_id, status: status)
11
39
  end
12
40
  end
13
41
  end
@@ -1,10 +1,10 @@
1
- require 'base64'
1
+ require "base64"
2
2
 
3
3
  module ChatWork
4
4
  class OAuthClient < BaseClient
5
5
  def initialize(client_id, client_secret, oauth_api_base)
6
- signature = Base64.encode64("#{client_id}:#{client_secret}").gsub("\n", "")
7
- super(oauth_api_base, "", {'Authorization' => "Basic #{signature}"})
6
+ signature = Base64.encode64("#{client_id}:#{client_secret}").delete("\n")
7
+ super(oauth_api_base, "", { "Authorization" => "Basic #{signature}" })
8
8
  end
9
9
  end
10
10
  end
@@ -1,13 +1,126 @@
1
1
  module ChatWork
2
- class Room < Entity
3
- install_class_operations :create, :get
2
+ module Room
3
+ extend EntityMethods
4
4
 
5
- def self.path
6
- "/rooms"
5
+ # Get the list of all chats on your account
6
+ #
7
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms
8
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
9
+ #
10
+ # @return [Array<Hashie::Mash>]
11
+ #
12
+ # @example response format
13
+ # [
14
+ # {
15
+ # "room_id": 123,
16
+ # "name": "Group Chat Name",
17
+ # "type": "group",
18
+ # "role": "admin",
19
+ # "sticky": false,
20
+ # "unread_num": 10,
21
+ # "mention_num": 1,
22
+ # "mytask_num": 0,
23
+ # "message_num": 122,
24
+ # "file_num": 10,
25
+ # "task_num": 17,
26
+ # "icon_path": "https://example.com/ico_group.png",
27
+ # "last_update_time": 1298905200
28
+ # }
29
+ # ]
30
+ def self.get
31
+ _get("/rooms")
7
32
  end
8
33
 
9
- def path
10
- "/rooms"
34
+ # rubocop:disable Metrics/ParameterLists
35
+
36
+ # Create a new group chat
37
+ #
38
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms
39
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
40
+ #
41
+ # @param description [String] Description of the group chat
42
+ # @param icon_preset [String] Type of the group chat icon (group, check, document, meeting, event, project, business,
43
+ # study, security, star, idea, heart, magcup, beer, music, sports, travel)
44
+ # @param members_admin_ids [Array<Integer>, String] List of user IDs who will be given administrator permission for the group chat.
45
+ # At least one user must be specified as an administrator.
46
+ # @param members_member_ids [Array<Integer>, String] List of user IDs who will be given member permission for the group chat.
47
+ # @param members_readonly_ids [Array<Integer>, String] List of user IDs who will be given read-only permission for the group chat.
48
+ # @param name [String] Title of the group chat.
49
+ #
50
+ # @return [Hashie::Mash]
51
+ #
52
+ # @example response format
53
+ # {
54
+ # "room_id": 1234
55
+ # }
56
+ def self.create(description: nil, icon_preset: nil, members_admin_ids:, members_member_ids: nil, members_readonly_ids: nil, name:)
57
+ params = {
58
+ description: description,
59
+ icon_preset: icon_preset,
60
+ members_admin_ids: Array(members_admin_ids).join(","),
61
+ name: name,
62
+ }
63
+ params[:members_member_ids] = Array(members_member_ids).join(",") if members_member_ids
64
+ params[:members_readonly_ids] = Array(members_readonly_ids).join(",") if members_readonly_ids
65
+
66
+ _post("/rooms", params)
67
+ end
68
+
69
+ # rubocop:enable Metrics/ParameterLists
70
+
71
+ # Get chat name, icon, and Type (my, direct, or group)
72
+ #
73
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id
74
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
75
+ #
76
+ # @param room_id [Integer]
77
+ #
78
+ # @return [Hashie::Mash]
79
+ #
80
+ # @example response format
81
+ # {
82
+ # "room_id": 123,
83
+ # "name": "Group Chat Name",
84
+ # "type": "group",
85
+ # "role": "admin",
86
+ # "sticky": false,
87
+ # "unread_num": 10,
88
+ # "mention_num": 1,
89
+ # "mytask_num": 0,
90
+ # "message_num": 122,
91
+ # "file_num": 10,
92
+ # "task_num": 17,
93
+ # "icon_path": "https://example.com/ico_group.png",
94
+ # "last_update_time": 1298905200,
95
+ # "description": "room description text"
96
+ # }
97
+ def self.find(room_id:)
98
+ _get("/rooms/#{room_id}")
99
+ end
100
+
101
+ # Change the title and icon type of the specified chat
102
+ #
103
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id
104
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
105
+ #
106
+ # @param room_id [Integer]
107
+ # @param description [String] Description of the group chat
108
+ # @param icon_preset [String] Type of the group chat icon (group, check, document, meeting, event, project, business,
109
+ # study, security, star, idea, heart, magcup, beer, music, sports, travel)
110
+ # @param name [String] Title of the group chat.
111
+ def self.update(room_id:, description: nil, icon_preset: nil, name: nil)
112
+ _put("/rooms/#{room_id}", description: description, icon_preset: icon_preset, name: name)
113
+ end
114
+
115
+ # Leave/Delete a group chat
116
+ #
117
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#DELETE-rooms-room_id
118
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
119
+ #
120
+ # @param room_id [Integer]
121
+ # @param action_type [String] leave from a room or delete a room (leave, delete)
122
+ def self.destroy(room_id:, action_type:)
123
+ _delete("/rooms/#{room_id}", action_type: action_type)
11
124
  end
12
125
  end
13
126
  end
@@ -1,9 +1,101 @@
1
1
  module ChatWork
2
- class Task < Entity
3
- install_class_operations :get, :create
2
+ module Task
3
+ extend EntityMethods
4
4
 
5
- def self.path
6
- "/rooms/%d/tasks"
5
+ # Get the list of tasks associated with the specified chat
6
+ #
7
+ # (*This method returns up to 100 entries. We are planning to implement pagination to support larger number of data retrieval)
8
+ #
9
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks
10
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
11
+ #
12
+ # @param room_id [Integer]
13
+ # @param account_id [Integer]
14
+ # @param assigned_by_account_id [Integer] Account ID of the person who assigned task
15
+ # @param status [String] Task status (open, done)
16
+ #
17
+ # @return [Array<Hashie::Mash>]
18
+ #
19
+ # @example response format
20
+ # [
21
+ # {
22
+ # "task_id": 3,
23
+ # "account": {
24
+ # "account_id": 123,
25
+ # "name": "Bob",
26
+ # "avatar_image_url": "https://example.com/abc.png"
27
+ # },
28
+ # "assigned_by_account": {
29
+ # "account_id": 456,
30
+ # "name": "Anna",
31
+ # "avatar_image_url": "https://example.com/def.png"
32
+ # },
33
+ # "message_id": "13",
34
+ # "body": "buy milk",
35
+ # "limit_time": 1384354799,
36
+ # "status": "open"
37
+ # }
38
+ # ]
39
+ def self.get(room_id:, account_id:, assigned_by_account_id: nil, status: nil)
40
+ _get("/rooms/#{room_id}/tasks", account_id: account_id, assigned_by_account_id: assigned_by_account_id, status: status)
41
+ end
42
+
43
+ # Add a new task to the chat
44
+ #
45
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-tasks
46
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
47
+ #
48
+ # @param room_id [Integer]
49
+ # @param body [String] Task description
50
+ # @param to_ids [Array<Integer>, String] Account ID of the person/people responsible to complete the task
51
+ # @param limit [Time, Integer] When the task is due
52
+ #
53
+ # @return [Hashie::Mash]
54
+ #
55
+ # @example response format
56
+ # {
57
+ # "task_ids": [123,124]
58
+ # }
59
+ def self.create(room_id:, body:, to_ids:, limit: nil)
60
+ params = {
61
+ body: body,
62
+ to_ids: Array(to_ids).join(","),
63
+ }
64
+ params[:limit] = limit.to_i if limit
65
+
66
+ _post("/rooms/#{room_id}/tasks", params)
67
+ end
68
+
69
+ # Get information about the specified task
70
+ #
71
+ # @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks-task_id
72
+ # @see http://download.chatwork.com/ChatWork_API_Documentation.pdf
73
+ #
74
+ # @param room_id [Integer]
75
+ # @param task_id [Integer]
76
+ #
77
+ # @return [Hashie::Mash]
78
+ #
79
+ # @example response format
80
+ # {
81
+ # "task_id": 3,
82
+ # "account": {
83
+ # "account_id": 123,
84
+ # "name": "Bob",
85
+ # "avatar_image_url": "https://example.com/abc.png"
86
+ # },
87
+ # "assigned_by_account": {
88
+ # "account_id": 456,
89
+ # "name": "Anna",
90
+ # "avatar_image_url": "https://example.com/def.png"
91
+ # },
92
+ # "message_id": "13",
93
+ # "body": "buy milk",
94
+ # "limit_time": 1384354799,
95
+ # "status": "open"
96
+ # }
97
+ def self.find(room_id:, task_id:)
98
+ _get("/rooms/#{room_id}/tasks/#{task_id}")
7
99
  end
8
100
  end
9
101
  end
@@ -1,5 +1,5 @@
1
1
  module ChatWork
2
- class Token
2
+ module Token
3
3
  # refresh access_token with refresh_token
4
4
  #
5
5
  # @param refresh_token [String]
@@ -23,7 +23,7 @@ module ChatWork
23
23
  params[:scope] = scope.join(" ") unless scope.empty?
24
24
 
25
25
  response = ChatWork.oauth_client.post("/token", params)
26
- raise response if response.kind_of?(Exception)
26
+ raise response if response.is_a?(Exception)
27
27
  response
28
28
  end
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module ChatWork
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0".freeze
3
3
  end
@@ -1,26 +1,28 @@
1
1
  describe ChatWork::ChatWorkError do
2
- describe '.from_response' do
2
+ describe ".from_response" do
3
3
  subject { ChatWork::ChatWorkError.from_response(status, body, headers) }
4
4
 
5
5
  context "with WWW-Authenticate header" do
6
6
  let(:status) { 401 }
7
7
 
8
8
  let(:body) do
9
- <<-JSON
10
- {"errors":["Invalid API Token"]}
11
- JSON
9
+ json =
10
+ <<-JSON
11
+ {"errors":["Invalid API Token"]}
12
+ JSON
13
+ Hashie::Mash.new(JSON.parse(json))
12
14
  end
13
15
 
14
16
  let(:headers) do
15
17
  {
16
- 'WWW-Authenticate' => 'Bearer error="invalid_token", error_description="The access token expired"'
18
+ "WWW-Authenticate" => 'Bearer error="invalid_token", error_description="The access token expired"',
17
19
  }
18
20
  end
19
21
 
20
22
  it { should be_an_instance_of ChatWork::AuthenticateError }
21
- its(:error) { should eq 'invalid_token' }
22
- its(:error_description) { should eq 'The access token expired' }
23
- its(:error_response) { should eq ['Invalid API Token'] }
23
+ its(:error) { should eq "invalid_token" }
24
+ its(:error_description) { should eq "The access token expired" }
25
+ its(:error_response) { should eq ["Invalid API Token"] }
24
26
  end
25
27
  end
26
28
  end
@@ -1,34 +1,34 @@
1
1
  describe ChatWork::Client do
2
- describe '#initialize' do
2
+ describe "#initialize" do
3
3
  subject { ChatWork::Client.new(api_key, access_token, api_base, api_version) }
4
4
 
5
5
  let(:api_key) { nil }
6
6
  let(:access_token) { nil }
7
- let(:api_base) { 'https://api.chatwork.com/' }
8
- let(:api_version) { '/v2' }
7
+ let(:api_base) { "https://api.chatwork.com/" }
8
+ let(:api_version) { "/v2" }
9
9
 
10
- context 'with api_key' do
11
- let(:api_key) { 'my_api_key' }
10
+ context "with api_key" do
11
+ let(:api_key) { "my_api_key" }
12
12
 
13
- it 'client has X-ChatWorkToken header' do
13
+ it "client has X-ChatWorkToken header" do
14
14
  connection = subject.instance_variable_get(:@conn)
15
15
 
16
- expect(connection.headers['X-ChatWorkToken']).to eq "my_api_key"
16
+ expect(connection.headers["X-ChatWorkToken"]).to eq "my_api_key"
17
17
  end
18
18
  end
19
19
 
20
- context 'with access_token' do
21
- let(:access_token) { 'my_access_token' }
20
+ context "with access_token" do
21
+ let(:access_token) { "my_access_token" }
22
22
 
23
- it 'client has Authorization header' do
23
+ it "client has Authorization header" do
24
24
  connection = subject.instance_variable_get(:@conn)
25
25
 
26
- expect(connection.headers['Authorization']).to eq "Bearer my_access_token"
26
+ expect(connection.headers["Authorization"]).to eq "Bearer my_access_token"
27
27
  end
28
28
  end
29
29
 
30
- context 'without both api_key and access_token' do
31
- it { expect { subject }.to raise_error 'Either api_key or access_token is required' }
30
+ context "without both api_key and access_token" do
31
+ it { expect { subject }.to raise_error "Either api_key or access_token is required" }
32
32
  end
33
33
  end
34
- end
34
+ end
@@ -0,0 +1,11 @@
1
+ describe ChatWork::Contacts do
2
+ describe ".get", type: :api do
3
+ subject { ChatWork::Contacts.get }
4
+
5
+ before do
6
+ stub_chatwork_request(:get, "/contacts")
7
+ end
8
+
9
+ it_behaves_like :a_chatwork_api, :get, "contacts"
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ describe ChatWork::EntityMethods do
2
+ include ChatWork::EntityMethods
3
+
4
+ describe "#boolean_to_integer" do
5
+ subject { boolean_to_integer(value) }
6
+
7
+ using RSpec::Parameterized::TableSyntax
8
+
9
+ where(:value, :expected) do
10
+ false | 0
11
+ true | 1
12
+ 0 | 0
13
+ 1 | 1
14
+ nil | nil
15
+ end
16
+
17
+ with_them do
18
+ it { should eq expected }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ describe ChatWork::File do
2
+ describe ".get", type: :api do
3
+ subject { ChatWork::File.get(room_id: room_id, account_id: account_id) }
4
+
5
+ before do
6
+ stub_chatwork_request(:get, "/rooms/#{room_id}/files", "/rooms/{room_id}/files")
7
+ end
8
+
9
+ let(:room_id) { 123 }
10
+ let(:account_id) { 101 }
11
+
12
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files"
13
+ end
14
+
15
+ describe ".find", type: :api do
16
+ subject { ChatWork::File.find(room_id: room_id, file_id: file_id, create_download_url: create_download_url) }
17
+
18
+ before do
19
+ stub_chatwork_request(:get, "/rooms/#{room_id}/files/#{file_id}", "/rooms/{room_id}/files/{file_id}")
20
+ end
21
+
22
+ let(:room_id) { 123 }
23
+ let(:file_id) { 101 }
24
+
25
+ context "when force is Integer" do
26
+ let(:create_download_url) { 1 }
27
+
28
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files/{file_id}"
29
+ end
30
+
31
+ context "when force is boolean" do
32
+ let(:create_download_url) { true }
33
+
34
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/files/{file_id}"
35
+ end
36
+ end
37
+ end