Skip to content

Commit

Permalink
Merge pull request #1 from Martin91/v0.0.2
Browse files Browse the repository at this point in the history
v0.0.2 版本,加入模板消息以及聊天室相关服务接口
  • Loading branch information
Martin91 authored Oct 18, 2016
2 parents 6f0a36a + 4a7e8bf commit 87b8983
Show file tree
Hide file tree
Showing 15 changed files with 372 additions and 32 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
2. **简洁**:不过分封装,仅做必要的请求实现,使用方自行处理响应的 JSON 解析后的 Hash 对象,各字段释义请自行查阅融云文档;
3. **丰富的异常类型**:针对不同的 HTTP 状态码,抛出相对应的异常类型,同时可以通过异常对象的 `business_code` 方法获取错误业务码,可以参考[request test 的代码](https://github.com/Martin91/rong_cloud/blob/master/test/rong_cloud/request_test.rb)

### TODOs
1. 私聊与系统模板消息;
2. 聊天室其他服务以及高级接口实现;
3. 实时消息路由;
4. 消息历史记录;
5. 在线状态订阅。
### TODOs for v0.1.0
1. 实时消息路由;
2. 消息历史记录;
3. 在线状态订阅。

### How to contribute
1. Fork this repo;
Expand Down
8 changes: 8 additions & 0 deletions lib/core_ext/array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Array
# inspired by http://api.rubyonrails.org/classes/Array.html#method-i-extract_options-21
unless instance_methods.include?(:extract_options!)
def extract_options!
last.is_a?(Hash) ? pop : {}
end
end
end
1 change: 1 addition & 0 deletions lib/rong_cloud.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rong_cloud/configuration'
require 'rong_cloud/errors'
require 'rong_cloud/service'
require 'core_ext/array'

# 融云 Ruby SDK 的全局命名空间
module RongCloud
Expand Down
2 changes: 2 additions & 0 deletions lib/rong_cloud/errors.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module RongCloud
# 不支持的消息类型错误
class UnsupportedMessageChannelName < ::StandardError;end
# 缺少必传参数
class MissingOptionError < ::StandardError;end
# 与融云接口请求相关的基本错误类型,其他错误类型继承此类型
class RequestError < ::StandardError
# @!attribute [rw] business_code
Expand Down
46 changes: 28 additions & 18 deletions lib/rong_cloud/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ module RongCloud
module Request
include Signature

# 执行请求
# @param path [String] 请求 API 的相对路径
# @param params [Hash] 请求的参数
# @param content_type [Symbol] 请求数据编码格式,:form_data 或者 :json,默认 :form_data
# @return [Hash] JSON 解析后的响应数据
# @raise [RongCloud::BadRequest] 请求参数有误,缺失或者不正确等,详见官方文档
def request(path, params = nil, content_type = :form_data)
uri = get_uri(path)
req = initialize_request(uri, params, content_type)
use_ssl = uri.scheme == 'https'
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) do |http|
http.request(req)
end

handle_response(res)
end

private
# 拼接请求的接口的路径
#
# @param path [String] 接口的相对路径,e.g. "/user/getToken" 或者 "user/getToken",代码中自动处理开头的斜杠
Expand All @@ -22,13 +40,21 @@ def get_uri(path)
#
# @param uri [URI] 请求路径对象
# @param params [Hash] 请求的参数,所有参数通过 form encoded data 方式发送
# @param content_type [Symbol] 请求数据编码格式,:form_data 或者 :json
# @return [Net::HTTP::Post] 请求的实例
#
def initialize_request(uri, params)
def initialize_request(uri, params, content_type)
req = Net::HTTP::Post.new(uri)
req.set_form_data(params) if params.respond_to?(:map)
signed_headers.each { |header, value| req[header] = value }

case content_type
when :form_data
req.set_form_data(params) if params.respond_to?(:map)
when :json
req.body = params.to_json
req["Content-Type"] = "application/json"
end

req
end

Expand All @@ -49,21 +75,5 @@ def handle_response(res)
raise error
end
end

# 执行请求
# @param path [String] 请求 API 的相对路径
# @param params [Hash] 请求的参数
# @return [Hash] JSON 解析后的响应数据
# @raise [RongCloud::BadRequest] 请求参数有误,缺失或者不正确等,详见官方文档
def request(path, params = nil)
uri = get_uri(path)
req = initialize_request(uri, params)
use_ssl = uri.scheme == 'https'
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) do |http|
http.request(req)
end

handle_response(res)
end
end
end
2 changes: 2 additions & 0 deletions lib/rong_cloud/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'rong_cloud/services/message'
require 'rong_cloud/services/wordfilter'
require 'rong_cloud/services/group'
require 'rong_cloud/services/chatroom'

