RCloud
融云 Ruby SDK
Installation
Add this line to your application's Gemfile:
gem 'rcloud'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rcloud
使用
基本使用
使用 RCloud::Request 类生成一个请求实例,调用它的方法进行请求。
方法列表见下一个小节。
request = RCloud::Request.new("mgb7ka1nb5dfg", "krO8vJkvpu1yj")
p request.user_get_token("jjjj","","")
如果正常请求,会返回一个Hash对象,如
{"code"=>200, "userId"=>"fkdskf", "token"=>"cSM+0tuXKeQ8x1DHvzI8uj1M2VmLzSGbhpswesn2LhvH6WoIIAXxUBoMXfPT/DVHIq0Sl9S2VhNgUSr2U3LF3g=="}
方法名和 url 映射规则, 以及http参数
http://docs.rongcloud.cn/server.html
所有 Server API 的 url 会被映射为 Ruby 的方法,规则:method_name = url.downcase.gsub(/\//, '_')[1..-1].to_sym
比如: /user/getToken => user_gettoken
而 HTTP 的参数会按照文档次序映射为方法的参数。
如果融云API文档中允许传入多个参数,那么可以传入一个参数的值或者数组。
例外
'/chatroom/create' 和 '/group/sync' 两个 API 有的参数需要不定长度的键值对,所以实现上有一些例外。
/chatroom/create 的参数是一个 Hash, key 是 groupId, value 是 groupName
/group/sync 的第一个参数是 user, 第二个参数是一个 Hash, key 是 groupId, value 是 groupName
它们对应的方法名称也支持自定义规则。
自定义方法名和 url 映射规则
支持自定义url和方法映射规则。Proc接收一个 '/user/getToken' 这样的字符串参数,返回一个方法名字的 symbol对象。
map = Proc.new do |url|
url.downcase.gsub(/\//, '')[0..-1].to_sym
end
request = RCloud::Request.new("mgb7ka1nb5dfg", "krO8vJkvpu1yj", map)
request.usergettoken('fkdskf')
第三方依赖
SDK 使用 rest-client 进行访问,如果认证错误或者其他问题,会触发 RestClient::Exception
关于 rest-client: https://github.com/rest-client/rest-client
使用 https://github.com/seattlerb/minitest 作为测试框架
Contributing
- Fork it ( https://github.com/[my-github-username]/rcloud/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
TODO
- 更好的错误控制和反馈与日志输出
- 产品级别的测试
- 联系发布到官方页面
Author
Lin Xiangyu [email protected]