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
@@ -0,0 +1,35 @@
1
+ describe ChatWork::IncomingRequest do
2
+ describe ".get", type: :api do
3
+ subject { ChatWork::IncomingRequest.get }
4
+
5
+ before do
6
+ stub_chatwork_request(:get, "/incoming_requests")
7
+ end
8
+
9
+ it_behaves_like :a_chatwork_api, :get, "/incoming_requests"
10
+ end
11
+
12
+ describe ".update", type: :api do
13
+ subject { ChatWork::IncomingRequest.update(request_id: request_id) }
14
+
15
+ let(:request_id) { 123 }
16
+
17
+ before do
18
+ stub_chatwork_request(:put, "/incoming_requests/#{request_id}", "/incoming_requests/{request_id}")
19
+ end
20
+
21
+ it_behaves_like :a_chatwork_api, :put, "/incoming_requests/{request_id}"
22
+ end
23
+
24
+ describe ".destroy", type: :api do
25
+ subject { ChatWork::IncomingRequest.destroy(request_id: request_id) }
26
+
27
+ let(:request_id) { 123 }
28
+
29
+ before do
30
+ stub_chatwork_request(:delete, "/incoming_requests/#{request_id}", "/incoming_requests/{request_id}", 204)
31
+ end
32
+
33
+ it_behaves_like :a_chatwork_api, :delete, "/incoming_requests/{request_id}", 204
34
+ end
35
+ end
@@ -0,0 +1,39 @@
1
+ describe ChatWork::Me do
2
+ describe ".get", type: :api do
3
+ subject { ChatWork::Me.get }
4
+
5
+ before do
6
+ stub_chatwork_request(:get, "/me")
7
+ end
8
+
9
+ it_behaves_like :a_chatwork_api, :get, "/me"
10
+
11
+ its(:account_id) { should eq 123 }
12
+ its(:room_id) { should eq 322 }
13
+ its(:name) { should eq "John Smith" }
14
+ its(:chatwork_id) { should eq "tarochatworkid" }
15
+ its(:organization_id) { should eq 101 }
16
+ its(:organization_name) { should eq "Hello Company" }
17
+ its(:department) { should eq "Marketing" }
18
+ its(:title) { should eq "CMO" }
19
+ its(:url) { should eq "http://mycompany.example.com" }
20
+ its(:introduction) { should eq "Self Introduction" }
21
+ its(:mail) { should eq "taro@example.com" }
22
+ its(:tel_organization) { should eq "XXX-XXXX-XXXX" }
23
+ its(:tel_extension) { should eq "YYY-YYYY-YYYY" }
24
+ its(:tel_mobile) { should eq "ZZZ-ZZZZ-ZZZZ" }
25
+ its(:skype) { should eq "myskype_id" }
26
+ its(:facebook) { should eq "myfacebook_id" }
27
+ its(:twitter) { should eq "mytwitter_id" }
28
+ its(:avatar_image_url) { should eq "https://example.com/abc.png" }
29
+ its(:login_mail) { should eq "account@example.com" }
30
+
31
+ context "when unauthorized" do
32
+ before do
33
+ stub_chatwork_request(:get, "/me", "/me", 401)
34
+ end
35
+
36
+ it { expect { subject }.to raise_error(ChatWork::APIError, "Invalid API token") }
37
+ end
38
+ end
39
+ end
@@ -1,15 +1,46 @@
1
1
  describe ChatWork::Member do
2
- include_context 'shared stubs'
3
- describe 'get room members' do
4
- it 'should work' do
5
- expect(ChatWork::Member.get(room_id: 1)).to eq([{ "account_id"=> 123, "name"=> "John Smith"}])
2
+ describe ".get", type: :api do
3
+ subject { ChatWork::Member.get(room_id: room_id) }
4
+
5
+ let(:room_id) { 123 }
6
+
7
+ before do
8
+ stub_chatwork_request(:get, "/rooms/#{room_id}/members", "/rooms/{room_id}/members")
6
9
  end
7
10
 