module RongCloud
# 封装所有接口的 Service 类,所有业务接口通过 Service 的实例完成调用
Expand All @@ -13,5 +14,6 @@ class Service
include RongCloud::Services::Message
include RongCloud::Services::Wordfilter
include RongCloud::Services::Group
include RongCloud::Services::Chatroom
end
end
98 changes: 98 additions & 0 deletions lib/rong_cloud/services/chatroom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module RongCloud
module Services
# 聊天室服务相关接口 http://www.rongcloud.cn/docs/server.html#聊天室服务
module Chatroom
# 创建聊天室,支持同时创建多个聊天室 http://www.rongcloud.cn/docs/server.html#创建聊天室_方法
#
# @param chatrooms [Hash] id 为 key,聊天室名称为 value 的 Hash 实例,多个 key-value 表示多个聊天室
#
def create_chatroom(chatrooms)
params = {}
chatrooms.each { |room_id, room_name| params["chatroom[#{room_id}]"] = room_name }

request("/chatroom/create", params)
end

# 加入聊天室 http://www.rongcloud.cn/docs/server.html#加入聊天室_方法
#
# @param user_id [String, Array] 一个或多个用户 id
# @param chatroom_id [String] 聊天室 id
#
def join_chatroom(user_id, chatroom_id)
request("/chatroom/join", userId: user_id, chatroomId: chatroom_id)
end

# 销毁聊天室 http://www.rongcloud.cn/docs/server.html#销毁聊天室_方法
#
# @param chatroom_ids [String, Array] 一个或多个聊天室 id
#
def destroy_chatroom(chatroom_ids)
request("/chatroom/destroy", chatroomId: chatroom_ids)
end

# 查询聊天室信息 http://www.rongcloud.cn/docs/server.html#查询聊天室信息_方法
#
# @param chatroom_ids [String, Array] 一个或多个聊天室 id
#
def query_chatroom(chatroom_ids)
request("/chatroom/query", chatroomId: chatroom_ids)
end

# 添加禁言聊天室成员 http://www.rongcloud.cn/docs/server.html#添加禁言聊天室成员_方法
#
def block_chatroom_user(chatroom_id, user_id, minute)
request("/chatroom/user/gag/add", chatroomId: chatroom_id, userId: user_id, minute: minute)
end

# 移除封禁聊天室成员 http://www.rongcloud.cn/docs/server.html#移除封禁聊天室成员_方法
#
def unblock_chatroom_user(chatroom_id, user_id)
request("/chatroom/user/block/rollback", chatroomId: chatroom_id, userId: user_id)
end

# 查询被封禁聊天室成员 http://www.rongcloud.cn/docs/server.html#查询被封禁聊天室成员_方法
#
def blocked_chatroom_users(chatroom_id)
request("/chatroom/user/block/list", chatroomId: chatroom_id)
end

# 查询聊天室内用户
#
# @param chatroom_id [String] 聊天室 id
# @param count [String, Fixnum] 要获取的聊天室成员数,上限为 500
# @param order [String] 加入聊天室的先后顺序, 1 为加入时间正序, 2 为加入时间倒序
#
def query_chatroom_users(chatroom_id, count, order = "1")
request("chatroom/user/query", chatroomId: chatroom_id, count: count, order: order)
end

# 聊天室消息停止分发 http://www.rongcloud.cn/docs/server.html#聊天室消息停止分发_方法
#
def stop_chatroom_distribution(chatroom_id)
request("/chatroom/message/stopDistribution", chatroomId: chatroom_id)
end

# 聊天室消息恢复分发 http://www.rongcloud.cn/docs/server.html#聊天室消息恢复分发_方法
#
def resume_chatroom_distribution(chatroom_id)
request("/chatroom/message/resumeDistribution", chatroomId: chatroom_id)
end

# 添加聊天室白名单成员 http://www.rongcloud.cn/docs/server.html#添加聊天室白名单成员_方法
#
def add_chatroom_whitelist(chatroom_id, user_id)
request("/chatroom/user/whitelist/add", chatroomId: chatroom_id, userId: user_id)
end

# 移除聊天室白名单成员 http://www.rongcloud.cn/docs/server.html#移除聊天室白名单成员_方法
def remove_chatroom_whitelist(chatroom_id, user_id)
request("/chatroom/user/whitelist/remove", chatroomId: chatroom_id, userId: user_id)
end

