baidu-cloud_push 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/baidu/cloud_push.rb +275 -0
- data/lib/baidu/request.rb +85 -0
- data/lib/baidu/response.rb +43 -0
- data/lib/baidu.rb +7 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 85519df09e85c1e26828005e3a55a33e684c52cc
|
4
|
+
data.tar.gz: 31c309c6acb70b4afc0b186e5f8a2c60bdbe30ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d6952cfa53805bfab06f64fffc36fe9de78b40e86b218af114c3ba5507679e23e62f9de70c89a26b3d10756a4111762e97feb24bbbd28af97351e059f544c70
|
7
|
+
data.tar.gz: 7df7280856947b63e3f67ec27523fc452fb9a3af40b363bc3107c637ed25e39b29e4002b61a1d789d728ffe60fd4543221662107d825fba0a183a89f5fecb9b0
|
@@ -0,0 +1,275 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Baidu
|
4
|
+
# 云推送的主类
|
5
|
+
#
|
6
|
+
# @attr_reader request [Baidu::Request] Baidu::Response 实例
|
7
|
+
# @attr_reader apikey [String] 应用的key
|
8
|
+
# @attr_reader resource_name [String] api的资源名称,如push
|
9
|
+
# @attr_reader method_name [String] api的请求方式,如single_device
|
10
|
+
# @attr_reader params [Hash] 请求参数
|
11
|
+
# @attr devise_type [Fixnum] 设备类型,3安卓设备,4iOS设备
|
12
|
+
# @attr expires [Fixnum]
|
13
|
+
# 用户指定本次请求签名的失效时间。格式为unix时间戳形式,
|
14
|
+
# 用于防止 replay 型攻击。为保证防止 replay攻击算法的正确有效,请保证客户端系统时间正确
|
15
|
+
class CloudPush
|
16
|
+
attr_reader :request,:apikey
|
17
|
+
attr_reader :resource_name,:method_name,:params
|
18
|
+
attr_accessor :devise_type,:expires
|
19
|
+
# 构造函数
|
20
|
+
#
|
21
|
+
# @param apikey [String] 应用的key
|
22
|
+
# @param apisecret [String] 应用的secret
|
23
|
+
# @param options [Hash] 自定义参数
|
24
|
+
# @option options [boolean] :use_ssl 默认:false,不使用https请求
|
25
|
+
def initialize(apikey,apisecret,options={})
|
26
|
+
@apikey = apikey
|
27
|
+
@request = Baidu::Request.new(apisecret,options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# 推送消息到单台设备
|
31
|
+
#
|
32
|
+
# @param channel_id [String] 设备唯一的channel_id
|
33
|
+
# @param msg [Hash] 消息内容
|
34
|
+
# @param opt [Hash] 自定义参数
|
35
|
+
# @option opt [Fixnum] :msg_type 消息类型
|
36
|
+
# @option opt [Fixnum] :msg_expires 消息过期时间,unix timestamp
|
37
|
+
# @option opt [Fixnum] :deploy_status iOS应用部署状态
|
38
|
+
# @return [Baidu::Response]
|
39
|
+
def push_single_device(channel_id,msg,opt={})
|
40
|
+
set_resource_and_method(__method__)
|
41
|
+
@params = {channel_id:channel_id,msg:msg.to_json}.merge(opt)
|
42
|
+
send_request
|
43
|
+
end
|
44
|
+
|
45
|
+
# 推送广播消息
|
46
|
+
#
|
47
|
+
# @param msg [Hash] 消息内容
|
48
|
+
# @param opt [Hash] 自定义参数
|
49
|
+
# @option opt [Fixnum] :msg_type 消息类型
|
50
|
+
# @option opt [Fixnum] :msg_expires 消息过期时间,unix timestamp
|
51
|
+
# @option opt [Fixnum] :deploy_status iOS应用部署状态
|
52
|
+
# @option opt [Fixnum] :send_time 指定发送的实际时间
|
53
|
+
# @return [Baidu::Response]
|
54
|
+
def push_all(msg,opt={})
|
55
|
+
set_resource_and_method(__method__)
|
56
|
+
@params = {msg:msg.to_json}.merge(opt)
|
57
|
+
send_request
|
58
|
+
end
|
59
|
+
|
60
|
+
# 推送组播消息
|
61
|
+
#
|
62
|
+
# @param tag [String] 标签类型
|
63
|
+
# @param msg [Hash] 消息内容
|
64
|
+
# @param opt [Hash] 自定义参数
|
65
|
+
# @option opt [Fixnum] :msg_type 消息类型
|
66
|
+
# @option opt [Fixnum] :msg_expires 消息过期时间,unix timestamp
|
67
|
+
# @option opt [Fixnum] :deploy_status iOS应用部署状态
|
68
|
+
# @option opt [Fixnum] :send_time 指定发送的实际时间
|
69
|
+
# @return [Baidu::Response]
|
70
|
+
def push_tags(tag,msg,opt={})
|
71
|
+
set_resource_and_method(__method__)
|
72
|
+
@params = {msg:msg.to_json,tag:tag,type:1}.merge(opt)
|
73
|
+
send_request
|
74
|
+
end
|
75
|
+
|
76
|
+
# 推送到给定的一组设备
|
77
|
+
#
|
78
|
+
# @param channel_ids [Array<String>] 一组channel_ids
|
79
|
+
# @param msg [Hash] 消息内容
|
80
|
+
# @param opt [Hash] 自定义参数
|
81
|
+
# @option opt [Fixnum] :msg_type 消息类型
|
82
|
+
# @option opt [Fixnum] :msg_expires 消息过期时间,unix timestamp
|
83
|
+
# @option opt [String] :topic_id 分类主题名称
|
84
|
+
def push_batch_device(channel_ids,msg,opt={})
|
85
|
+
set_resource_and_method(__method__)
|
86
|
+
@params = {channel_ids: channel_ids.to_json, msg: msg.to_json}.merge(opt)
|
87
|
+
send_request
|
88
|
+
end
|
89
|
+
|
90
|
+
# 查询消息的发送状态
|
91
|
+
#
|
92
|
+
# @param msg_ids [Array<String>] msg_id的数组
|
93
|
+
# @return [Baidu::Response]
|
94
|
+
def report_query_msg_status(msg_ids)
|
95
|
+
set_resource_and_method(__method__)
|
96
|
+
@params = {msg_id: msg_ids.to_json}
|
97
|
+
send_request
|
98
|
+
end
|
99
|
+
|
100
|
+
# 查询定时消息的发送记录
|
101
|
+
#
|
102
|
+
# @param timer_id [String] 推送接口返回的timer_id
|
103
|
+
# @param opt [Hash] 自定义参数
|
104
|
+
# @option opt [Fixnum] :start 返回记录的索引起始位置
|
105
|
+
# @option opt [Fixnum] :limit 返回的记录条数
|
106
|
+
# @option opt [Fixnum] :range_start 查询的起始时间,unix timestamp
|
107
|
+
# @option opt [Fixnum] :range_end 查询的戒指时间,unix timestamp
|
108
|
+
# @return [Baidu::Response]
|
109
|
+
def report_query_timer_records(timer_id,opt={})
|
110
|
+
set_resource_and_method(__method__)
|
111
|
+
@params = {timer_id: timer_id}.merge(opt)
|
112
|
+
send_request
|
113
|
+
end
|
114
|
+
|
115
|
+
# 查询指定分类主题的发送记录
|
116
|
+
#
|
117
|
+
# @param topic_id [String] 分类主题的名称
|
118
|
+
# @param opt [Hash] 自定义参数
|
119
|
+
# @option opt [Fixnum] :start 返回记录的索引起始位置
|
120
|
+
# @option opt [Fixnum] :limit 返回的记录条数
|
121
|
+
# @option opt [Fixnum] :range_start 查询的起始时间,unix timestamp
|
122
|
+
# @option opt [Fixnum] :range_end 查询的戒指时间,unix timestamp
|
123
|
+
# @return [Baidu::Response]
|
124
|
+
def report_query_topic_records(topic_id,opt={})
|
125
|
+
set_resource_and_method(__method__)
|
126
|
+
@params = {topic_id:topic_id}.merge(opt)
|
127
|
+
send_request
|
128
|
+
end
|
129
|
+
|
130
|
+
# 查询标签组的列表
|
131
|
+
#
|
132
|
+
# @param opt [Hash] 自定义参数
|
133
|
+
# @option opt [String] :tag 标签名称
|
134
|
+
# @option opt [Fixnum] :start 返回记录的索引起始位置
|
135
|
+
# @option opt [Fixnum] :limit 返回的记录条数
|
136
|
+
# @return [Baidu::Response]
|
137
|
+
def app_query_tags(opt={})
|
138
|
+
set_resource_and_method(__method__)
|
139
|
+
@params = opt
|
140
|
+
send_request
|
141
|
+
end
|
142
|
+
|
143
|
+
# 创建标签组
|
144
|
+
#
|
145
|
+
# @param tag [String] 标签名称
|
146
|
+
# @return [Baidu::Response]
|
147
|
+
def app_create_tag(tag)
|
148
|
+
set_resource_and_method(__method__)
|
149
|
+
@params = {tag: tag}
|
150
|
+
send_request
|
151
|
+
end
|
152
|
+
|
153
|
+
# 删除标签组
|
154
|
+
#
|
155
|
+
# @param tag [String] 标签名称
|
156
|
+
# @return [Baidu::Response]
|
157
|
+
def app_del_tag(tag)
|
158
|
+
set_resource_and_method(__method__)
|
159
|
+
@params = {tag:tag}
|
160
|
+
send_request
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
# 添加设备到标签组
|
165
|
+
#
|
166
|
+
# @param tag [String] 标签名称
|
167
|
+
# @param channel_ids [Array<String>] 一组channel_id
|
168
|
+
# @return [Baidu::Response]
|
169
|
+
def tag_add_devices(tag,channel_ids)
|
170
|
+
set_resource_and_method(__method__)
|
171
|
+
@params = {tag:tag,channel_ids:channel_ids.to_json}
|
172
|
+
send_request
|
173
|
+
end
|
174
|
+
|
175
|
+
# 将设备从标签组中移除
|
176
|
+
#
|
177
|
+
# @param tag [String] 标签名称
|
178
|
+
# @param channel_ids [Array<String>] 一组channel_id
|
179
|
+
# @return [Baidu::Response]
|
180
|
+
def tag_del_devices(tag,channel_ids)
|
181
|
+
set_resource_and_method(__method__)
|
182
|
+
@params = {tag:tag,channel_ids:channel_ids.to_json}
|
183
|
+
send_request
|
184
|
+
end
|
185
|
+
|
186
|
+
# 查询标签组设备数量
|
187
|
+
#
|
188
|
+
# @param tag [String] 标签名称
|
189
|
+
# @return [Baidu::Response]
|
190
|
+
def tag_device_num(tag)
|
191
|
+
set_resource_and_method(__method__)
|
192
|
+
@params = {tag:tag}
|
193
|
+
send_request
|
194
|
+
end
|
195
|
+
|
196
|
+
# 查询定时任务列表
|
197
|
+
#
|
198
|
+
# @param opt [Hash] 自定义参数
|
199
|
+
# @option opt [String] :timer_id
|
200
|
+
# @option opt [Fixnum] :start 返回记录的索引起始位置
|
201
|
+
# @option opt [Fixnum] :limit 返回的记录条数
|
202
|
+
# @return [Baidu::Response]
|
203
|
+
def timer_query_list(opt={})
|
204
|
+
set_resource_and_method(__method__)
|
205
|
+
@params = opt
|
206
|
+
send_request
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
# 取消定时任务
|
211
|
+
#
|
212
|
+
# @param timer_id [String] 定时任务ID
|
213
|
+
# @return [Baidu::Response]
|
214
|
+
def timer_cancel(timer_id)
|
215
|
+
set_resource_and_method(__method__)
|
216
|
+
@params = {timer_id:timer_id}
|
217
|
+
send_request
|
218
|
+
end
|
219
|
+
|
220
|
+
# 查询分类主题列表
|
221
|
+
#
|
222
|
+
# @param opt [Hash] 自定义参数
|
223
|
+
# @option opt [Fixnum] :start 返回记录的索引起始位置
|
224
|
+
# @option opt [Fixnum] :limit 返回的记录条数
|
225
|
+
# @return [Baidu::Response]
|
226
|
+
def topic_query_list(opt={})
|
227
|
+
set_resource_and_method(__method__)
|
228
|
+
@params = opt
|
229
|
+
send_request
|
230
|
+
end
|
231
|
+
|
232
|
+
# 当前应用的设备统计
|
233
|
+
#
|
234
|
+
# @return [Baidu::Response]
|
235
|
+
def report_statistic_device
|
236
|
+
set_resource_and_method(__method__)
|
237
|
+
@params = {}
|
238
|
+
send_request
|
239
|
+
end
|
240
|
+
|
241
|
+
# 查看分类主题统计信息
|
242
|
+
#
|
243
|
+
# @param topic_id [String] 一个已使用过的分类主题
|
244
|
+
# @return [Baidu::Response]
|
245
|
+
def report_statistic_topic(topic_id)
|
246
|
+
set_resource_and_method(__method__)
|
247
|
+
@params = {topic_id:topic_id}
|
248
|
+
send_request
|
249
|
+
end
|
250
|
+
|
251
|
+
private
|
252
|
+
def merge_params(args={})
|
253
|
+
params = {}
|
254
|
+
params[:expires] = Time.now.to_i + 60*@expires unless @expires.nil?
|
255
|
+
params[:device_type] = @device_type unless @device_type.nil?
|
256
|
+
params[:timestamp] = Time.now.to_i
|
257
|
+
params[:apikey] = @apikey
|
258
|
+
|
259
|
+
params = params.merge(args)
|
260
|
+
new_params = {}
|
261
|
+
params.sort.each{|p| new_params[p[0]] = p[1]}
|
262
|
+
new_params
|
263
|
+
end
|
264
|
+
|
265
|
+
def set_resource_and_method(method_name)
|
266
|
+
splited =method_name.to_s.sub("_"," ").split(" ")
|
267
|
+
@resource_name = splited[0]
|
268
|
+
@method_name = splited[1]
|
269
|
+
end
|
270
|
+
|
271
|
+
def send_request
|
272
|
+
@request.start(@resource_name,@method_name,merge_params(@params))
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'digest'
|
3
|
+
require 'sysinfo'
|
4
|
+
|
5
|
+
module Baidu
|
6
|
+
# 百度云推送API分装类
|
7
|
+
#
|
8
|
+
# @attr_reader apisecret [String] 应用的secret
|
9
|
+
# @attr_reader options [Hash] 配置参数
|
10
|
+
class Request
|
11
|
+
attr_reader :apisecret,:options
|
12
|
+
|
13
|
+
#@private
|
14
|
+
API_URL = '://api.tuisong.baidu.com/rest/3.0'
|
15
|
+
|
16
|
+
#@private
|
17
|
+
REQUEST_GET = {
|
18
|
+
report: ['statistic_device','statistic_topic','query_msg_status','query_timer_records','query_topic_records'],
|
19
|
+
app: ['query_tags'],
|
20
|
+
tag: ['device_num'],
|
21
|
+
timer: ['query_list'],
|
22
|
+
topic: ['query_list']
|
23
|
+
}
|
24
|
+
|
25
|
+
def initialize(apisecret,options = {})
|
26
|
+
@apisecret = apisecret
|
27
|
+
@options = {use_ssl: false}.merge(options)
|
28
|
+
@sysinfo = SysInfo.new
|
29
|
+
end
|
30
|
+
|
31
|
+
def start(resource,method,params={})
|
32
|
+
send_request(resource,method,params)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def send_request(resource,method,params)
|
37
|
+
uri = get_uri(resource,method)
|
38
|
+
type = get_type(resource,method)
|
39
|
+
params = params.merge({sign:gen_sign(@apisecret,type,get_url(resource,method),params)})
|
40
|
+
req = nil
|
41
|
+
case type
|
42
|
+
when 'GET'
|
43
|
+
uri.query = URI.encode_www_form(params)
|
44
|
+
req = Net::HTTP::Get.new(uri)
|
45
|
+
when 'POST'
|
46
|
+
req = Net::HTTP::Post.new(uri)
|
47
|
+
req.set_form_data(params)
|
48
|
+
end
|
49
|
+
req['Content-Type'] = "application/x-www-form-urlencoded;charset=utf-8"
|
50
|
+
req['User-Agent'] = "BCCS_SDK/3.0 (#{@sysinfo.os},#{@sysinfo.arch},#{@sysinfo.impl}) Ruby/#{RUBY_VERSION} (Baidu Push Server SDK V3.0.0)"
|
51
|
+
response = Net::HTTP.start(uri.host,uri.port,use_ssl: @options[:use_ssl]){|http| http.request(req)}
|
52
|
+
http_response_to_baidu_response(response)
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_uri(resource,method)
|
56
|
+
URI(get_url(resource,method))
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_type(resource,method)
|
60
|
+
if REQUEST_GET.has_key?(resource.to_sym) && REQUEST_GET[resource.to_sym].include?(method)
|
61
|
+
'GET'
|
62
|
+
else
|
63
|
+
'POST'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_url(resource,method)
|
68
|
+
http = @options[:use_ssl] ? 'https' : 'http'
|
69
|
+
"#{http+API_URL}/#{resource}/#{method}"
|
70
|
+
end
|
71
|
+
|
72
|
+
def http_response_to_baidu_response(response)
|
73
|
+
Baidu::Response.new(response)
|
74
|
+
end
|
75
|
+
|
76
|
+
def gen_sign(apisecret,requet_method,url,params)
|
77
|
+
parameter = requet_method+url
|
78
|
+
params.sort.each{|p|
|
79
|
+
parameter = parameter + "#{p[0]}=#{p[1]}"
|
80
|
+
}
|
81
|
+
parameter = parameter + apisecret
|
82
|
+
Digest::MD5.hexdigest(URI.encode_www_form_component(parameter))
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Baidu
|
4
|
+
class Response
|
5
|
+
attr_reader :request_id,:response_params,:error_code,:error_msg,:result
|
6
|
+
|
7
|
+
def initialize(http_response)
|
8
|
+
body = JSON.parse(http_response.body)
|
9
|
+
if http_response.code.to_i == 200
|
10
|
+
# success
|
11
|
+
@result = true
|
12
|
+
@request_id = body["request_id"]
|
13
|
+
@response_params = body["response_params"]
|
14
|
+
else
|
15
|
+
# failed
|
16
|
+
@result = false
|
17
|
+
@request_id = body["request_id"]
|
18
|
+
@error_code = body["error_code"]
|
19
|
+
@error_msg = body["error_msg"]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# to_json
|
24
|
+
#
|
25
|
+
# @return [Hash] 返回一个Hash
|
26
|
+
def to_json
|
27
|
+
if @result
|
28
|
+
{
|
29
|
+
result: @result,
|
30
|
+
request_id: @request_id,
|
31
|
+
response_params: @response_params
|
32
|
+
}
|
33
|
+
else
|
34
|
+
{
|
35
|
+
result: @result,
|
36
|
+
request_id: @request_id,
|
37
|
+
error_code: @error_code,
|
38
|
+
error_msg: @error_msg
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/baidu.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baidu-cloud_push
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tesla Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sysinfo
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: This is the Ruby SDK for Baidu Cloud Push RestAPI version 3.0
|
28
|
+
email: leechee89@hotmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/baidu.rb
|
34
|
+
- lib/baidu/cloud_push.rb
|
35
|
+
- lib/baidu/request.rb
|
36
|
+
- lib/baidu/response.rb
|
37
|
+
homepage: https://github.com/liqites/baidu-cloud_push
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 1.9.3
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.0.3
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Baidu Cloud Push Rest SDK 3.0
|
61
|
+
test_files: []
|
62
|
+
has_rdoc:
|