rong_cloud_server 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: c0a792a26e83b1a5420aa834459d44f62be0ec8c
4
- data.tar.gz: 50b3157736cff25fa9937cd99a002d7fa5a62134
3
+ metadata.gz: 6f18dffa1673ff35d013c644ec931c44af2f5931
4
+ data.tar.gz: 7d63b3bee76e8106091e92753f4ad8f2ec48ea05
5
5
  SHA512:
6
- metadata.gz: 9a77797344c9ad396fc0f8b8586d68c061944abac3932cac51445470c2f21d28d4e9544a4ec8524bf21899d2c50a24703b51005ac3b8257b7edf4bf61fdd1ad1
7
- data.tar.gz: 72ae4a709051e62499d409c103feb9c106e6f4d1f49b4cbd30cde0f506c3f8ee4d10c4cde46b9495e9f9fe89386ecd99be40e1372b15d1d29c7c45e678cf88fa
6
+ metadata.gz: 2366fd2761501b3e14a03b5c8e27c0f31c8086a749d5409bae58704f7d2c998578ed1759aed4ce45875e2a266ba3efebaa792a96959153bbb7dbcc440b41a700
7
+ data.tar.gz: d6822867ea92aff168a5641df355deb9bc461d71c8f6af4280c7aacba4761742c837891b910b936774ec6576c0128d1e671dc889c09d0fc50405191f31dc2d6c
data/README.md CHANGED
@@ -28,12 +28,10 @@
28
28
  2. **简洁**:不过分封装,仅做必要的请求实现,使用方自行处理响应的 JSON 解析后的 Hash 对象,各字段释义请自行查阅融云文档;
29
29
  3. **丰富的异常类型**:针对不同的 HTTP 状态码,抛出相对应的异常类型,同时可以通过异常对象的 `business_code` 方法获取错误业务码,可以参考[request test 的代码](https://github.com/Martin91/rong_cloud/blob/master/test/rong_cloud/request_test.rb)。
30
30
 
31
- ### TODOs
32
- 1. 私聊与系统模板消息;
33
- 2. 聊天室其他服务以及高级接口实现;
34
- 3. 实时消息路由;
35
- 4. 消息历史记录;
36
- 5. 在线状态订阅。
31
+ ### TODOs for v0.1.0
32
+ 1. 实时消息路由;
33
+ 2. 消息历史记录;
34
+ 3. 在线状态订阅。
37
35
 
38
36
  ### How to contribute
39
37
  1. Fork this repo;
@@ -0,0 +1,8 @@
1
+ class Array
2
+ # inspired by http://api.rubyonrails.org/classes/Array.html#method-i-extract_options-21
3
+ unless instance_methods.include?(:extract_options!)
4
+ def extract_options!
5
+ last.is_a?(Hash) ? pop : {}
6
+ end
7
+ end
8
+ end
data/lib/rong_cloud.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rong_cloud/configuration'
2
2
  require 'rong_cloud/errors'
3
3
  require 'rong_cloud/service'
4
+ require 'core_ext/array'
4
5
 
5
6
  # 融云 Ruby SDK 的全局命名空间
6
7
  module RongCloud
@@ -1,6 +1,8 @@
1
1
  module RongCloud
2
2
  # 不支持的消息类型错误
3
3
  class UnsupportedMessageChannelName < ::StandardError;end
4
+ # 缺少必传参数
5
+ class MissingOptionError < ::StandardError;end
4
6
  # 与融云接口请求相关的基本错误类型,其他错误类型继承此类型
5
7
  class RequestError < ::StandardError
6
8
  # @!attribute [rw] business_code
@@ -8,6 +8,24 @@ module RongCloud
8
8
  module Request
9
9
  include Signature
10
10
 
11
+ # 执行请求
12
+ # @param path [String] 请求 API 的相对路径
13
+ # @param params [Hash] 请求的参数
14
+ # @param content_type [Symbol] 请求数据编码格式,:form_data 或者 :json,默认 :form_data
15
+ # @return [Hash] JSON 解析后的响应数据
16
+ # @raise [RongCloud::BadRequest] 请求参数有误,缺失或者不正确等,详见官方文档
17
+ def request(path, params = nil, content_type = :form_data)
18
+ uri = get_uri(path)
19
+ req = initialize_request(uri, params, content_type)
20
+ use_ssl = uri.scheme == 'https'
21
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) do |http|
22
+ http.request(req)
23
+ end
24
+
25
+ handle_response(res)
26
+ end
27
+
28
+ private
11
29
  # 拼接请求的接口的路径