# 查询聊天室白名单成员 http://www.rongcloud.cn/docs/server.html#查询聊天室白名单成员_方法
def whitelisted_chatroom_users(chatroom_id)
request("/chatroom/user/whitelist/query", chatroomId: chatroom_id)
end
end
end
end
26 changes: 23 additions & 3 deletions lib/rong_cloud/services/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@ module Message
# @param [Hash] content 消息体,更多消息请看 http://www.rongcloud.cn/docs/server.html#内置消息类型表
# @option content [Object] :content 消息体中的正文,如果为自定义消息,由消息消费方自行解析
# @option content [Object] :extra 消息体中的附加信息,传递时为字符串,由消息消费方自行解析
#
# @param args [Array] 额外的 1 到 2 个参数,1 个参数时,表示 options 参数;2 个参数时,第一个参数表示模板消息的 values,第二个参数为 options 参数
# @param [Hash] options 额外选项,包含 pushContent 以及 pushData 等配置,所有支持选项根据各消息类型确定
# @option options [String] :pushContent 推送通知显示的 Push 内容
# @option options [Hash] :pushData 推送 payload
#
def send_message(from_user_id, target_id, channel_name, object_name, content, options = {})
def send_message(from_user_id, target_id, channel_name, object_name, content, *args)
options = args.extract_options!
values = args.first
content_type = values.nil? ? :form_data : :json

message_channel = MessageChannel.new(channel_name)
params = { fromUserId: from_user_id, objectName: object_name, content: content.to_json }
if message_channel.target_param_name
params.merge!(message_channel.target_param_name => target_id)
end
if values
if options[:pushContent].nil?
raise RongCloud::MissingOptionError, "pushContent is required for template messages"
end
params.merge!(values: values)
end

params.merge!(options)
request(message_channel.api_path, params)
request(message_channel.api_path, params, content_type)
end

# 发送单聊消息 http://www.rongcloud.cn/docs/server.html#发送单聊消息_方法
Expand All @@ -34,11 +44,21 @@ def send_private_message(from_user_id, to_user_id, object_name, content, options
send_message(from_user_id, to_user_id, :private, object_name, content, options)
end

# 发送单聊模板消息 http://www.rongcloud.cn/docs/server.html#发送单聊模板消息_方法
def send_private_template_message(from_user_id, to_user_id, object_name, content, values, options = {})
send_message(from_user_id, to_user_id, :private_template, object_name, content, values, options)
end

# 发送系统消息 http://www.rongcloud.cn/docs/server.html#发送系统消息_方法
def send_system_message(from_user_id, to_user_id, object_name, content, options = {})
send_message(from_user_id, to_user_id, :system, object_name, content, options)
end

# 发送系统模板消息 http://www.rongcloud.cn/docs/server.html#发送系统模板消息_方法
def send_system_template_message(from_user_id, to_user_id, object_name, content, values, options = {})
send_message(from_user_id, to_user_id, :system_template, object_name, content, values, options)
end

# 发送群组消息 http://www.rongcloud.cn/docs/server.html#发送群组消息_方法
def send_group_message(from_user_id, to_group_id, object_name, content, options = {})
send_message(from_user_id, to_group_id, :group, object_name, content, options)
Expand Down
2 changes: 2 additions & 0 deletions lib/rong_cloud/services/message/message_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class MessageChannel
#
CHANNEL_TO_REQUEST_DETAILS_MAP = {
'private': { target_param_name: "toUserId", api_path: "/message/private/publish" },
private_template: { target_param_name: "toUserId", api_path: "/message/private/publish_template" },
system: { target_param_name: "toUserId", api_path: "/message/system/publish" },
system_template: { target_param_name: "toUserId", api_path: "/message/system/publish_template" },
group: { target_param_name: 'toGroupId', api_path: "/message/group/publish" },
discussion: { target_param_name: "toDiscussionId", api_path: "/message/discussion/publish" },
chatroom: { target_param_name: "toChatroomId", api_path: "/message/chatroom/publish" },
Expand Down
2 changes: 1 addition & 1 deletion rong_cloud.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Gem::Specification.new do |s|
s.name = 'rong_cloud'
s.name = 'rong_cloud_server'
s.platform = Gem::Platform::RUBY
s.require_path = 'lib'
s.summary = '融云 Server API SDK'
Expand Down
2 changes: 2 additions & 0 deletions test/rong_cloud/service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
require 'rong_cloud/services/message_test'
require 'rong_cloud/services/wordfilter_test'
require 'rong_cloud/services/group_test'
require 'rong_cloud/services/chatroom_test'

module RongCloud
class ServiceTest < Minitest::Test
include RongCloud::Services::UserTest
include RongCloud::Services::MessageTest
include RongCloud::Services::WordfilterTest
include RongCloud::Services::GroupTest
include RongCloud::Services::ChatroomTest

def setup
rong_cloud_configure_with_settings
Expand Down
Loading

0 comments on commit 87b8983

Please sign in to comment.