typetalk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +4 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +215 -0
  7. data/Rakefile +7 -0
  8. data/lib/typetalk.rb +36 -0
  9. data/lib/typetalk/api.rb +49 -0
  10. data/lib/typetalk/api/auth.rb +43 -0
  11. data/lib/typetalk/api/mention.rb +28 -0
  12. data/lib/typetalk/api/message.rb +105 -0
  13. data/lib/typetalk/api/notification.rb +77 -0
  14. data/lib/typetalk/api/topic.rb +56 -0
  15. data/lib/typetalk/api/user.rb +17 -0
  16. data/lib/typetalk/connection.rb +31 -0
  17. data/lib/typetalk/error.rb +7 -0
  18. data/lib/typetalk/version.rb +3 -0
  19. data/spec/cassettes/Typetalk_Api_Auth/_get_access_token/should_get_the_correct_resource_by_authorization_code.yml +40 -0
  20. data/spec/cassettes/Typetalk_Api_Auth/_get_access_token/should_get_the_correct_resource_by_client_credentials.yml +40 -0
  21. data/spec/cassettes/Typetalk_Api_Auth/_get_access_token/should_get_the_correct_resource_by_client_credentials_when_scope_changed.yml +40 -0
  22. data/spec/cassettes/Typetalk_Api_Auth/_get_access_token/should_raise_error_when_authorization_code_is_wrong.yml +40 -0
  23. data/spec/cassettes/Typetalk_Api_Auth/_get_access_token/should_raise_error_when_client_id_is_wrong.yml +40 -0
  24. data/spec/cassettes/Typetalk_Api_Auth/_get_access_token/should_raise_error_when_client_secret_is_wrong.yml +40 -0
  25. data/spec/cassettes/Typetalk_Api_Auth/_get_access_token/should_raise_error_when_redirect_uri_mismatch.yml +40 -0
  26. data/spec/cassettes/Typetalk_Api_Mention/_get_mentions/should_get_the_correct_resource.yml +85 -0
  27. data/spec/cassettes/Typetalk_Api_Mention/_get_mentions/should_get_the_correct_resource_till_mention.yml +83 -0
  28. data/spec/cassettes/Typetalk_Api_Mention/_get_mentions/should_get_the_unread_resource.yml +83 -0
  29. data/spec/cassettes/Typetalk_Api_Mention/_read_mention/should_get_the_correct_resource.yml +83 -0
  30. data/spec/cassettes/Typetalk_Api_Mention/_read_mention/should_raise_error_when_mention_id_is_wrong.yml +73 -0
  31. data/spec/cassettes/Typetalk_Api_Message/_get_message/should_get_the_correct_resource.yml +129 -0
  32. data/spec/cassettes/Typetalk_Api_Message/_get_message/should_raise_error_when_post_id_is_wrong.yml +71 -0
  33. data/spec/cassettes/Typetalk_Api_Message/_get_message/should_raise_error_when_topic_id_is_wrong.yml +71 -0
  34. data/spec/cassettes/Typetalk_Api_Message/_like_message/should_get_the_correct_resource.yml +161 -0
  35. data/spec/cassettes/Typetalk_Api_Message/_like_message/should_raise_error_when_post_id_is_wrong.yml +73 -0
  36. data/spec/cassettes/Typetalk_Api_Message/_post_message/should_get_the_correct_resource.yml +85 -0
  37. data/spec/cassettes/Typetalk_Api_Message/_post_message/should_get_the_correct_resource_for_attachments.yml +536 -0
  38. data/spec/cassettes/Typetalk_Api_Message/_post_message/should_get_the_correct_resource_for_reply.yml +130 -0
  39. data/spec/cassettes/Typetalk_Api_Message/_post_message/should_get_the_correct_resource_for_talks.yml +88 -0
  40. data/spec/cassettes/Typetalk_Api_Message/_post_message/should_raise_error_when_topic_id_is_wrong.yml +73 -0
  41. data/spec/cassettes/Typetalk_Api_Message/_read_message/should_get_the_correct_resource.yml +171 -0
  42. data/spec/cassettes/Typetalk_Api_Message/_read_message/should_get_the_correct_resource_till_post.yml +171 -0
  43. data/spec/cassettes/Typetalk_Api_Message/_read_message/should_raise_error_when_post_id_is_wrong.yml +73 -0
  44. data/spec/cassettes/Typetalk_Api_Message/_remove_message/should_get_the_correct_resource.yml +199 -0
  45. data/spec/cassettes/Typetalk_Api_Message/_remove_message/should_raise_error_when_post_id_is_wrong.yml +71 -0
  46. data/spec/cassettes/Typetalk_Api_Message/_unlike_message/should_get_the_correct_resource.yml +200 -0
  47. data/spec/cassettes/Typetalk_Api_Message/_unlike_message/should_raise_error_when_post_id_is_wrong.yml +71 -0
  48. data/spec/cassettes/Typetalk_Api_Message/_upload_attachment/should_get_the_correct_resource.yml +258 -0
  49. data/spec/cassettes/Typetalk_Api_Message/_upload_attachment/should_raise_error_when_topic_id_is_wrong.yml +250 -0
  50. data/spec/cassettes/Typetalk_Api_Notification/_accept_team/should_get_the_correct_resource.yml +119 -0
  51. data/spec/cassettes/Typetalk_Api_Notification/_accept_team/should_raise_error_when_invite_team_id_is_wrong.yml +73 -0
  52. data/spec/cassettes/Typetalk_Api_Notification/_accept_team/should_raise_error_when_team_id_is_wrong.yml +73 -0
  53. data/spec/cassettes/Typetalk_Api_Notification/_accept_topic/should_get_the_correct_resource.yml +120 -0
  54. data/spec/cassettes/Typetalk_Api_Notification/_accept_topic/should_raise_error_when_invite_topic_id_is_wrong.yml +73 -0
  55. data/spec/cassettes/Typetalk_Api_Notification/_accept_topic/should_raise_error_when_topic_id_is_wrong.yml +73 -0
  56. data/spec/cassettes/Typetalk_Api_Notification/_decline_team/should_get_the_correct_resource.yml +118 -0
  57. data/spec/cassettes/Typetalk_Api_Notification/_decline_team/should_raise_error_when_invite_team_id_is_wrong.yml +73 -0
  58. data/spec/cassettes/Typetalk_Api_Notification/_decline_team/should_raise_error_when_team_id_is_wrong.yml +73 -0
  59. data/spec/cassettes/Typetalk_Api_Notification/_decline_topic/should_get_the_correct_resource.yml +120 -0
  60. data/spec/cassettes/Typetalk_Api_Notification/_decline_topic/should_raise_error_when_invite_topic_id_is_wrong.yml +73 -0
  61. data/spec/cassettes/Typetalk_Api_Notification/_decline_topic/should_raise_error_when_topic_id_is_wrong.yml +73 -0
  62. data/spec/cassettes/Typetalk_Api_Notification/_get_notifications/should_get_the_correct_resource.yml +101 -0
  63. data/spec/cassettes/Typetalk_Api_Notification/_get_notifications_status/should_get_the_correct_resource.yml +79 -0
  64. data/spec/cassettes/Typetalk_Api_Notification/_read_notifications/should_get_the_correct_resource.yml +81 -0
  65. data/spec/cassettes/Typetalk_Api_Topic/_favorite_topic/should_get_the_correct_resource.yml +82 -0
  66. data/spec/cassettes/Typetalk_Api_Topic/_favorite_topic/should_raise_error_when_topic_id_is_wrong.yml +73 -0
  67. data/spec/cassettes/Typetalk_Api_Topic/_get_topic/should_get_the_correct_backward_resource.yml +85 -0
  68. data/spec/cassettes/Typetalk_Api_Topic/_get_topic/should_get_the_correct_forward_resource.yml +84 -0
  69. data/spec/cassettes/Typetalk_Api_Topic/_get_topic/should_get_the_correct_resource.yml +88 -0
  70. data/spec/cassettes/Typetalk_Api_Topic/_get_topic/should_raise_error_when_topic_id_is_wrong.yml +71 -0
  71. data/spec/cassettes/Typetalk_Api_Topic/_get_topic_members/should_get_the_correct_resource.yml +81 -0
  72. data/spec/cassettes/Typetalk_Api_Topic/_get_topic_members/should_raise_error_when_topic_id_is_wrong.yml +71 -0
  73. data/spec/cassettes/Typetalk_Api_Topic/_get_topics/should_get_the_correct_resource.yml +83 -0
  74. data/spec/cassettes/Typetalk_Api_Topic/_unfavorite_topic/should_get_the_correct_resource.yml +80 -0
  75. data/spec/cassettes/Typetalk_Api_Topic/_unfavorite_topic/should_raise_error_when_topic_id_is_wrong.yml +71 -0
  76. data/spec/cassettes/Typetalk_Api_User/_get_profile/should_get_the_correct_resource.yml +81 -0
  77. data/spec/cassettes/Typetalk_Api_User/_get_profile/should_raise_error_when_access_token_is_wrong.yml +73 -0
  78. data/spec/cassettes/Typetalk_Api_User/_get_profile/should_raise_error_when_scope_is_wrong.yml +73 -0
  79. data/spec/fixtures/attachments/logo_cacoo.jpg +0 -0
  80. data/spec/fixtures/attachments/logo_typetalk.jpg +0 -0
  81. data/spec/spec_helper.rb +36 -0
  82. data/spec/typetalk/api/auth_spec.rb +81 -0
  83. data/spec/typetalk/api/mention_spec.rb +73 -0
  84. data/spec/typetalk/api/message_spec.rb +232 -0
  85. data/spec/typetalk/api/notification_spec.rb +200 -0
  86. data/spec/typetalk/api/topic_spec.rb +150 -0
  87. data/spec/typetalk/api/user_spec.rb +27 -0
  88. data/typetalk.gemspec +33 -0
  89. metadata +326 -0
