Baidupush 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Baidupush.gemspec +23 -0
- data/Gemfile +5 -0
- data/LICENSE +0 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +9 -0
- data/lib/Baidupush/api.rb +332 -0
- data/lib/Baidupush/base.rb +103 -0
- data/lib/Baidupush/base_error.rb +75 -0
- data/lib/Baidupush/version.rb +3 -0
- data/lib/Baidupush.rb +5 -0
- data/lib/rails/generators/Baidupush/config/config_generator.rb +16 -0
- data/lib/rails/generators/Baidupush/config/templates/baidu_cloud_push.yml +4 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4eec366eb925647d38fd6128f85812cc9941719e
|
4
|
+
data.tar.gz: 7810c6e59f2a9dc6330e4e3cbb75966f2352f170
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 43fa223edbae80da52f30f656e7685267253950c27db450c123857151c25767079a6d83aefd7982c6ff3f0b9189870d99a7e69a1b4d90e98c95657a3cbdfcfd6
|
7
|
+
data.tar.gz: 4047a487462e08a957c31350f124f713a0daff0750d16b12ff97da3eb8a54fe540d9bcfc347c3a3bc39e84fdf3209cf39a2dd548bd2cc364fd5701a5bde74d47
|
data/.gitignore
ADDED
data/Baidupush.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'Baidupush/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "Baidupush"
|
8
|
+
spec.version = Baidupush::VERSION
|
9
|
+
spec.authors = ["zql"]
|
10
|
+
spec.email = ["hahazhouqunli@gmail.com"]
|
11
|
+
spec.description = %q{百度云推送的SDK}
|
12
|
+
spec.summary = %q{百度云推送}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/Gemfile
ADDED
data/LICENSE
ADDED
File without changes
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 zql
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Baidupush
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'Baidupush'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install Baidupush
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
30
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,332 @@
|
|
1
|
+
#encoding:utf-8
|
2
|
+
require "#{File.dirname(__FILE__)}/base_error"
|
3
|
+
require "#{File.dirname(__FILE__)}/base"
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
module Baidupush
|
7
|
+
class Api
|
8
|
+
# To change this template use File | Settings | File Templates.
|
9
|
+
|
10
|
+
HTTP_METHOD_POST = "POST"
|
11
|
+
HTTP_METHOD_GET = "GET"
|
12
|
+
|
13
|
+
def initialize(options={})
|
14
|
+
options = YAML.load(File.expand_path('../config/baidu_cloud_push.yml', __FILE__)) unless options&&!File.exist?(File.expand_path('../config/baidu_cloud_push.yml', __FILE__))
|
15
|
+
@options = _params_default(options,DEFAULT_API)
|
16
|
+
end
|
17
|
+
|
18
|
+
#获取secretkey
|
19
|
+
def get_secret_key
|
20
|
+
@options[:secret_key]
|
21
|
+
end
|
22
|
+
|
23
|
+
#获取apikey
|
24
|
+
def get_api_key
|
25
|
+
@options[:api_key]
|
26
|
+
end
|
27
|
+
|
28
|
+
#获取配置的应用名
|
29
|
+
def get_push_name
|
30
|
+
@options[:name]
|
31
|
+
end
|
32
|
+
|
33
|
+
#-----------------Channel相关函数---------------------------------------------------------------
|
34
|
+
|
35
|
+
# 查询设备、应用、用户与百度Channel的绑定关系。
|
36
|
+
# Arguments:
|
37
|
+
# method: (String) 必须 方法名,必须存在:query_bindlist。
|
38
|
+
# apikey: (String) 必须 访问令牌,明文AK,可从此值获得App的信息,配合sign中的sk做合法性身份认证
|
39
|
+
# user_id: (String) 不必须 用户标识。不超过256字节。如果存在此字段,则只返回与该用户相关的绑定关系
|
40
|
+
# device_type: (String) 不必须 百度Channel支持多种设备,各种设备的类型编号如下:
|
41
|
+
#1:浏览器设备;
|
42
|
+
#2:PC设备;
|
43
|
+
#3:Andriod设备;
|
44
|
+
#4:iOS设备;
|
45
|
+
#5:Windows Phone设备;
|
46
|
+
#如果存在此字段,则只返回该设备类型的绑定关系。 默认不区分设备类型。
|
47
|
+
#
|
48
|
+
# start: (Integer) 不必须 查询起始页码,默认为0。
|
49
|
+
# limit: (Integer) 不必须 一次查询条数,默认为10。
|
50
|
+
# timestamp: (integer) 必须 用户发起请求时的unix时间戳。本次请求签名的有效时间为该时间戳+10分钟。
|
51
|
+
# sign: (String) 必须 调用参数签名值,与apikey成对出现。
|
52
|
+
# expires: (Integer) 必须 用户指定本次请求签名的失效时间。格式为unix时间戳形式。
|
53
|
+
# v: (Integer) 不必须 API版本号,默认使用最高版本。
|
54
|
+
def query_bindlist(options={})
|
55
|
+
options = _params_default(options,DEFAULT_REQUREST_BAIDU_METHOD)
|
56
|
+
options = _params_default(options,DEFAULT_BINDLIST)
|
57
|
+
request_server(options)
|
58
|
+
end
|
59
|
+
|
60
|
+
#-----------Channel相关函数----End----------------------------------------
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
#---------------标签相关函数----------------------------------------------
|
65
|
+
# 服务器端设置用户标签。当该标签不存在时,服务端将会创建该标签。特别地,当user_id被提交时,服务端将会完成用户和tag的绑定操作
|
66
|
+
# Arguments:
|
67
|
+
# method:(String) 是 方法名,必须存在:set_tag。
|
68
|
+
# apikey: (String) 是 访问令牌,明文AK,可从此值获得App的信息,配合sign中的sk做合法性身份认证。
|
69
|
+
# tag: (String) 是 标签名,最长128字节。
|
70
|
+
# user_id: (String) 否 用户标识,最长256字节。
|
71
|
+
# timestamp: (Integer) 是 用户发起请求时的unix时间戳。本次请求签名的有效时间为该时间戳+10分钟。
|
72
|
+
# sign: (String) 是 调用参数签名值,与apikey成对出现。
|
73
|
+
# 详细用法,请参考:签名计算算法
|
74
|
+
#
|
75
|
+
#expires: (String) 否 用户指定本次请求签名的失效时间。格式为unix时间戳形式。
|
76
|
+
#v: (Integer) 否 API版本号,默认使用最高版本。
|
77
|
+
def tag_set(options={})
|
78
|
+
options = _params_default(options,DEFAULT_REQUREST_BAIDU_METHOD)
|
79
|
+
options = _params_default(options,DEFAULT_SET_TAG)
|
80
|
+
request_server(options)
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
#App Server查询应用标签。
|
85
|
+
#Arguments:
|
86
|
+
# method: (String) 是 方法名,必须存在:fetch_tag。
|
87
|
+
# apikey: (String) 是 访问令牌,明文AK,可从此值获得App的信息,配合sign中的sk做合法性身份认证。
|
88
|
+
# name: (String) 否 标签名称。
|
89
|
+
# start: (Integer) 否 查询页码,默认为0。
|
90
|
+
# limit: (Integer) 否 一次查询条数,默认为10。
|
91
|
+
# timestamp: (Integer) 是 用户发起请求时的unix时间戳。本次请求签名的有效时间为该时间戳+10分钟。
|
92
|
+
# sign: (String) 是 调用参数签名值,与apikey成对出现。详细用法,请参考:签名计算算法
|
93
|
+
#
|
94
|
+
# expires: (Integer) 否 用户指定本次请求签名的失效时间。格式为unix时间戳形式。
|
95
|
+
# v: (Integer) 否 API版本号,默认使用最高版本。
|
96
|
+
def tag_fetch(options={})
|
97
|
+
options = _params_default(options,DEFAULT_REQUREST_BAIDU_METHOD)
|
98
|
+
options = _params_default(options,DEFAULT_FETCH_TAG)
|
99
|
+
request_server(options)
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
#服务端删除用户标签。特别地,当user_id被提交时,服务端将只会完成解除该用户与tag绑定关系的操作。
|
104
|
+
# Arguments:
|
105
|
+
# method: (String) 是 方法名,必须存在:delete_tag。
|
106
|
+
# apikey: (String) 是 访问令牌,明文AK,可从此值获得App的信息,配合sign中的sk做合法性身份认证。
|
107
|
+
# tag: (String) 是 标签名,最长128字节。
|
108
|
+
# user_id: (String) 否 用户标识,最长256字节。
|
109
|
+
# timestamp: (Integer) 是 用户发起请求时的unix时间戳。本次请求签名的有效时间为该时间戳+10分钟。
|
110
|
+
# sign: (String) 是 调用参数签名值,与apikey成对出现。详细用法,请参考:签名计算算法
|
111
|
+
#
|
112
|
+
# expires uint 否 用户指定本次请求签名的失效时间。格式为unix时间戳形式。
|
113
|
+
# v uint 否 API版本号,默认使用最高版本。
|
114
|
+
def tag_delete(options={})
|
115
|
+
options = _params_default(options,DEFAULT_REQUREST_BAIDU_METHOD)
|
116
|
+
options = _params_default(options,DEFAULT_DELETE_TAG)
|
117
|
+
request_server(options)
|
118
|
+
end
|
119
|
+
|
120
|
+
#---------------标签相关函数---END----------------------------------------
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
#---------推送相关函数-------------------------------------------
|
127
|
+
|
128
|
+
|
129
|
+
#设置推送的设置 并返回
|
130
|
+
#Arguments:
|
131
|
+
# method: (String) 是 方法名,必须存在:push_msg。
|
132
|
+
# apikey: (String) 是 访问令牌,明文AK,可从此值获得App的信息,配合sign中的sk做合法性身份认证。
|
133
|
+
# user_id: (String) 否 用户标识,在Android里,channel_id + userid指定某一个特定client。不超过256字节,如果存在此字段,则只推送给此用户。
|
134
|
+
# push_type: (Integer) 是 推送类型,取值范围为:1~3
|
135
|
+
# 1:单个人,必须指定user_id 和 channel_id (指定用户的指定设备)或者user_id(指定用户的所有设备)
|
136
|
+
#
|
137
|
+
# 2:一群人,必须指定 tag
|
138
|
+
#
|
139
|
+
# 3:所有人,无需指定tag、user_id、channel_id
|
140
|
+
#
|
141
|
+
# channel_id: (Integer) 否 通道标识
|
142
|
+
# tag: (String) 否 标签名称,不超过128字节
|
143
|
+
# device_type: (Integer) 否 设备类型,取值范围为:1~5
|
144
|
+
# 云推送支持多种设备,各种设备的类型编号如下:
|
145
|
+
#
|
146
|
+
# 1:浏览器设备;
|
147
|
+
#
|
148
|
+
# 2:PC设备;
|
149
|
+
#
|
150
|
+
# 3:Andriod设备;
|
151
|
+
#
|
152
|
+
# 4:iOS设备;
|
153
|
+
#
|
154
|
+
# 5:Windows Phone设备;
|
155
|
+
#
|
156
|
+
#如果存在此字段,则向指定的设备类型推送消息。 默认为android设备类型。
|
157
|
+
def set_push_config(options={})
|
158
|
+
push_config = _params_default(options,DEFAULT_PUSH_CONFIG)
|
159
|
+
push_config
|
160
|
+
end
|
161
|
+
|
162
|
+
def push_msg_ios(options={})
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
#"title" : "hello" ,
|
168
|
+
# “description: "hello world"
|
169
|
+
#
|
170
|
+
#
|
171
|
+
# //android自定义字段
|
172
|
+
#"custom_content": {
|
173
|
+
# "key1":"value1",
|
174
|
+
# "key2":"value2"
|
175
|
+
#},
|
176
|
+
#生成用于Andriod发送的消息
|
177
|
+
# Augment:
|
178
|
+
def get_android_msg(title,description,custom_content={},options={})
|
179
|
+
options = _params_default(options,DEFAULT_ANDRIOD_FIELD)
|
180
|
+
params = {}
|
181
|
+
params.merge!("title"=>title)
|
182
|
+
params.merge!("description"=>description)
|
183
|
+
options.each do |key,value|
|
184
|
+
params.merge!({key.to_s=>value})
|
185
|
+
end
|
186
|
+
params.merge!("custom_content"=>custom_content)
|
187
|
+
params
|
188
|
+
end
|
189
|
+
|
190
|
+
|
191
|
+
#options:
|
192
|
+
#method string 是 方法名,必须存在:push_msg。
|
193
|
+
#apikey string 是 访问令牌,明文AK,可从此值获得App的信息,配合sign中的sk做合法性身份认证。
|
194
|
+
#user_id string 否 用户标识,在Android里,channel_id + userid指定某一个特定client。不超过256字节,如果存在此字段,则只推送给此用户。
|
195
|
+
#push_type uint 是 推送类型,取值范围为:1~3
|
196
|
+
#1:单个人,必须指定user_id 和 channel_id (指定用户的指定设备)或者user_id(指定用户的所有设备)
|
197
|
+
#
|
198
|
+
#2:一群人,必须指定 tag
|
199
|
+
#
|
200
|
+
#3:所有人,无需指定tag、user_id、channel_id
|
201
|
+
#
|
202
|
+
#channel_id uint 否 通道标识
|
203
|
+
#tag string 否 标签名称,不超过128字节
|
204
|
+
#device_type uint 否 设备类型,取值范围为:1~5
|
205
|
+
#云推送支持多种设备,各种设备的类型编号如下:
|
206
|
+
#
|
207
|
+
#1:浏览器设备;
|
208
|
+
#
|
209
|
+
#2:PC设备;
|
210
|
+
#
|
211
|
+
#3:Andriod设备;
|
212
|
+
#
|
213
|
+
#4:iOS设备;
|
214
|
+
#
|
215
|
+
#5:Windows Phone设备;
|
216
|
+
#
|
217
|
+
#如果存在此字段,则向指定的设备类型推送消息。 默认为android设备类型。
|
218
|
+
def push_msg_andriod(send_andriod_msg,options={})
|
219
|
+
options = _params_default(options,DEFAULT_REQUREST_BAIDU_METHOD)
|
220
|
+
options = _params_default(options,DEFAULT_PUSH_CONFIG)
|
221
|
+
options[:msg_keys] = Time.now.to_i.to_s + ::SecureRandom.uuid.gsub!("-","")
|
222
|
+
options.merge!(:messages => send_andriod_msg)
|
223
|
+
request_server(options)
|
224
|
+
end
|
225
|
+
|
226
|
+
#---------推送相关函数------------END-------------------------------------------
|
227
|
+
|
228
|
+
|
229
|
+
private
|
230
|
+
|
231
|
+
|
232
|
+
#---------------基础函数-----------------------------
|
233
|
+
#获取认证
|
234
|
+
def get_sign(options,url_method)
|
235
|
+
Base.get_sign(options,@options[:secret_key],url_method,@options[:url])
|
236
|
+
end
|
237
|
+
|
238
|
+
#获取请求参数
|
239
|
+
def get_params(options,http_method)
|
240
|
+
options.merge!(:apikey=>@options[:api_key])
|
241
|
+
options.merge!(:sign=>get_sign(options,http_method))
|
242
|
+
params = options
|
243
|
+
params
|
244
|
+
end
|
245
|
+
|
246
|
+
#请求服务器
|
247
|
+
def request_server(options)
|
248
|
+
case options[:url_method]
|
249
|
+
when HTTP_METHOD_POST
|
250
|
+
Base.http_post(@options[:url],get_params(options,HTTP_METHOD_POST))
|
251
|
+
when HTTP_METHOD_GET
|
252
|
+
Base.http_get(@options[:url],get_params(options,HTTP_METHOD_GET))
|
253
|
+
end
|
254
|
+
end
|
255
|
+
#----------------基础函数------------END-----------------------------
|
256
|
+
|
257
|
+
|
258
|
+
#初始化options
|
259
|
+
def _params_default(options,_default)
|
260
|
+
_default.keys.each do |key|
|
261
|
+
options[key] = options[key.to_s]||options[key]
|
262
|
+
options[key] = options[key]||_default[key]
|
263
|
+
end
|
264
|
+
options
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
#类初始值
|
269
|
+
DEFAULT_API = {
|
270
|
+
:name => "测试应用",
|
271
|
+
:secret_key => "7rWFgwXsECVcIzZ2jz5O9Zcy61CCkh6o",
|
272
|
+
:api_key => "e9s6PNT5aTAwysl2D46c8HM4",
|
273
|
+
:url => "http://channel.api.duapp.com/rest/2.0/channel/channel",
|
274
|
+
}
|
275
|
+
|
276
|
+
|
277
|
+
#所有请求百度的函数都需初始化的设置
|
278
|
+
DEFAULT_REQUREST_BAIDU_METHOD = {
|
279
|
+
:timestamp => Time.now.to_i,
|
280
|
+
:expires => Time.now.to_i+60*10
|
281
|
+
}
|
282
|
+
|
283
|
+
#Bindlist查询函数的初始化设置
|
284
|
+
DEFAULT_BINDLIST = {
|
285
|
+
:start => 0,
|
286
|
+
:limit => 20,
|
287
|
+
:url_method => HTTP_METHOD_GET,
|
288
|
+
:method => "query_bindlist",
|
289
|
+
}
|
290
|
+
|
291
|
+
#Andriod的推送的特有消息的初始化设置
|
292
|
+
DEFAULT_ANDRIOD_FIELD = {
|
293
|
+
:notification_builder_id => 0,
|
294
|
+
:notification_basic_style => 7,
|
295
|
+
:open_type => 0,
|
296
|
+
:net_support=>1,
|
297
|
+
:user_confirm=> 0,
|
298
|
+
:url => "http://developer.baidu.com",
|
299
|
+
:pkg_content=>"",
|
300
|
+
:pkg_name=> "com.baidu.bccsclient",
|
301
|
+
:pkg_version=>"0.1",
|
302
|
+
}
|
303
|
+
|
304
|
+
#推送类型函数的初始化设置
|
305
|
+
DEFAULT_PUSH_CONFIG = {
|
306
|
+
:method => "push_msg",
|
307
|
+
:url_method => HTTP_METHOD_POST,
|
308
|
+
:push_type => 3,
|
309
|
+
:msg_keys => (Time.now.to_i.to_s+rand(999).to_s),
|
310
|
+
}
|
311
|
+
|
312
|
+
#设置标签函数的初始化设置
|
313
|
+
DEFAULT_SET_TAG = {
|
314
|
+
:method => "set_tag",
|
315
|
+
:tag => "test",
|
316
|
+
:url_method => HTTP_METHOD_POST,
|
317
|
+
}
|
318
|
+
|
319
|
+
DEFAULT_FETCH_TAG = {
|
320
|
+
:method => "fetch_tag",
|
321
|
+
:url_method => HTTP_METHOD_POST,
|
322
|
+
}
|
323
|
+
|
324
|
+
|
325
|
+
DEFAULT_DELETE_TAG = {
|
326
|
+
:method => "delete_tag",
|
327
|
+
:tag => "test",
|
328
|
+
}
|
329
|
+
|
330
|
+
|
331
|
+
end
|
332
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
#encoding:utf-8
|
2
|
+
require 'yaml'
|
3
|
+
require "#{File.dirname(__FILE__)}/base_error"
|
4
|
+
require 'digest'
|
5
|
+
require 'uri'
|
6
|
+
require "net/http"
|
7
|
+
require "net/https"
|
8
|
+
require 'cgi'
|
9
|
+
|
10
|
+
module Baidupush
|
11
|
+
class Base
|
12
|
+
# To change this template use File | Settings | File Templates.
|
13
|
+
|
14
|
+
# 获取配置信息
|
15
|
+
# Example:
|
16
|
+
# >> Base.load_config
|
17
|
+
# => {"name"=>"测试推送","secret_key"=>"","api_key"=>""}
|
18
|
+
def self.load_config
|
19
|
+
path = Rails.root.join("config",'baidu_cloud_push.yml')
|
20
|
+
config = YAML.load(path)
|
21
|
+
DEFAULT_CONFIG.keys.each do |key|
|
22
|
+
unless config[key.to_s]
|
23
|
+
raise BaseError.new(BaseError.missing_parameters,"缺少参数:#{key.to_s}")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get_sign(params,secertkey,url_method,url)
|
29
|
+
str = "#{url_method}#{url}"
|
30
|
+
_params = {}
|
31
|
+
keys = params.keys
|
32
|
+
keys = keys.sort
|
33
|
+
keys.each do |key|
|
34
|
+
str += "#{key}=#{params[key]}"
|
35
|
+
end
|
36
|
+
str += "#{secertkey}"
|
37
|
+
p "**STR:#{str}"
|
38
|
+
str = CGI::escape(str)
|
39
|
+
p "**Str: #{str}"
|
40
|
+
sign = Digest::MD5.hexdigest(str)
|
41
|
+
sign
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.https_get(url,args)
|
45
|
+
uri = URI.parse(url)
|
46
|
+
uri.query = URI.encode_www_form(args)
|
47
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
48
|
+
http.use_ssl = false
|
49
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
50
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
51
|
+
time_start = Time.now.to_i
|
52
|
+
response = http.request(request)
|
53
|
+
time_end = Time.now.to_i
|
54
|
+
response.body
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.http_post(url,args)
|
58
|
+
uri = URI.parse(url)
|
59
|
+
https=Net::HTTP.new(uri.host,uri.port)
|
60
|
+
req = Net::HTTP::Post.new(uri.path)
|
61
|
+
req.set_form_data(args)
|
62
|
+
time_start = Time.now.to_i
|
63
|
+
p "req : #{req.body}"
|
64
|
+
res = https.request(req)
|
65
|
+
time_end = Time.now.to_i
|
66
|
+
res.body
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
def self.http_get(url,args)
|
71
|
+
uri = URI.parse(url)
|
72
|
+
uri.query = URI.encode_www_form(args)
|
73
|
+
p "URL : #{uri}"
|
74
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
75
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
76
|
+
time_start = Time.now.to_i
|
77
|
+
response = http.request(request)
|
78
|
+
time_end = Time.now.to_i
|
79
|
+
response.body
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.https_post(url,args)
|
83
|
+
uri = URI.parse(url)
|
84
|
+
https=Net::HTTP.new(uri.host,uri.port)
|
85
|
+
https.use_ssl=true
|
86
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
87
|
+
req = Net::HTTP::Post.new(uri.path)
|
88
|
+
req.set_form_data(args)
|
89
|
+
res = https.request(req)
|
90
|
+
res.body
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
#初始化百度推送配置
|
96
|
+
DEFAULT_CONFIG = {
|
97
|
+
:name => "",
|
98
|
+
:secret_key => "",
|
99
|
+
:api_key => "",
|
100
|
+
}
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#encoding:utf-8
|
2
|
+
|
3
|
+
module Baidupush
|
4
|
+
class BaseError < RuntimeError
|
5
|
+
# To change this template use File | Settings | File Templates.
|
6
|
+
#错误的类型
|
7
|
+
attr_reader :type
|
8
|
+
#错误的状态码
|
9
|
+
attr_reader :status
|
10
|
+
#错误的信息
|
11
|
+
attr_reader :msg
|
12
|
+
|
13
|
+
|
14
|
+
#缺少某些参数
|
15
|
+
def self.missing_parameters
|
16
|
+
1
|
17
|
+
end
|
18
|
+
|
19
|
+
#子类应该重写此方法
|
20
|
+
#为每个子类分配不同的错误起始状态值
|
21
|
+
def error_num_start
|
22
|
+
100
|
23
|
+
end
|
24
|
+
|
25
|
+
# 初始化
|
26
|
+
# Arguments:
|
27
|
+
# error_type: (Integer)
|
28
|
+
# error_msg: (String)
|
29
|
+
def initialize(error_type,error_msg="")
|
30
|
+
@type = error_type
|
31
|
+
@msg = error_msg
|
32
|
+
set_status
|
33
|
+
end
|
34
|
+
|
35
|
+
# 获取状态值
|
36
|
+
def status
|
37
|
+
@status
|
38
|
+
end
|
39
|
+
|
40
|
+
def default_error_msg
|
41
|
+
case @type
|
42
|
+
when 0
|
43
|
+
"未知错误"
|
44
|
+
when 1
|
45
|
+
"缺少某些参数"
|
46
|
+
else
|
47
|
+
"未知错误"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
#用于子类中定义独属于子类的错误
|
52
|
+
# Example:
|
53
|
+
# >> BaseError.new.get_error_msg
|
54
|
+
# => {:simale=>"缺少某些参数",:detailed=>"缺少参数name"}
|
55
|
+
def get_error_msg
|
56
|
+
ret = default_error_msg
|
57
|
+
if ret.eql?("未知错误")
|
58
|
+
case @type
|
59
|
+
when 0
|
60
|
+
ret = "未知错误"
|
61
|
+
when 1
|
62
|
+
ret = "缺少某些参数"
|
63
|
+
else
|
64
|
+
ret = "未知错误"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
ret = {:simaple => ret, :detailed => @msg}
|
68
|
+
end
|
69
|
+
|
70
|
+
def set_status
|
71
|
+
@status = error_num_start + @type
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
data/lib/Baidupush.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Baidupush
|
4
|
+
# Your code goes here...
|
5
|
+
module Generators
|
6
|
+
class ConfigGenerator < Rails::Generators::Base
|
7
|
+
desc "Creates a baidu_cloud_push configuration file at config/baidu_push.yml"
|
8
|
+
|
9
|
+
def create_config_file
|
10
|
+
template 'baidu_cloud_push.yml', File.join('config', 'baidu_cloud_push.yml')
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Baidupush
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- zql
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-16 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: 百度云推送的SDK
|
42
|
+
email:
|
43
|
+
- hahazhouqunli@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Baidupush.gemspec
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/Baidupush.rb
|
56
|
+
- lib/Baidupush/api.rb
|
57
|
+
- lib/Baidupush/base.rb
|
58
|
+
- lib/Baidupush/base_error.rb
|
59
|
+
- lib/Baidupush/version.rb
|
60
|
+
- lib/rails/generators/Baidupush/config/config_generator.rb
|
61
|
+
- lib/rails/generators/Baidupush/config/templates/baidu_cloud_push.yml
|
62
|
+
homepage: ''
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.0.3
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: 百度云推送
|
86
|
+
test_files: []
|