12
30
  #
13
31
  # @param path [String] 接口的相对路径,e.g. "/user/getToken" 或者 "user/getToken",代码中自动处理开头的斜杠
@@ -22,13 +40,21 @@ module RongCloud
22
40
  #
23
41
  # @param uri [URI] 请求路径对象
24
42
  # @param params [Hash] 请求的参数,所有参数通过 form encoded data 方式发送
43
+ # @param content_type [Symbol] 请求数据编码格式,:form_data 或者 :json
25
44
  # @return [Net::HTTP::Post] 请求的实例
26
45
  #
27
- def initialize_request(uri, params)
46
+ def initialize_request(uri, params, content_type)
28
47
  req = Net::HTTP::Post.new(uri)
29
- req.set_form_data(params) if params.respond_to?(:map)
30
48
  signed_headers.each { |header, value| req[header] = value }
31
49
 
50
+ case content_type
51
+ when :form_data
52
+ req.set_form_data(params) if params.respond_to?(:map)
53
+ when :json
54
+ req.body = params.to_json
55
+ req["Content-Type"] = "application/json"
56
+ end
57
+
32
58
  req
33
59
  end
34
60
 
@@ -49,21 +75,5 @@ module RongCloud
49
75
  raise error
50
76
  end
51
77
  end
52
-
53
- # 执行请求
54
- # @param path [String] 请求 API 的相对路径
55
- # @param params [Hash] 请求的参数
56
- # @return [Hash] JSON 解析后的响应数据
57
- # @raise [RongCloud::BadRequest] 请求参数有误,缺失或者不正确等,详见官方文档
58
- def request(path, params = nil)
59
- uri = get_uri(path)
60
- req = initialize_request(uri, params)
61
- use_ssl = uri.scheme == 'https'
62
- res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) do |http|
63
- http.request(req)
64
- end
65
-
66
- handle_response(res)
67
- end
68
78
  end
69
79
  end
@@ -3,6 +3,7 @@ require 'rong_cloud/services/user'
3
3
  require 'rong_cloud/services/message'
4
4
  require 'rong_cloud/services/wordfilter'
5
5
  require 'rong_cloud/services/group'
6
+ require 'rong_cloud/services/chatroom'
6
7
 
7
8
  module RongCloud
8
9
  # 封装所有接口的 Service 类,所有业务接口通过 Service 的实例完成调用
@@ -13,5 +14,6 @@ module RongCloud
13
14
  include RongCloud::Services::Message
14
15
  include RongCloud::Services::Wordfilter
15
16
  include RongCloud::Services::Group
17
+ include RongCloud::Services::Chatroom
16
18
  end
17
19
  end