@@ -0,0 +1,81 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://typetalk.in/oauth2/access_token
6
+ body:
7
+ encoding: UTF-8
8
+ string: client_id=(CLIENT_ID)&client_secret=(CLIENT_SECRET)&grant_type=client_credentials&scope=topic.read%2Ctopic.post%2Cmy
9
+ headers:
10
+ Accept:
11
+ - application/json; charset=utf-8
12
+ User-Agent:
13
+ - Typetalk Rubygem 0.0.1
14
+ Content-Type:
15
+ - application/x-www-form-urlencoded
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Server:
24
+ - nginx
25
+ Date:
26
+ - Thu, 15 May 2014 06:04:18 GMT
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Transfer-Encoding:
30
+ - chunked
31
+ Connection:
32
+ - keep-alive
33
+ Strict-Transport-Security:
34
+ - max-age=2592000
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"access_token":"(ACCESS_TOKEN)","expires_in":3600,"scope":"topic.read,topic.post,my","refresh_token":"(REFRESH_TOKEN)","token_type":"Bearer"}'
38
+ http_version:
39
+ recorded_at: Thu, 15 May 2014 06:04:18 GMT
40
+ - request:
41
+ method: get
42
+ uri: https://typetalk.in/api/v1/profile?access_token=(ACCESS_TOKEN)
43
+ body:
44
+ encoding: US-ASCII
45
+ string: ''
46
+ headers:
47
+ Accept:
48
+ - application/json; charset=utf-8
49
+ User-Agent:
50
+ - Typetalk Rubygem 0.0.1
51
+ Accept-Encoding:
52
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
53
+ response:
54
+ status:
55
+ code: 200
56
+ message: OK
57
+ headers:
58
+ Server:
59
+ - nginx
60
+ Date:
61
+ - Thu, 15 May 2014 06:04:19 GMT
62
+ Content-Type:
63
+ - application/json; charset=utf-8
64
+ Transfer-Encoding:
65
+ - chunked
66
+ Connection:
67
+ - keep-alive
68
+ Cache-Control:
69
+ - no-cache
70
+ Etag:
71
+ - W/"5377f5cd1d69b3d136271ecd317f5353"
72
+ Strict-Transport-Security:
73
+ - max-age=2592000
74
+ body:
75
+ encoding: UTF-8
76
+ string: '{"account":{"name":"typetalk-rubygem-tester","updatedAt":"2014-05-15T00:36:03Z","fullName":"Rubygem
77
+ Tester","id":3865,"createdAt":"2014-05-14T09:09:44Z","suggestion":"Rubygem
78
+ Tester","imageUrl":"https://typetalk.in/accounts/3865/profile_image.png?t=1400114163838"}}'
79
+ http_version:
80
+ recorded_at: Thu, 15 May 2014 06:04:19 GMT
81
+ recorded_with: VCR 2.9.0
@@ -0,0 +1,73 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://typetalk.in/oauth2/access_token
6
+ body:
7
+ encoding: UTF-8
8
+ string: client_id=(CLIENT_ID)&client_secret=(CLIENT_SECRET)&grant_type=client_credentials&scope=topic.read%2Ctopic.post%2Cmy
9
+ headers:
10
+ Accept:
11
+ - application/json; charset=utf-8
12
+ User-Agent:
13
+ - Typetalk Rubygem 0.0.1
14
+ Content-Type:
15
+ - application/x-www-form-urlencoded
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Server:
24
+ - nginx
25
+ Date:
26
+ - Thu, 15 May 2014 07:01:14 GMT
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Transfer-Encoding:
30
+ - chunked
31
+ Connection:
32
+ - keep-alive
33
+ Strict-Transport-Security:
34
+ - max-age=2592000
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"access_token":"(ACCESS_TOKEN)","expires_in":3600,"scope":"topic.read,topic.post,my","refresh_token":"(REFRESH_TOKEN)","token_type":"Bearer"}'
38
+ http_version:
39
+ recorded_at: Thu, 15 May 2014 07:01:14 GMT
40
+ - request:
41
+ method: get
42
+ uri: https://typetalk.in/api/v1/profile?access_token=(WRONG_ACCESS_TOKEN)
43
+ body:
44
+ encoding: US-ASCII
45
+ string: ''
46
+ headers:
47
+ Accept:
48
+ - application/json; charset=utf-8
49
+ User-Agent:
50
+ - Typetalk Rubygem 0.0.1
51
+ Accept-Encoding:
52
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
53
+ response:
54
+ status:
55
+ code: 401
56
+ message: Unauthorized
57
+ headers:
58
+ Server:
59
+ - nginx
60
+ Date:
61
+ - Thu, 15 May 2014 07:01:14 GMT
62
+ Content-Length:
63
+ - '0'
64
+ Connection:
65
+ - keep-alive
66
+ Www-Authenticate:
67
+ - Bearer error="invalid_token", error_description="Invalid access token"
68
+ body:
69
+ encoding: UTF-8
70
+ string: ''
71
+ http_version:
72
+ recorded_at: Thu, 15 May 2014 07:01:14 GMT
73
+ recorded_with: VCR 2.9.0
@@ -0,0 +1,73 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://typetalk.in/oauth2/access_token
6
+ body:
7
+ encoding: UTF-8
8
+ string: client_id=(CLIENT_ID)&client_secret=(CLIENT_SECRET)&grant_type=client_credentials&scope=topic.read%2Ctopic.post
9
+ headers:
10
+ Accept:
11
+ - application/json; charset=utf-8
12
+ User-Agent:
13
+ - Typetalk Rubygem 0.0.1
14
+ Content-Type:
15
+ - application/x-www-form-urlencoded
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Server:
24
+ - nginx
25
+ Date:
26
+ - Thu, 15 May 2014 07:34:58 GMT
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Transfer-Encoding:
30
+ - chunked
31
+ Connection:
32
+ - keep-alive
33
+ Strict-Transport-Security:
34
+ - max-age=2592000
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"access_token":"(ACCESS_TOKEN)","expires_in":3600,"scope":"topic.read,topic.post","refresh_token":"(REFRESH_TOKEN)","token_type":"Bearer"}'
38
+ http_version:
39
+ recorded_at: Thu, 15 May 2014 07:34:58 GMT
40
+ - request:
41
+ method: get
42
+ uri: https://typetalk.in/api/v1/profile?access_token=(ACCESS_TOKEN)
43
+ body:
44
+ encoding: US-ASCII
45
+ string: ''
46
+ headers:
47
+ Accept:
48
+ - application/json; charset=utf-8
49
+ User-Agent:
50
+ - Typetalk Rubygem 0.0.1
51
+ Accept-Encoding:
52
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
53
+ response:
54
+ status:
55
+ code: 401
56
+ message: Unauthorized
57
+ headers:
58
+ Server:
59
+ - nginx
60
+ Date:
61
+ - Thu, 15 May 2014 07:34:58 GMT
62
+ Content-Length:
63
+ - '0'
64
+ Connection:
65
+ - keep-alive
66
+ Www-Authenticate:
67
+ - Bearer error="invalid_scope"
68
+ body:
69
+ encoding: UTF-8
70
+ string: ''
71
+ http_version:
72
+ recorded_at: Thu, 15 May 2014 07:34:59 GMT
73
+ recorded_with: VCR 2.9.0
@@ -0,0 +1,36 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'bundler'
3
+ require 'vcr'
4
+ # require 'webmock/rspec'
5
+
6
+ Bundler.require
7
+
8
+
9
+ VCR.configure do |c|
10
+ c.cassette_library_dir = 'spec/cassettes'
11
+ c.hook_into :webmock
12
+ c.configure_rspec_metadata!
13
+ c.allow_http_connections_when_no_cassette = true
14
+ #c.default_cassette_options = { :record => :none }
15
+
16
+ c.before_record do |i|
17
+ i.request.body.sub!(/(&?client_id=)[^&]+/, '\1(CLIENT_ID)')
18
+ i.request.body.sub!(/(&?client_secret=)[^&]+/, '\1(CLIENT_SECRET)')
19
+ i.request.uri.sub!(/((\?|&)?access_token=)[^&]+/, '\1(ACCESS_TOKEN)')
20
+
21
+ begin
22
+ response = JSON.parse i.response.body
23
+ if response.key?('access_token')
24
+ response['access_token'] = '(ACCESS_TOKEN)'
25
+ response['refresh_token'] = '(REFRESH_TOKEN)'
26
+ i.response.body = response.to_json
27
+ end
28
+ rescue
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ RSpec.configure do |c|
35
+ c.treat_symbols_as_metadata_keys_with_true_values = true
36
+ end
@@ -0,0 +1,81 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Typetalk::Api::Auth do
5
+
6
+ let(:api) { Typetalk::Api.new }
7
+
8
+ describe '#get_access_token', :vcr do
9
+ it 'should get the correct resource by client_credentials' do
10
+ response = api.get_access_token
11
+ expect(response).to be_a(Hashie::Mash)
12
+ expect(response.access_token).to eq('(ACCESS_TOKEN)')
13
+ expect(response.refresh_token).to eq('(REFRESH_TOKEN)')
14
+ expect(response.scope).to eq('topic.read,topic.post,my')
15
+ expect(response.token_type).to eq('Bearer')
16
+ expect(response.expires_in).to eq(3600)
17
+ end
18
+
19
+ it 'should get the correct resource by client_credentials when scope changed' do
20
+ response = api.get_access_token(scope: 'my')
21
+ expect(response).to be_a(Hashie::Mash)
22
+ expect(response.access_token).to eq('(ACCESS_TOKEN)')
23
+ expect(response.refresh_token).to eq('(REFRESH_TOKEN)')
24
+ expect(response.scope).to eq('my')
25
+ end
26
+
27
+ it 'should raise error when client_id is wrong' do
28
+ expect{ api.get_access_token(client_id: 'dummy_id') }.to raise_error(Typetalk::InvalidRequest)
29
+ end
30
+
31
+ it 'should raise error when client_secret is wrong' do
32
+ expect{ api.get_access_token(client_secret: 'dummy_secret') }.to raise_error(Typetalk::InvalidRequest)
33
+ end
34
+
35
+ it 'should get the correct resource by authorization_code' do
36
+ api.authorization_code = '(AUTHORIZATION_CODE)'
37
+ response = api.get_access_token(grant_type: 'authorization_code', redirect_uri: 'http://dummy/')
38
+ expect(response).to be_a(Hashie::Mash)
39
+ expect(response.access_token).to eq('(ACCESS_TOKEN)')
40
+ expect(response.refresh_token).to eq('(REFRESH_TOKEN)')
41
+ expect(response.scope).to eq('topic.read,topic.post,my')
42
+ expect(response.token_type).to eq('Bearer')
43
+ expect(response.expires_in).to eq(3600)
44
+ end
45
+
46
+ it 'should raise error when redirect_uri mismatch' do
47
+ api.authorization_code = '(AUTHORIZATION_CODE)'
48
+ expect{ api.get_access_token(grant_type: 'authorization_code', redirect_uri: 'http://dummy_mismatch/') }.to raise_error(Typetalk::InvalidRequest)
49
+ end
50
+
51
+ it 'should raise error when authorization_code is wrong' do
52
+ expect{ api.get_access_token(grant_type: 'authorization_code', redirect_uri: 'http://dummy/', code:'dummy_code') }.to raise_error(Typetalk::InvalidRequest)
53
+ end
54
+ end
55
+
56
+
57
+
58
+ describe '.authorize_url' do
59
+ before do
60
+ Typetalk.configure do |config|
61
+ config.client_id = 'dummy_id'
62
+ config.redirect_uri = 'http://dummy/'
63
+ config.scope = 'topic.read,topic.write'
64
+ end
65
+ end
66
+ after { Typetalk.reset_config }
67
+
68
+ it 'should get the correct url' do
69
+ url = URI.parse(Typetalk::Api::Auth.authorize_url)
70
+ query = Hash[URI.decode_www_form(url.query)]
71
+ expect(url.scheme).to eq('https')
72
+ expect(url.host).to eq('typetalk.in')
73
+ expect(url.path).to eq('/oauth2/authorize')
74
+ expect(query['client_id']).to eq('dummy_id')
75
+ expect(query['redirect_uri']).to eq('http://dummy/')
76
+ expect(query['response_type']).to eq('code')
77
+ expect(query['scope']).to eq('topic.read,topic.write')
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,73 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Typetalk::Api::Mention do
5
+
6
+ let(:api) { Typetalk::Api.new }
7
+
8
+ describe '#get_mentions', :vcr do
9
+ it 'should get the correct resource' do
10
+ response = api.get_mentions
11
+ expect(response).to be_a(Hashie::Mash)
12
+
13
+ expect(response.mentions.size).to eq(3)
14
+ expect(response.mentions[0].post.message).to eq('Test Message 1 - 4 @typetalk-rubygem-tester ')
15
+ expect(response.mentions[0].post.topic.name).to eq('Test Topic 1')
16
+ expect(response.mentions[0].post.account.name).to eq('umakoz')
17
+ expect(response.mentions[1].post.message).to eq('Test Message 1 - 3 @typetalk-rubygem-tester ')
18
+ expect(response.mentions[1].post.topic.name).to eq('Test Topic 1')
19
+ expect(response.mentions[1].post.account.name).to eq('umakoz')
20
+ expect(response.mentions[2].post.message).to eq('Test Message 3 - 5 @typetalk-rubygem-tester ')
21
+ expect(response.mentions[2].post.topic.name).to eq('Test Topic 3')
22
+ expect(response.mentions[2].post.account.name).to eq('umakoz')
23
+ end
24
+
25
+
26
+ it 'should get the correct resource till mention' do
27
+ response = api.get_mentions(from:36069) # Test Message 1 - 4 @typetalk-rubygem-tester
28
+ expect(response).to be_a(Hashie::Mash)
29
+
30
+ expect(response.mentions.size).to eq(2)
31
+ expect(response.mentions[0].post.message).to eq('Test Message 1 - 3 @typetalk-rubygem-tester ')
32
+ expect(response.mentions[0].post.topic.name).to eq('Test Topic 1')
33
+ expect(response.mentions[0].post.account.name).to eq('umakoz')
34
+ expect(response.mentions[1].post.message).to eq('Test Message 3 - 5 @typetalk-rubygem-tester ')
35
+ expect(response.mentions[1].post.topic.name).to eq('Test Topic 3')
36
+ expect(response.mentions[1].post.account.name).to eq('umakoz')
37
+ end
38
+
39
+
40
+ it 'should get the unread resource' do
41
+ response = api.get_mentions(unread:true)
42
+ expect(response).to be_a(Hashie::Mash)
43
+
44
+ expect(response.mentions.size).to eq(2)
45
+ expect(response.mentions[0].post.message).to eq('Test Message 1 - 4 @typetalk-rubygem-tester ')
46
+ expect(response.mentions[0].post.topic.name).to eq('Test Topic 1')
47
+ expect(response.mentions[0].post.account.name).to eq('umakoz')
48
+ expect(response.mentions[1].post.message).to eq('Test Message 3 - 5 @typetalk-rubygem-tester ')
49
+ expect(response.mentions[1].post.topic.name).to eq('Test Topic 3')
50
+ expect(response.mentions[1].post.account.name).to eq('umakoz')
51
+ end
52
+ end
53
+
54
+
55
+
56
+
57
+
58
+ describe '#read_mention', :vcr do
59
+ it 'should get the correct resource' do
60
+ response = api.read_mention(36069)
61
+ expect(response).to be_a(Hashie::Mash)
62
+
63
+ expect(response.mention.post.message).to eq('Test Message 1 - 4 @typetalk-rubygem-tester ')
64
+ expect(response.mention.post.topic.name).to eq('Test Topic 1')
65
+ expect(response.mention.post.account.name).to eq('umakoz')
66
+ end
67
+
68
+ it 'should raise error when mention_id is wrong' do
69
+ expect{ api.read_mention('dummy') }.to raise_error(Typetalk::InvalidRequest)
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,232 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Typetalk::Api::Message do
5
+
6
+ let(:api) { Typetalk::Api.new }
7
+ let(:topic_id) { 5148 } # Test Topic 2
8
+
9
+ describe '#post_message', :vcr do
10
+ it 'should get the correct resource' do
11
+ response = api.post_message(topic_id, 'Test Message 2 - 1')
12
+ expect(response).to be_a(Hashie::Mash)
13
+ expect(response.topic.name).to eq('Test Topic 2')
14
+ expect(response.post.message).to eq('Test Message 2 - 1')
15
+ expect(response.post.account.name).to eq('typetalk-rubygem-tester')
16
+ expect(response.post.replyTo).to be_nil
17
+ expect(response.post.talks).to be_empty
18
+ expect(response.post.attachments).to be_empty
19
+ end
20
+
21
+
22
+ it 'should get the correct resource for reply' do
23
+ response1 = api.post_message(topic_id, 'Test Message 2 - 2')
24
+ expect(response1).to be_a(Hashie::Mash)
25
+ expect(response1.post.message).to eq('Test Message 2 - 2')
26
+ expect(response1.post.replyTo).to be_nil
27
+
28
+ response2 = api.post_message(topic_id, 'Test Message 2 - 3', reply_to:response1.post.id)
29
+ expect(response2).to be_a(Hashie::Mash)
30
+ expect(response2.post.message).to eq('Test Message 2 - 3')
31
+ expect(response2.post.replyTo).to eq(response1.post.id)
32
+ end
33
+
34
+
35
+ it 'should get the correct resource for attachments' do
36
+ attachment1 = api.upload_attachment(topic_id, File.join(__dir__, '../../fixtures/attachments/logo_typetalk.jpg'))
37
+ attachment2 = api.upload_attachment(topic_id, File.join(__dir__, '../../fixtures/attachments/logo_cacoo.jpg'))
38
+ response = api.post_message(topic_id, 'Test Message 2 - 4', file_keys:[attachment1.fileKey, attachment2.fileKey])
39
+ expect(response).to be_a(Hashie::Mash)
40
+ expect(response.post.message).to eq('Test Message 2 - 4')
41
+
42
+ a1 = response.post.attachments.find {|a| a.fileName == 'logo_typetalk.jpg'}
43
+ expect(a1).not_to be_nil
44
+ a2 = response.post.attachments.find {|a| a.fileName == 'logo_cacoo.jpg'}
45
+ expect(a2).not_to be_nil
46
+ end
47
+
48
+
49
+ it 'should get the correct resource for talks' do
50
+ response = api.post_message(topic_id, 'Test Message 2 - 5', talk_ids:[3993, 3994, 3995]) # 'Test Summary 2 - 1', 'Test Summary 2 - 2', 'Test Summary 2 - 3'
51
+ expect(response).to be_a(Hashie::Mash)
52
+ expect(response.post.message).to eq('Test Message 2 - 5')
53
+
54
+ talk1 = response.post.talks.find {|t| t.name == 'Test Summary 2 - 1'}
55
+ expect(talk1).not_to be_nil
56
+ talk2 = response.post.talks.find {|t| t.name == 'Test Summary 2 - 2'}
57
+ expect(talk2).not_to be_nil
58
+ talk3 = response.post.talks.find {|t| t.name == 'Test Summary 2 - 3'}
59
+ expect(talk3).not_to be_nil
60
+ end
61
+
62
+
63
+ it 'should raise error when topic_id is wrong' do
64
+ expect{ api.post_message('dummy', 'Test Message Dummy') }.to raise_error(Typetalk::InvalidRequest)
65
+ end
66
+ end
67
+
68
+
69
+
70
+
71
+
72
+ describe '#upload_attachment', :vcr do
73
+ it 'should get the correct resource' do
74
+ response = api.upload_attachment(topic_id, File.join(__dir__, '../../fixtures/attachments/logo_typetalk.jpg'))
75
+ expect(response).to be_a(Hashie::Mash)
76
+ expect(response.fileName).to eq('logo_typetalk.jpg')
77
+ expect(response.fileSize).to eq(7622)
78
+ expect(response.fileKey).not_to be_nil
79
+ end
80
+
81
+
82
+ it 'should raise error when topic_id is wrong' do
83
+ expect{ api.upload_attachment('dummy', File.join(__dir__, '../../fixtures/attachments/logo_typetalk.jpg')) }.to raise_error(Typetalk::InvalidRequest)
84
+ end
85
+
86
+
87
+ it 'should raise error when file size 10MB', vcr:false do
88
+ file = File.join(__dir__, '../../fixtures/attachments/10mb.txt')
89
+ File.write(file, 'a' * (10485760 + 1))
90
+ expect{ api.upload_attachment(topic_id, file) }.to raise_error(Typetalk::InvalidFileSize)
91
+ File.unlink(file)
92
+ end
93
+ end
94
+
95
+
96
+
97
+
98
+
99
+ describe '#get_message', :vcr do
100
+ it 'should get the correct resource' do
101
+ message = api.post_message(topic_id, 'Test Message 2 - 6')
102
+ response = api.get_message(topic_id, message.post.id)
103
+ expect(response).to be_a(Hashie::Mash)
104
+ expect(response.topic.name).to eq('Test Topic 2')
105
+ expect(response.post.message).to eq('Test Message 2 - 6')
106
+ expect(response.post.account.name).to eq('typetalk-rubygem-tester')
107
+ expect(response.post.replyTo).to be_nil
108
+ expect(response.post.talks).to be_empty
109
+ expect(response.post.attachments).to be_empty
110
+ end
111
+
112
+
113
+ it 'should raise error when topic_id is wrong' do
114
+ expect{ api.get_message('dummy', 'dummy') }.to raise_error(Typetalk::InvalidRequest)
115
+ end
116
+
117
+
118
+ it 'should raise error when post_id is wrong' do
119
+ expect{ api.get_message(topic_id, 'dummy') }.to raise_error(Typetalk::InvalidRequest)
120
+ end
121
+ end
122
+
123
+
124
+
125
+
126
+
127
+ describe '#remove_message', :vcr do
128
+ it 'should get the correct resource' do
129
+ message = api.post_message(topic_id, 'Test Message 2 - 7')
130
+
131
+ response = api.get_message(topic_id, message.post.id)
132
+ expect(response).to be_a(Hashie::Mash)
133
+ expect(response.post.message).to eq('Test Message 2 - 7')
134
+
135
+ response = api.remove_message(topic_id, message.post.id)
136
+ expect(response).to be true
137
+
138
+ expect{ api.get_message(topic_id, message.post.id) }.to raise_error(Typetalk::NotFound)
139
+ end
140
+
141
+
142
+ it 'should raise error when post_id is wrong' do
143
+ expect{ api.remove_message(topic_id, 'dummy') }.to raise_error(Typetalk::InvalidRequest)
144
+ end
145
+ end
146
+
147
+
148
+
149
+
150
+
151
+ describe '#like_message', :vcr do
152
+ it 'should get the correct resource' do
153
+ message = api.post_message(topic_id, 'Test Message 2 - 8')
154
+
155
+ response = api.like_message(topic_id, message.post.id)
156
+ expect(response).to be_a(Hashie::Mash)
157
+ expect(response.like.topicId).to eq(topic_id)
158
+ expect(response.like.postId).to eq(message.post.id)
159
+ expect(response.like.account.name).to eq('typetalk-rubygem-tester')
160
+
161
+ # when already liked
162
+ expect { api.like_message(topic_id, message.post.id) }.to raise_error(Typetalk::InvalidRequest)
163
+ end
164
+
165
+
166
+ it 'should raise error when post_id is wrong' do
167
+ expect{ api.like_message(topic_id, 'dummy') }.to raise_error(Typetalk::InvalidRequest)
168
+ end
169
+ end
170
+
171
+
172
+
173
+
174
+
175
+ describe '#unlike_message', :vcr do
176
+ it 'should get the correct resource' do
177
+ message = api.post_message(topic_id, 'Test Message 2 - 9')
178
+
179
+ # when unliked
180
+ expect { api.unlike_message(topic_id, message.post.id) }.to raise_error(Typetalk::NotFound)
181
+
182
+ liked = api.like_message(topic_id, message.post.id)
183
+
184
+ response = api.unlike_message(topic_id, message.post.id)
185
+ expect(response).to be_a(Hashie::Mash)
186
+ expect(response.like.topicId).to eq(topic_id)
187
+ expect(response.like.postId).to eq(message.post.id)
188
+ expect(response.like.id).to eq(liked.like.id)
189
+ expect(response.like.account.name).to eq('typetalk-rubygem-tester')
190
+ end
191
+
192
+
193
+ it 'should raise error when post_id is wrong' do
194
+ expect{ api.unlike_message(topic_id, 'dummy') }.to raise_error(Typetalk::InvalidRequest)
195
+ end
196
+ end
197
+
198
+
199
+
200
+
201
+
202
+ describe '#read_message', :vcr do
203
+ it 'should get the correct resource' do
204
+ message1 = api.post_message(topic_id, 'Test Message 2 - 10')
205
+ message2 = api.post_message(topic_id, 'Test Message 2 - 11')
206
+
207
+ response = api.read_message(topic_id)
208
+ expect(response).to be_a(Hashie::Mash)
209
+ expect(response.unread.topicId).to eq(topic_id)
210
+ expect(response.unread.postId).to eq(message2.post.id)
211
+ expect(response.unread["count"]).to eq(0)
212
+ end
213
+
214
+
215
+ it 'should get the correct resource till post' do
216
+ message1 = api.post_message(topic_id, 'Test Message 2 - 12')
217
+ message2 = api.post_message(topic_id, 'Test Message 2 - 13')
218
+
219
+ response = api.read_message(topic_id, message1.post.id)
220
+ expect(response).to be_a(Hashie::Mash)
221
+ expect(response.unread.topicId).to eq(topic_id)
222
+ expect(response.unread.postId).to eq(message1.post.id)
223
+ expect(response.unread["count"]).to eq(1)
224
+ end
225
+
226
+
227
+ it 'should raise error when post_id is wrong' do
228
+ expect{ api.read_message(topic_id, 'dummy') }.to raise_error(Typetalk::InvalidRequest)
229
+ end
230
+ end
231
+
232
+ end