8
- it 'should work with block' do
9
- ChatWork::Member.get(room_id: 1) do |body, headers|
10
- expect(body).to eq([{ "account_id"=> 123, "name"=> "John Smith"}])
11
- expect(headers).to eq({'X-ratelimit-limit'=> '100', 'X-ratelimit-remaining'=> '44'})
12
- end
11
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/members"
12
+ end
13
+
14
+ describe ".update_all", type: :api do
15
+ subject do
16
+ ChatWork::Member.update_all(
17
+ room_id: room_id,
18
+ members_admin_ids: members_admin_ids,
19
+ members_member_ids: members_member_ids,
20
+ members_readonly_ids: members_readonly_ids,
21
+ )
22
+ end
23
+
24
+ let(:room_id) { 123 }
25
+
26
+ before do
27
+ stub_chatwork_request(:put, "/rooms/#{room_id}/members", "/rooms/{room_id}/members")
28
+ end
29
+
30
+ context "with String" do
31
+ let(:members_admin_ids) { "123,542,1001" }
32
+ let(:members_member_ids) { "21,344" }
33
+ let(:members_readonly_ids) { "15,103" }
34
+
35
+ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/members"
36
+ end
37
+
38
+ context "with Array" do
39
+ let(:members_admin_ids) { [123, 542, 1001] }
40
+ let(:members_member_ids) { [21, 344] }
41
+ let(:members_readonly_ids) { [15, 103] }
42
+
43
+ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/members"
13
44
  end
14
45
  end
15
46
  end
@@ -0,0 +1,102 @@
1
+ describe ChatWork::Message do
2
+ describe ".get", type: :api do
3
+ subject { ChatWork::Message.get(room_id: room_id, force: force) }
4
+
5
+ let(:room_id) { 123 }
6
+
7
+ before do
8
+ stub_chatwork_request(:get, "/rooms/#{room_id}/messages", "/rooms/{room_id}/messages")
9
+ end
10
+
11
+ context "when force is Integer" do
12
+ let(:force) { 0 }
13
+
14
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/messages"
15
+ end
16
+
17
+ context "when force is boolean" do
18
+ let(:force) { false }
19
+
20
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/messages"
21
+ end
22
+ end
23
+
24
+ describe ".create", type: :api do
25
+ subject { ChatWork::Message.create(room_id: room_id, body: body) }
26
+
27
+ let(:room_id) { 123 }
28
+ let(:body) { "Hello ChatWork!" }
29
+
30
+ before do
31
+ stub_chatwork_request(:post, "/rooms/#{room_id}/messages", "/rooms/{room_id}/messages")
32
+ end
33
+
34
+ it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/messages"
35
+ end
36
+
37
+ describe ".read", type: :api do
38
+ subject { ChatWork::Message.read(room_id: room_id, message_id: message_id) }
39
+
40
+ let(:room_id) { 123 }
41
+ let(:message_id) { "101" }
42
+
43
+ before do
44
+ stub_chatwork_request(:put, "/rooms/#{room_id}/messages/read", "/rooms/{room_id}/messages/read")
45
+ end
46
+
47
+ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/read"
48
+ end
49
+
50
+ describe ".unread", type: :api do
51
+ subject { ChatWork::Message.unread(room_id: room_id, message_id: message_id) }
52
+
53
+ let(:room_id) { 123 }
54
+ let(:message_id) { "101" }
55
+
56
+ before do
57
+ stub_chatwork_request(:put, "/rooms/#{room_id}/messages/unread", "/rooms/{room_id}/messages/unread")
58
+ end
59
+
60
+ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/unread"
61
+ end
62
+
63
+ describe ".find", type: :api do
64
+ subject { ChatWork::Message.find(room_id: room_id, message_id: message_id) }
65
+
66
+ let(:room_id) { 123 }
67
+ let(:message_id) { "101" }
68
+
69
+ before do
70
+ stub_chatwork_request(:get, "/rooms/#{room_id}/messages/#{message_id}", "/rooms/{room_id}/messages/{message_id}")
71
+ end
72
+
73
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/messages/{message_id}"
74
+ end
75
+
76
+ describe ".update", type: :api do
77
+ subject { ChatWork::Message.update(room_id: room_id, message_id: message_id, body: body) }
78
+
79
+ let(:room_id) { 123 }
80
+ let(:message_id) { "101" }
81
+ let(:body) { "Hello ChatWork!" }
82
+
83
+ before do
84
+ stub_chatwork_request(:put, "/rooms/#{room_id}/messages/#{message_id}", "/rooms/{room_id}/messages/{message_id}")
85
+ end
86
+
87
+ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}/messages/{message_id}"
88
+ end
89
+
90
+ describe ".destroy", type: :api do
91
+ subject { ChatWork::Message.destroy(room_id: room_id, message_id: message_id) }
92
+
93
+ let(:room_id) { 123 }
94
+ let(:message_id) { "101" }
95
+
96
+ before do
97
+ stub_chatwork_request(:delete, "/rooms/#{room_id}/messages/#{message_id}", "/rooms/{room_id}/messages/{message_id}")
98
+ end
99
+
100
+ it_behaves_like :a_chatwork_api, :delete, "/rooms/{room_id}/messages/{message_id}"
101
+ end
102
+ end
@@ -0,0 +1,13 @@
1
+ describe ChatWork::MyStatus do
2
+ describe ".get", type: :api do
3
+ subject do
4
+ ChatWork::MyStatus.get
5
+ end
6
+
7
+ before do
8
+ stub_chatwork_request(:get, "/my/status")
9
+ end
10
+
11
+ it_behaves_like :a_chatwork_api, :get, "/my/status"
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ describe ChatWork::MyTask do
2
+ describe ".get", type: :api do
3
+ subject do
4
+ ChatWork::MyTask.get(
5
+ assigned_by_account_id: assigned_by_account_id,
6
+ status: status,
7
+ )
8
+ end
9
+
10
+ let(:assigned_by_account_id) { 78 }
11
+ let(:status) { "done" }
12
+
13
+ before do
14
+ stub_chatwork_request(:get, "/my/tasks")
15
+ end
16
+
17
+ it_behaves_like :a_chatwork_api, :get, "/my/tasks"
18
+ end
19
+ end
@@ -1,16 +1,16 @@
1
1
  describe ChatWork::OAuthClient do