@@ -0,0 +1,98 @@
1
+ module RongCloud
2
+ module Services
3
+ # 聊天室服务相关接口 http://www.rongcloud.cn/docs/server.html#聊天室服务
4
+ module Chatroom
5
+ # 创建聊天室,支持同时创建多个聊天室 http://www.rongcloud.cn/docs/server.html#创建聊天室_方法
6
+ #
7
+ # @param chatrooms [Hash] id 为 key,聊天室名称为 value 的 Hash 实例,多个 key-value 表示多个聊天室
8
+ #
9
+ def create_chatroom(chatrooms)
10
+ params = {}
11
+ chatrooms.each { |room_id, room_name| params["chatroom[#{room_id}]"] = room_name }
12
+
13
+ request("/chatroom/create", params)
14
+ end
15
+
16
+ # 加入聊天室 http://www.rongcloud.cn/docs/server.html#加入聊天室_方法
17
+ #
18
+ # @param user_id [String, Array] 一个或多个用户 id
19
+ # @param chatroom_id [String] 聊天室 id
20
+ #
21
+ def join_chatroom(user_id, chatroom_id)
22
+ request("/chatroom/join", userId: user_id, chatroomId: chatroom_id)
23
+ end
24
+
25
+ # 销毁聊天室 http://www.rongcloud.cn/docs/server.html#销毁聊天室_方法
26
+ #
27
+ # @param chatroom_ids [String, Array] 一个或多个聊天室 id
28
+ #
29
+ def destroy_chatroom(chatroom_ids)
30
+ request("/chatroom/destroy", chatroomId: chatroom_ids)
31
+ end
32
+
33
+ # 查询聊天室信息 http://www.rongcloud.cn/docs/server.html#查询聊天室信息_方法
34
+ #
35
+ # @param chatroom_ids [String, Array] 一个或多个聊天室 id
36
+ #
37
+ def query_chatroom(chatroom_ids)
38
+ request("/chatroom/query", chatroomId: chatroom_ids)
39
+ end
40
+
41
+ # 添加禁言聊天室成员 http://www.rongcloud.cn/docs/server.html#添加禁言聊天室成员_方法
42
+ #
43
+ def block_chatroom_user(chatroom_id, user_id, minute)
44
+ request("/chatroom/user/gag/add", chatroomId: chatroom_id, userId: user_id, minute: minute)
45
+ end
46
+
47
+ # 移除封禁聊天室成员 http://www.rongcloud.cn/docs/server.html#移除封禁聊天室成员_方法
48
+ #
49
+ def unblock_chatroom_user(chatroom_id, user_id)
50
+ request("/chatroom/user/block/rollback", chatroomId: chatroom_id, userId: user_id)
51
+ end
52
+
53
+ # 查询被封禁聊天室成员 http://www.rongcloud.cn/docs/server.html#查询被封禁聊天室成员_方法
54
+ #
55
+ def blocked_chatroom_users(chatroom_id)
56
+ request("/chatroom/user/block/list", chatroomId: chatroom_id)
57
+ end
58
+
59
+ # 查询聊天室内用户
60
+ #
61
+ # @param chatroom_id [String] 聊天室 id
62
+ # @param count [String, Fixnum] 要获取的聊天室成员数,上限为 500
63
+ # @param order [String] 加入聊天室的先后顺序, 1 为加入时间正序, 2 为加入时间倒序
64
+ #
65
+ def query_chatroom_users(chatroom_id, count, order = "1")
66
+ request("chatroom/user/query", chatroomId: chatroom_id, count: count, order: order)
67
+ end
68
+
69
+ # 聊天室消息停止分发 http://www.rongcloud.cn/docs/server.html#聊天室消息停止分发_方法
70
+ #
71
+ def stop_chatroom_distribution(chatroom_id)
72
+ request("/chatroom/message/stopDistribution", chatroomId: chatroom_id)
73
+ end
74
+
75
+ # 聊天室消息恢复分发 http://www.rongcloud.cn/docs/server.html#聊天室消息恢复分发_方法
76
+ #
77
+ def resume_chatroom_distribution(chatroom_id)
78
+ request("/chatroom/message/resumeDistribution", chatroomId: chatroom_id)
79
+ end
80
+
81
+ # 添加聊天室白名单成员 http://www.rongcloud.cn/docs/server.html#添加聊天室白名单成员_方法
82
+ #
83
+ def add_chatroom_whitelist(chatroom_id, user_id)
84
+ request("/chatroom/user/whitelist/add", chatroomId: chatroom_id, userId: user_id)
85
+ end
86
+
87
+ # 移除聊天室白名单成员 http://www.rongcloud.cn/docs/server.html#移除聊天室白名单成员_方法
88
+ def remove_chatroom_whitelist(chatroom_id, user_id)
89
+ request("/chatroom/user/whitelist/remove", chatroomId: chatroom_id, userId: user_id)
90
+ end
91
+
92
+ # 查询聊天室白名单成员 http://www.rongcloud.cn/docs/server.html#查询聊天室白名单成员_方法
93
+ def whitelisted_chatroom_users(chatroom_id)
94
+ request("/chatroom/user/whitelist/query", chatroomId: chatroom_id)
95
+ end
96
+ end
97
+ end
98
+ end
@@ -12,20 +12,30 @@ module RongCloud
12
12
  # @param [Hash] content 消息体,更多消息请看 http://www.rongcloud.cn/docs/server.html#内置消息类型表
