ezaliyun-ams 0.1.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ezaliyun-ams.rb +361 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fad0fee2040ac1144b68bf34f18b9e93e89c151d
4
+ data.tar.gz: fde1c11c50aedc2ee69389e4a8304db164c6a117
5
+ SHA512:
6
+ metadata.gz: 3b1d3bdb08fa2a83be028ef82a259653daf02ff3536454ea9e0188363cd7f3c9416cbe612d00ad468acfd25bc5352555e198ba9e3d14be9582d1a2119174fe66
7
+ data.tar.gz: 58d112c52dd82b264c8e9ca5344ec6b29ab8be62616795713d1c4d95775590423f42104bcbf9d251e4243a7fd5b1c79032790416b7c1a7bbd41b735107bbc66d
@@ -0,0 +1,361 @@
1
+ # require 'ezaliyun_ams/version'
2
+ require 'openssl'
3
+ require 'base64'
4
+ require 'erb'
5
+ require 'net/https'
6
+ require 'json'
7
+ include ERB::Util
8
+
9
+ module EzaliyunAms
10
+ class Configuration
11
+ attr_accessor :access_key_secret, :access_key_id, :format, :region_id,
12
+ :version, :signature_method, :signature_version, :domain
13
+ def initialize
14
+ @access_key_secret ||= ''
15
+ @access_key_id ||= ''
16
+ @format ||= 'JSON'
17
+ @region_id ||= 'cn-hangzhou'
18
+ @version ||= '2016-08-01'
19
+ @signature_method ||= 'HMAC-SHA1'
20
+ @signature_version ||= '1.0'
21
+ @domain ||= 'cloudpush.aliyuncs.com'
22
+ end
23
+ end
24
+
25
+ class << self
26
+ attr_writer :configuration
27
+
28
+ def configuration
29
+ @configuration ||= Configuration.new
30
+ end
31
+
32
+ def configure
33
+ yield(configuration)
34
+ end
35
+
36
+ def get_rs api_params
37
+ public_params ={
38
+ 'Format' => configuration.format,
39
+ 'RegionId' => configuration.region_id,
40
+ 'Version' => configuration.version,
41
+ 'AccessKeyId' => configuration.access_key_id,
42
+ 'SignatureMethod' => configuration.signature_method,
43
+ 'Timestamp' => seed_timestamp,
44
+ 'SignatureVersion' => configuration.signature_version,
45
+ 'SignatureNonce' => seed_signature_nonce
46
+ }
47
+ public_params = public_params.merge api_params
48
+ public_params['Signature'] = sign configuration.access_key_secret, public_params
49
+ post_param(public_params).body
50
+ end
51
+
52
+ # 原生参数经过2次编码拼接成标准字符串
53
+ def canonicalized_query_string(params)
54
+ cqstring = ''
55
+
56
+ # Canonicalized Query String/使用请求参数构造规范化的请求字符串
57
+ # 按照参数名称的字典顺序对请求中所有的请求参数进行排序
58
+ params = params.sort.to_h
59
+
60
+ params.each do |key, value|
61
+ if cqstring.empty?
62
+ cqstring += url_encode"#{key}=#{url_encode(value)}"
63
+ else
64
+ cqstring += url_encode"&#{key}=#{url_encode(value)}"
65
+ end
66
+ end
67
+ return cqstring
68
+ end
69
+
70
+ # 生成数字签名
71
+ def sign(key_secret, params)
72
+ key = key_secret + '&'
73
+ signature = 'POST' + '&' + url_encode('/') + '&' + canonicalized_query_string(params)
74
+ digest = OpenSSL::Digest.new('sha1')
75
+ sign = Base64.encode64(OpenSSL::HMAC.digest(digest, key, signature))
76
+ # url_encode(sign.chomp) # 通过chomp去掉最后的换行符 LF
77
+ sign.chomp # 通过chomp去掉最后的换行符 LF
78
+ end
79
+
80
+ # 生成短信时间戳
81
+ def seed_timestamp
82
+ Time.now.utc.strftime("%FT%TZ")
83
+ end
84
+
85
+ # 生成短信唯一标识码,采用到微秒的时间戳
86
+ def seed_signature_nonce
87
+ Time.now.utc.strftime("%Y%m%d%H%M%S%L")
88
+ end
89
+
90
+ def post_param params
91
+ uri = URI('https://' + configuration.domain)
92
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
93
+ request = Net::HTTP::Post.new uri
94
+ request.set_form_data params
95
+ # request.body = params.to_json
96
+ http.request request # Net::HTTPResponse object
97
+ end
98
+ end
99
+
100
+
101
+ # ------各种接口从这开始------
102
+ # APP概览列表
103
+ def ListSummaryApps
104
+ api_params = {
105
+ 'Action' => __method__.to_s
106
+ }
107
+ get_rs api_params
108
+ end
109
+
110
+ # ------推送相关接口
111
+ # 推消息给Android设备
112
+ def PushMessageToAndroid app_key, target, target_value, title, body
113
+ api_params = {
114
+ 'Action' => __method__.to_s,
115
+ 'AppKey' => app_key,
116
+ 'Target' => target,
117
+ 'TargetValue' => target_value,
118
+ 'Title' => title,
119
+ 'Body' => body
120
+ }
121
+ get_rs api_params
122
+ end
123
+
124
+ # 推消息给IOS设备
125
+ def PushMessageToiOS app_key, target, target_value, title, body
126
+ api_params = {
127
+ 'Action' => __method__.to_s,
128
+ 'AppKey' => app_key,
129
+ 'Target' => target,
130
+ 'TargetValue' => target_value,
131
+ 'Title' => title,
132
+ 'Body' => body
133
+ }
134
+ get_rs api_params
135
+ end
136
+
137
+ # 推通知给Android设备
138
+ def PushNoticeToAndroid app_key, target, target_value, title, body, ext_parameters = nil
139
+ api_params = {
140
+ 'Action' => __method__.to_s,
141
+ 'AppKey' => app_key,
142
+ 'Target' => target,
143
+ 'TargetValue' => target_value,
144
+ 'Title' => title,
145
+ 'Body' => body,
146
+ 'ExtParameters' => ext_parameters
147
+ }
148
+ if !ext_parameters.nil? then api_params['ExtParameters'] = ext_parameters end
149
+ get_rs api_params
150
+ end
151
+
152
+ # 推通知给IOS设备
153
+ def PushNoticeToiOS app_key, target, target_value, apns_env, title, body, ext_parameters = nil
154
+ api_params = {
155
+ 'Action' => __method__.to_s,
156
+ 'AppKey' => app_key,
157
+ 'Target' => target,
158
+ 'TargetValue' => target_value,
159
+ 'ApnsEnv' => apns_env,
160
+ 'Title' => title,
161
+ 'Body' => body
162
+ }
163
+ if !ext_parameters.nil? then api_params['ExtParameters'] = ext_parameters end
164
+ get_rs api_params
165
+ end
166
+
167
+ # 推送高级接口 未完成
168
+ def Push app_key, other_hash
169
+ api_params = {
170
+ 'Action' => __method__.to_s,
171
+ 'AppKey' => app_key
172
+ }
173
+ api_params.merge! other_hash
174
+ get_rs api_params
175
+ end
176
+
177
+ # 取消定时推送任务
178
+ def CancelPush app_key, message_id
179
+ api_params = {
180
+ 'Action' => __method__.to_s,
181
+ 'AppKey' => app_key,
182
+ 'MessageId' => message_id
183
+ }
184
+ get_rs api_params
185
+ end
186
+
187
+ # ------查询相关接口
188
+ # 查询推送列表
189
+ def ListPushRecords app_key, start_time, end_time, push_type=nil, page=nil, page_size=nil
190
+ api_params = {
191
+ 'Action' => __method__.to_s,
192
+ 'AppKey' => app_key,
193
+ 'StartTime' => start_time,
194
+ 'EndTime' => end_time,
195
+ 'PushType' => push_type,
196
+ 'Page' => page,
197
+ 'PageSize' => page_size
198
+ }
199
+ if !push_type.nil? then api_params['PushType'] = push_type end
200
+ if !page.nil? then api_params['Page'] = page end
201
+ if !page_size.nil? then api_params['PageSize'] = page_size end
202
+ get_rs api_params
203
+ end
204
+
205
+ # APP维度推送统计
206
+ def QueryPushStatByApp app_key, start_time, end_time, granularity
207
+ api_params = {
208
+ 'Action' => __method__.to_s,
209
+ 'AppKey' => app_key,
210
+ 'StartTime' => start_time,
211
+ 'EndTime' => end_time,
212
+ 'Granularity' => granularity
213
+ }
214
+ get_rs api_params
215
+ end
216
+
217
+ # 任务维度推送统计
218
+ def QueryPushStatByMsg app_key, message_id
219
+ api_params = {
220
+ 'Action' => __method__.to_s,
221
+ 'AppKey' => app_key,
222
+ 'MessageId' => message_id
223
+ }
224
+ get_rs api_params
225
+ end
226
+
227
+ # 设备新增与留存
228
+ def QueryDeviceStat app_key, start_time, end_time, device_type, query_type
229
+ api_params = {
230
+ 'Action' => __method__.to_s,
231
+ 'AppKey' => app_key,
232
+ 'StartTime' => start_time,
233
+ 'EndTime' => end_time,
234
+ 'DeviceType' => device_type,
235
+ 'QueryType' => query_type
236
+ }
237
+ get_rs api_params
238
+ end
239
+
240
+ # 去重设备统计
241
+ def QueryUniqueDeviceStat app_key, start_time, end_time
242
+ api_params = {
243
+ 'Action' => __method__.to_s,
244
+ 'AppKey' => app_key,
245
+ 'StartTime' => start_time,
246
+ 'EndTime' => end_time
247
+ }
248
+ get_rs api_params
249
+ end
250
+
251
+ # 查询设备详情
252
+ def QueryDeviceInfo app_key, device_id
253
+ api_params = {
254
+ 'Action' => __method__.to_s,
255
+ 'AppKey' => app_key,
256
+ 'DeviceId' => device_id
257
+ }
258
+ get_rs api_params
259
+ end
260
+
261
+ # 批量检查设备有效性
262
+ def CheckDevices app_key, device_ids
263
+ api_params = {
264
+ 'Action' => __method__.to_s,
265
+ 'AppKey' => app_key,
266
+ 'DeviceIds' => device_ids
267
+ }
268
+ get_rs api_params
269
+ end
270
+
271
+ # ------TAG相关接口
272
+ # 绑定TAG
273
+ def BindTag app_key, client_key, key_type, tag_name
274
+ api_params = {
275
+ 'Action' => __method__.to_s,
276
+ 'AppKey' => app_key,
277
+ 'ClientKey' => client_key,
278
+ 'KeyType' => key_type,
279
+ 'TagName' => tag_name
280
+ }
281
+ get_rs api_params
282
+ end
283
+
284
+ # 查询TAG
285
+ def QueryTags app_key, client_key, key_type
286
+ api_params = {
287
+ 'Action' => __method__.to_s,
288
+ 'AppKey' => app_key,
289
+ 'ClientKey' => client_key,
290
+ 'KeyType' => key_type
291
+ }
292
+ get_rs api_params
293
+ end
294
+
295
+ # 解绑TAG
296
+ def UnbindTag app_key, client_key, key_type, tag_name
297
+ api_params = {
298
+ 'Action' => __method__.to_s,
299
+ 'AppKey' => app_key,
300
+ 'ClientKey' => client_key,
301
+ 'KeyType' => key_type,
302
+ 'TagName' => tag_name
303
+ }
304
+ get_rs api_params
305
+ end
306
+
307
+ # TAG列表
308
+ def ListTags app_key, client_key, key_type, tag_name
309
+ api_params = {
310
+ 'Action' => __method__.to_s,
311
+ 'AppKey' => app_key
312
+ }
313
+ get_rs api_params
314
+ end
315
+
316
+ # 删除TAG
317
+ def RemoveTag app_key, tag_name
318
+ api_params = {
319
+ 'Action' => __method__.to_s,
320
+ 'AppKey' => app_key,
321
+ 'TagName' => tag_name
322
+ }
323
+ get_rs api_params
324
+ end
325
+
326
+ # ------别名相关接口
327
+ # 绑定别名
328
+ def BindAlias app_key, device_id, alias_name
329
+ api_params = {
330
+ 'Action' => __method__.to_s,
331
+ 'AppKey' => app_key,
332
+ 'DeviceId' => device_id,
333
+ 'AliasName' => alias_name
334
+ }
335
+ get_rs api_params
336
+ end
337
+
338
+ # 查询别名
339
+ def QueryAliases app_key, device_id
340
+ api_params = {
341
+ 'Action' => __method__.to_s,
342
+ 'AppKey' => app_key,
343
+ 'DeviceId' => device_id
344
+ }
345
+ get_rs api_params
346
+ end
347
+
348
+ # 解绑别名
349
+ def UnbindAlias app_key, device_id, unbind_all = nil, alias_name = nil
350
+ api_params = {
351
+ 'Action' => __method__.to_s,
352
+ 'AppKey' => app_key,
353
+ 'DeviceId' => device_id
354
+ }
355
+ if !unbind_all.nil? then api_params['UnbindAll'] = unbind_all end
356
+ if !alias_name.nil? then api_params['AliasName'] = alias_name end
357
+ get_rs api_params
358
+ end
359
+
360
+ end
361
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ezaliyun-ams
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "李祎"
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: "阿里云移动推送,基本实现。"
42
+ email:
43
+ - vlyonline@163.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/ezaliyun-ams.rb
49
+ homepage: https://github.com/vlyonline/ezaliyun-ams
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.4.8
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: "阿里云移动推送"
73
+ test_files: []