2
- describe '#initialize' do
2
+ describe "#initialize" do
3
3
  subject { ChatWork::OAuthClient.new(client_id, client_secret, api_base) }
4
4
 
5
- let(:client_id) { 'client_id' }
6
- let(:client_secret) { 'client_secret' }
7
- let(:api_base) { 'https://token.chatwork.com/' }
8
- let(:signature) { 'Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=' }
5
+ let(:client_id) { "client_id" }
6
+ let(:client_secret) { "client_secret" }
7
+ let(:api_base) { "https://token.chatwork.com/" }
8
+ let(:signature) { "Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=" }
9
9
 
10
- it 'client has Authorization header' do
10
+ it "client has Authorization header" do
11
11
  connection = subject.instance_variable_get(:@conn)
12
12
 
13
- expect(connection.headers['Authorization']).to eq "Basic #{signature}"
13
+ expect(connection.headers["Authorization"]).to eq "Basic #{signature}"
14
14
  end
15
15
  end
16
16
  end
@@ -0,0 +1,102 @@
1
+ describe ChatWork::Room do
2
+ describe ".get", type: :api do
3
+ subject { ChatWork::Room.get }
4
+
5
+ let(:room_id) { 123 }
6
+
7
+ before do
8
+ stub_chatwork_request(:get, "/rooms")
9
+ end
10
+
11
+ it_behaves_like :a_chatwork_api, :get, "/rooms"
12
+ end
13
+
14
+ describe ".create", type: :api do
15
+ subject do
16
+ ChatWork::Room.create(
17
+ description: description,
18
+ icon_preset: icon_preset,
19
+ members_admin_ids: members_admin_ids,
20
+ members_member_ids: members_member_ids,
21
+ members_readonly_ids: members_readonly_ids,
22
+ name: name,
23
+ )
24
+ end
25
+
26
+ let(:description) { "group chat description" }
27
+ let(:icon_preset) { "meeting" }
28
+ let(:name) { "Website renewal project" }
29
+
30
+ before do
31
+ stub_chatwork_request(:post, "/rooms")
32
+ end
33
+
34
+ context "with String" do
35
+ let(:members_admin_ids) { "123,542,1001" }
36
+ let(:members_member_ids) { "21,344" }
37
+ let(:members_readonly_ids) { "15,103" }
38
+
39
+ it_behaves_like :a_chatwork_api, :post, "/rooms"
40
+ end
41
+
42
+ context "with Array" do
43
+ let(:members_admin_ids) { [123, 542, 1001] }
44
+ let(:members_member_ids) { [21, 344] }
45
+ let(:members_readonly_ids) { [15, 103] }
46
+
47
+ it_behaves_like :a_chatwork_api, :post, "/rooms"
48
+ end
49
+ end
50
+
51
+ describe ".find", type: :api do
52
+ subject { ChatWork::Room.find(room_id: room_id) }
53
+
54
+ let(:room_id) { 123 }
55
+
56
+ before do
57
+ stub_chatwork_request(:get, "/rooms/#{room_id}", "/rooms/{room_id}")
58
+ end
59
+
60
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}"
61
+ end
62
+
63
+ describe ".update", type: :api do
64
+ subject do
65
+ ChatWork::Room.update(
66
+ room_id: room_id,
67
+ description: description,
68
+ icon_preset: icon_preset,
69
+ name: name,
70
+ )
71
+ end
72
+
73
+ let(:room_id) { 123 }
74
+ let(:description) { "group chat description" }
75
+ let(:icon_preset) { "meeting" }
76
+ let(:name) { "Website renewal project" }
77
+
78
+ before do
79
+ stub_chatwork_request(:put, "/rooms/#{room_id}", "/rooms/{room_id}")
80
+ end
81
+
82
+ it_behaves_like :a_chatwork_api, :put, "/rooms/{room_id}"
83
+ end
84
+
85
+ describe ".destroy", type: :api do
86
+ subject do
87
+ ChatWork::Room.destroy(
88
+ room_id: room_id,
89
+ action_type: action_type,
90
+ )
91
+ end
92
+
93
+ let(:room_id) { 123 }
94
+ let(:action_type) { "leave" }
95
+
96
+ before do
97
+ stub_chatwork_request(:delete, "/rooms/#{room_id}", "/rooms/{room_id}", 204)
98
+ end
99
+
100
+ it_behaves_like :a_chatwork_api, :delete, "/rooms/{room_id}", 204
101
+ end
102
+ end
@@ -0,0 +1,77 @@
1
+ describe ChatWork::Task do
2
+ describe ".get", type: :api do
3
+ subject do
4
+ ChatWork::Task.get(
5
+ room_id: room_id,
6
+ account_id: account_id,
7
+ assigned_by_account_id: assigned_by_account_id,
8
+ status: status,
9
+ )
10
+ end
11
+
12
+ let(:room_id) { 123 }
13
+ let(:account_id) { 101 }
14
+ let(:assigned_by_account_id) { 78 }
15
+ let(:status) { "done" }
16
+
17
+ before do
18
+ stub_chatwork_request(:get, "/rooms/#{room_id}/tasks", "/rooms/{room_id}/tasks")
19
+ end
20
+
21
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/tasks"
22
+ end
23
+
24
+ describe ".create", type: :api do
25
+ subject do
26
+ ChatWork::Task.create(
27
+ room_id: room_id,
28
+ body: body,
29
+ to_ids: to_ids,
30
+ limit: limit,
31
+ )
32
+ end
33
+
34
+ let(:room_id) { 123 }
35
+ let(:body) { "Buy milk" }
36
+ let(:to_ids) { "1,3,6" }
37
+ let(:limit) { "1385996399" }
38
+
39
+ before do
40
+ stub_chatwork_request(:post, "/rooms/#{room_id}/tasks", "/rooms/{room_id}/tasks")
41
+ end
42
+
43
+ context "when to_ids and limit are String" do
44
+ it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/tasks"
45
+ end
46
+
47
+ context "when to_ids is Array" do
48
+ let(:to_ids) { [1, 3, 6] }
49
+
50
+ it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/tasks"
51
+ end
52
+
53
+ context "when limit is Time" do
54
+ let(:limit) { Time.at(1_385_996_399) }
55
+
56
+ it_behaves_like :a_chatwork_api, :post, "/rooms/{room_id}/tasks"
57
+ end
58
+ end
59
+
60
+ describe ".find", type: :api do
61
+ subject do
62
+ ChatWork::Task.find(
63
+ room_id: room_id,
64
+ task_id: task_id,
65
+ )
66
+ end
67
+
68
+ let(:room_id) { 123 }
69
+ let(:task_id) { 3 }
70
+
71
+ before do
72
+ stub_chatwork_request(:get, "/rooms/#{room_id}/tasks/#{task_id}", "/rooms/{room_id}/tasks/{task_id}")
73
+ end
74
+
75
+ it_behaves_like :a_chatwork_api, :get, "/rooms/{room_id}/tasks/{task_id}"
76
+ end
77
+ end