13
13
  # @option content [Object] :content 消息体中的正文,如果为自定义消息,由消息消费方自行解析
14
14
  # @option content [Object] :extra 消息体中的附加信息,传递时为字符串,由消息消费方自行解析
15
- #
15
+ # @param args [Array] 额外的 1 到 2 个参数,1 个参数时,表示 options 参数;2 个参数时,第一个参数表示模板消息的 values,第二个参数为 options 参数
16
16
  # @param [Hash] options 额外选项,包含 pushContent 以及 pushData 等配置,所有支持选项根据各消息类型确定
17
17
  # @option options [String] :pushContent 推送通知显示的 Push 内容
18
18
  # @option options [Hash] :pushData 推送 payload
19
19
  #
20
- def send_message(from_user_id, target_id, channel_name, object_name, content, options = {})
20
+ def send_message(from_user_id, target_id, channel_name, object_name, content, *args)
21
+ options = args.extract_options!
22
+ values = args.first
23
+ content_type = values.nil? ? :form_data : :json
24
+
21
25
  message_channel = MessageChannel.new(channel_name)
22
26
  params = { fromUserId: from_user_id, objectName: object_name, content: content.to_json }
23
27
  if message_channel.target_param_name
24
28
  params.merge!(message_channel.target_param_name => target_id)
25
29
  end
30
+ if values
31
+ if options[:pushContent].nil?
32
+ raise RongCloud::MissingOptionError, "pushContent is required for template messages"
33
+ end
34
+ params.merge!(values: values)
35
+ end
26
36
 
27
37
  params.merge!(options)
28
- request(message_channel.api_path, params)
38
+ request(message_channel.api_path, params, content_type)
29
39
  end
30
40
 
31
41
  # 发送单聊消息 http://www.rongcloud.cn/docs/server.html#发送单聊消息_方法
@@ -34,11 +44,21 @@ module RongCloud
34
44
  send_message(from_user_id, to_user_id, :private, object_name, content, options)
35
45
  end
36
46
 
47
+ # 发送单聊模板消息 http://www.rongcloud.cn/docs/server.html#发送单聊模板消息_方法
48
+ def send_private_template_message(from_user_id, to_user_id, object_name, content, values, options = {})
49
+ send_message(from_user_id, to_user_id, :private_template, object_name, content, values, options)
50
+ end
51
+
37
52
  # 发送系统消息 http://www.rongcloud.cn/docs/server.html#发送系统消息_方法
38
53
  def send_system_message(from_user_id, to_user_id, object_name, content, options = {})
39
54
  send_message(from_user_id, to_user_id, :system, object_name, content, options)
40
55
  end
41
56
 
57
+ # 发送系统模板消息 http://www.rongcloud.cn/docs/server.html#发送系统模板消息_方法
58
+ def send_system_template_message(from_user_id, to_user_id, object_name, content, values, options = {})
59
+ send_message(from_user_id, to_user_id, :system_template, object_name, content, values, options)
60
+ end
61
+
42
62
  # 发送群组消息 http://www.rongcloud.cn/docs/server.html#发送群组消息_方法
43
63
  def send_group_message(from_user_id, to_group_id, object_name, content, options = {})
44
64
  send_message(from_user_id, to_group_id, :group, object_name, content, options)
@@ -7,7 +7,9 @@ module RongCloud
7
7
  #
8
8
  CHANNEL_TO_REQUEST_DETAILS_MAP = {
9
9
  'private': { target_param_name: "toUserId", api_path: "/message/private/publish" },
10
+ private_template: { target_param_name: "toUserId", api_path: "/message/private/publish_template" },
10
11
  system: { target_param_name: "toUserId", api_path: "/message/system/publish" },
12
+ system_template: { target_param_name: "toUserId", api_path: "/message/system/publish_template" },
11
13
  group: { target_param_name: 'toGroupId', api_path: "/message/group/publish" },
12
14
  discussion: { target_param_name: "toDiscussionId", api_path: "/message/discussion/publish" },
13
15
  chatroom: { target_param_name: "toChatroomId", api_path: "/message/chatroom/publish" },
data/rong_cloud.gemspec CHANGED
@@ -4,7 +4,7 @@ Gem::Specification.new do |s|
4
4
  s.require_path = 'lib'
5
5
  s.summary = '融云 Server API SDK'
6
6
  s.description = '融云服务器端接口 API,http://www.rongcloud.cn/docs/server.html'
7
- s.version = '0.0.1'
7
+ s.version = '0.0.2'
8
8
  s.files = `git ls-files`.split("\n")
9
9
  s.authors = ['Martin Hong']
10
10
  s.email = 'hongzeqin@gmail.com'
@@ -3,6 +3,7 @@ require 'rong_cloud/services/user_test'
3
3
  require 'rong_cloud/services/message_test'
4
4
  require 'rong_cloud/services/wordfilter_test'
5
5
  require 'rong_cloud/services/group_test'
6
+ require 'rong_cloud/services/chatroom_test'
6
7
 
7
8
  module RongCloud
8
9
  class ServiceTest < Minitest::Test
@@ -10,6 +11,7 @@ module RongCloud
10
11
  include RongCloud::Services::MessageTest
11
12
  include RongCloud::Services::WordfilterTest
12
13
  include RongCloud::Services::GroupTest
14
+ include RongCloud::Services::ChatroomTest
13
15
 
14
16
  def setup
15
17
  rong_cloud_configure_with_settings
@@ -0,0 +1,136 @@
1
+ module RongCloud
2
+ module Services
3
+ module ChatroomTest
4
+ def test_create_chatroom
5
+ chatrooms = { 10001 => "name1", 10002 => "name2" }
6
+ response = @service.create_chatroom(chatrooms)
7
+ assert_equal 200, response["code"]
8
+ end
9
+
10
+ def test_join_chatroom_with_unexisted_chatroom_id
11
+ error = assert_raises RongCloud::BadRequest do
12
+ @service.join_chatroom(["user"], rand.to_s[2..8])
13
+ end
14
+
15
+ assert_equal "chatroomId is not exist.", error.message
16
+ end
17
+
18
+ def test_join_chatroom_with_multiple_users_less_than_50
19
+ create_chatrooms({10003 => "room3"})
20
+ response = @service.join_chatroom(["user1", "user2", "user3"], 10003)
21
+ assert_equal 200, response["code"]
22
+ end
23
+
24
+ def test_join_chatroom_with_multiple_users_greater_than_50
25
+ create_chatrooms({10004 => "room4"})
26
+ users = 1.upto(51).map { |i| "user#{i}" }
27
+ response = @service.join_chatroom(users, 10004)
28
+ assert_equal 200, response["code"]
29
+ end
30
+
31
+ def test_destroy_chatroom_with_multiple_chatroom_ids_existed
32
+ create_chatrooms({10005 => "room5", 10006 => "room6"})
33
+ response = @service.destroy_chatroom([10005, 10006])
34
+ assert_equal 200, response["code"]
35
+ end
36
+
37
+ def test_destroy_chatroom_with_multiple_chatroom_ids_unexisted
38
+ response = @service.destroy_chatroom(%w(unexisted_chat1 unexisted_chat2))
39
+ assert_equal 200, response["code"]
40
+ end
41
+
42
+ def test_query_chatroom_with_existed_chatroom_ids
43
+ create_chatrooms({10007 => "room7", 10008 => "room8"})
44
+ chatrooms = @service.query_chatroom(%w(10007 10008))["chatRooms"]
45
+ chatroom_ids = chatrooms.map{ |chatroom| chatroom["chrmId"] }
46
+ room = chatrooms.detect{ |chatroom| chatroom["chrmId"] == "10007" }
47
+ assert_equal 2, chatrooms.count
48
+ assert_equal "room7", room["name"]
49
+ end
50
+
51
+ def test_query_chatroom_with_unexisted_chatroom_ids
52
+ chatrooms = @service.query_chatroom(%w(unexisted_chat1 unexisted_chat2))["chatRooms"]
53
+ assert_equal [], chatrooms
54
+ end
55
+
56
+ def test_query_chatroom_users_with_count_1_and_order_1
57
+ create_chatrooms({10009 => "room9"})
58
+ @service.join_chatroom("user3", 10009)
59
+ @service.join_chatroom("user4", 10009)
60
+
61
+ users = @service.query_chatroom_users("10009", 1, "1")["users"]
62
+ user = users.first
63
+ assert_equal 1, users.count
64
+ assert_equal "user3", user["id"]
65
+ end
66
+
67
+ def test_query_chatroom_users_with_count_1_and_order_2
68
+ create_chatrooms({10009 => "room9"})
69
+ @service.join_chatroom("user3", 10009)
70
+ @service.join_chatroom("user4", 10009)
71
+
72
+ users = @service.query_chatroom_users("10009", 1, "2")["users"]
73
+ user = users.first
74
+ assert_equal "user4", user["id"]
75
+ end
76
+
77
+ def test_query_chatroom_users_with_count_2
78
+ create_chatrooms({10009 => "room9"})
79
+ @service.join_chatroom("user3", 10009)
80
+ @service.join_chatroom("user4", 10009)
81
+
82
+ users = @service.query_chatroom_users("10009", 2)["users"]
83
+ assert_equal 2, users.count
84
+ end
85
+
86
+ def test_block_chatroom_user_flow
87
+ create_chatrooms({10010 => "room10"})
88
+ @service.join_chatroom("user5", 10010)
89
+
90
+ response = @service.block_chatroom_user(10010, "user5", 60)
91
+ assert_equal 200, response["code"]
92
+
93
+ response = @service.blocked_chatroom_users(10010)
94
+ assert_equal 200, response["code"]
95
+ assert response["users"]
96
+
97
+ response = @service.unblock_chatroom_user(10010, "user5")
98
+ assert_equal 200, response["code"]
99
+ end
100
+
101
+ def test_stop_chatroom_distribution
102
+ response = @service.stop_chatroom_distribution(10011)
103
+ assert_equal 200, response["code"]
104
+ end
105
+
106
+ def test_resume_chatroom_distribution
107
+ response = @service.resume_chatroom_distribution(10011)
108
+ assert_equal 200, response["code"]
109
+ end
110
+
111
+ def test_chatroom_whitelist_flow
112
+ create_chatrooms({10011 => "room11"})
113
+ @service.join_chatroom("user6", 10011)
114
+
115
+ response = @service.add_chatroom_whitelist(10011, "user6")
116
+ assert_equal 200, response["code"]
117
+ response = @service.add_chatroom_whitelist(10011, ["user7", "user8"])
118
+ assert_equal 200, response["code"]
119
+
120
+ response = @service.whitelisted_chatroom_users(10011)
121
+ assert_equal 200, response["code"]
122
+ assert response["users"]
123
+
124
+ response = @service.remove_chatroom_whitelist(10011, "user6")
125
+ assert_equal 200, response["code"]
126
+ response = @service.remove_chatroom_whitelist(10011, ["user7", "user8"])
127
+ assert_equal 200, response["code"]
128
+ end
129
+
130
+ private
131
+ def create_chatrooms(chatrooms = { 10000001 => "super chatroom"})
132
+ @service.create_chatroom(chatrooms)
133
+ end
134
+ end
135
+ end
136
+ end
@@ -25,7 +25,8 @@ module RongCloud
25
25
 
26
26
  def test_create_group_without_group_name
27
27
  response = @service.create_group('user1', '5', nil)
28
- assert_equal 200, response['code']
28
+
29
+ assert_equal 200, response["code"]
29
30
  end
30
31
 
31
32
  def test_join_group_with_single_user_id
@@ -47,7 +48,8 @@ module RongCloud
47
48
 
48
49
  def test_join_group_without_group_name
49
50
  response = @service.join_group('user1', '5', nil)
50
- assert_equal 200, response['code']
51
+
52
+ assert_equal 200, response["code"]
51
53
  end
52
54
 
53
55
  def test_quit_group
@@ -94,7 +96,6 @@ module RongCloud
94
96
  response = @service.blocked_group_members('group1')
95
97
  user_ids = response['users'].map{|user| user['userId']}
96
98
 
97
- assert_equal 1, user_ids.count
98
99
  assert_includes user_ids, "user2"
99
100
  end
100
101
  end
@@ -13,7 +13,7 @@ module RongCloud
13
13
  error = assert_raises RongCloud::UnsupportedMessageChannelName do
14
14
  RongCloud::Services::Message::MessageChannel.new(:nothing)
15
15
  end
16
- expected_error = "support only channels: [\"private\", \"system\", \"group\", \"discussion\", \"chatroom\", \"broadcast\"]"
16
+ expected_error = "support only channels: [\"private\", \"private_template\", \"system\", \"system_template\", \"group\", \"discussion\", \"chatroom\", \"broadcast\"]"
17
17
  assert_equal expected_error, error.message
18
18
  end
19
19
 
@@ -22,11 +22,21 @@ module RongCloud
22
22
  assert_equal "toUserId", channel.target_param_name
23
23
  end
24
24
 
25
+ def test_target_param_name_for_private_template
26
+ channel = RongCloud::Services::Message::MessageChannel.new(:private_template)
27
+ assert_equal "toUserId", channel.target_param_name
28
+ end
29
+
25
30
  def test_target_param_name_for_system
26
31
  channel = RongCloud::Services::Message::MessageChannel.new(:system)
27
32
  assert_equal "toUserId", channel.target_param_name
28
33
  end
29
34
 
35
+ def test_target_param_name_for_system_template
36
+ channel = RongCloud::Services::Message::MessageChannel.new(:system_template)
37
+ assert_equal "toUserId", channel.target_param_name
38
+ end
39
+
30
40
  def test_target_param_name_for_group
31
41
  channel = RongCloud::Services::Message::MessageChannel.new(:group)
32
42
  assert_equal "toGroupId", channel.target_param_name
@@ -52,11 +62,21 @@ module RongCloud
52
62
  assert_equal "/message/private/publish", channel.api_path
53
63
  end
54
64
 
65
+ def test_api_path_for_private_template
66
+ channel = RongCloud::Services::Message::MessageChannel.new(:private_template)
67
+ assert_equal "/message/private/publish_template", channel.api_path
68
+ end
69
+
55
70
  def test_api_path_for_system
56
71
  channel = RongCloud::Services::Message::MessageChannel.new(:system)
57
72
  assert_equal "/message/system/publish", channel.api_path
58
73
  end
59
74
 
75
+ def test_api_path_for_system_template
76
+ channel = RongCloud::Services::Message::MessageChannel.new(:system_template)
77
+ assert_equal "/message/system/publish_template", channel.api_path
78
+ end
79
+
60
80
  def test_api_path_for_group
61
81
  channel = RongCloud::Services::Message::MessageChannel.new(:group)
62
82
  assert_equal "/message/group/publish", channel.api_path
@@ -11,6 +11,33 @@ module RongCloud
11
11
  assert_equal 200, response["code"]
12
12
  end
13
13
 
14
+ def test_send_private_template_message_with_multiple_to_user_ids
15
+ response = @service.send_private_template_message(1, [2, 3, 4], "RC:TxtMsg",
16
+ { content: "hello {name}", extra: "nothing" },
17
+ [
18
+ {"{name}" => "user2"},
19
+ {"{name}" => "user3"},
20
+ {"{name}" => "user4"},
21
+ ],
22
+ pushContent: Array.new(3, "欢迎 {name}")
23
+ )
24
+ assert_equal 200, response["code"]
25
+ end
26
+
27
+ def test_send_private_template_message_with_multiple_to_user_ids_missing_push_content
28
+ error = assert_raises RongCloud::MissingOptionError do
29
+ @service.send_private_template_message(1, [2, 3], "RC:TxtMsg",
30
+ { content: "hello {name}", extra: "nothing" },
31
+ [
32
+ {"{name}" => "user2"},
33
+ {"{name}" => "user3"}
34
+ ],
35
+ )
36
+ end
37
+
38
+ assert_equal "pushContent is required for template messages", error.message
39
+ end
40
+
14
41
  def test_send_private_message_with_options
15
42
  options = { pushContent: "hello", pushData: { shouldBeTrue: "true" } }
16
43
  response = @service.send_private_message(1, [2, 3, 4], "RC:TxtMsg", { content: "hello world", extra: "nothing" }, options)
@@ -27,6 +54,19 @@ module RongCloud
27
54
  assert_equal 200, response["code"]
28
55
  end
29
56
 
57
+ def test_send_system_template_message_with_multiple_to_user_ids
58
+ response = @service.send_system_template_message(1, [2, 3, 4], "RC:TxtMsg",
59
+ { content: "hello {name}", extra: "nothing" },
60
+ [
61
+ {"{name}" => "user5"},
62
+ {"{name}" => "user6"},
63
+ {"{name}" => "user7"},
64
+ ],
65
+ pushContent: Array.new(3, "欢迎 {name}")
66
+ )
67
+ assert_equal 200, response["code"]
68
+ end
69
+
30
70
  def test_send_system_message_with_options
31
71
  options = { pushContent: "hello", pushData: { shouldBeTrue: "true" } }
32
72
  response = @service.send_system_message(1, [2, 3, 4], "RC:TxtMsg", { content: "hello world", extra: "nothing" }, options)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rong_cloud_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Hong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-09 00:00:00.000000000 Z
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 融云服务器端接口 API,http://www.rongcloud.cn/docs/server.html
14
14
  email: hongzeqin@gmail.com
@@ -19,11 +19,13 @@ files:
19
19
  - ".gitignore"
20
20
  - README.md
21
21
  - config.example.yml
22
+ - lib/core_ext/array.rb
22
23
  - lib/rong_cloud.rb
23
24
  - lib/rong_cloud/configuration.rb
24
25
  - lib/rong_cloud/errors.rb
25
26
  - lib/rong_cloud/request.rb
26
27
  - lib/rong_cloud/service.rb
28
+ - lib/rong_cloud/services/chatroom.rb
27
29
  - lib/rong_cloud/services/group.rb
28
30
  - lib/rong_cloud/services/message.rb
29
31
  - lib/rong_cloud/services/message/message_channel.rb
@@ -35,6 +37,7 @@ files:
35
37
  - test/rong_cloud/configuration_test.rb
36
38
  - test/rong_cloud/request_test.rb
37
39
  - test/rong_cloud/service_test.rb
40
+ - test/rong_cloud/services/chatroom_test.rb
38
41
  - test/rong_cloud/services/group_test.rb
39
42
  - test/rong_cloud/services/message/message_channel_test.rb
40
43
  - test/rong_cloud/services/message_test.rb