wei-backend 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +75 -14
- data/{Rakefile → examples/waiting_bus/Rakefile} +0 -0
- data/lib/wei-backend/base.rb +13 -1
- data/lib/wei-backend/version.rb +1 -1
- data/spec/spec_helper.rb +19 -0
- data/spec/wei_backend_spec.rb +15 -5
- data/wei-backend.gemspec +15 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3f0021088d9e698689cd29d21e80aaff92c7471
|
4
|
+
data.tar.gz: 93e01ff4fde62445cef476919c502f9759374d03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b2177cd5fa8259c5c344fcdae4e94c343d6cd3253e1ddcbe64b83f04d515be5e43cef3fcb64d90dec24acc0a6b708b374c3392b93d874d1e4bfbcdea32988a4
|
7
|
+
data.tar.gz: 20dbec32ec7eea7d8b0d45d3d7e6ce8a09f4f52d3aa398fd5c56e4cd85550e1fa715ff7204eaa144b1812ec11e619909e836b0c91555591395c2cae2406bf0fd
|
data/README.md
CHANGED
@@ -1,23 +1,84 @@
|
|
1
1
|
微信公众平台后台框架
|
2
2
|
========
|
3
|
-
##
|
4
|
-
|
3
|
+
## 如何安装:
|
4
|
+
* ### 使用bundler
|
5
|
+
将`wei-backend`添加到`Gemfile`里:
|
6
|
+
|
7
|
+
gem 'wei-backend'
|
5
8
|
|
6
|
-
|
9
|
+
* ### 使用`gem`直接安装:
|
7
10
|
|
8
|
-
|
11
|
+
gem install wei-backend
|
12
|
+
|
13
|
+
## 如何使用
|
14
|
+
* 创建一个文件,如app.js,写入如下内容:
|
15
|
+
|
16
|
+
require ‘sinatra’
|
17
|
+
require 'wei-backend'
|
18
|
+
|
19
|
+
on_text do
|
20
|
+
"Received a text message: #{params[:Content]}!!, and send back a text message!"
|
21
|
+
end
|
9
22
|
|
10
|
-
## 如何定制
|
11
23
|
* 启动
|
12
24
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
25
|
+
ruby app.rb
|
26
|
+
* 测试
|
27
|
+
|
28
|
+
```
|
29
|
+
curl -H 'Content-type:text/xml' -d@- localhost:4567 << EOF
|
30
|
+
<xml>
|
31
|
+
<ToUserName><![CDATA[toUser]]></ToUserName>
|
32
|
+
<FromUserName><![CDATA[fromUser]]></FromUserName>
|
33
|
+
<CreateTime>1348831860</CreateTime>
|
34
|
+
<MsgType><![CDATA[text]]></MsgType>
|
35
|
+
<Content><![CDATA[This is a text message]]></Content>
|
36
|
+
<MsgId>1234567890123456</MsgId>
|
37
|
+
</xml>
|
38
|
+
EOF
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
将会得到一段text返回值,一切OK:
|
43
|
+
|
44
|
+
```
|
45
|
+
<xml>
|
46
|
+
<ToUserName><![CDATA[fromUser]]></ToUserName>
|
47
|
+
<FromUserName><![CDATA[toUser]]></FromUserName>
|
48
|
+
<CreateTime><![CDATA[1386522760]]></CreateTime>
|
49
|
+
<MsgType><![CDATA[text]]></MsgType>
|
50
|
+
<Content><![CDATA[Received a text message: This is a text message!!, and send back a text message!]]></Content>
|
51
|
+
</xml>
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
## 接口说明:
|
56
|
+
|
57
|
+
1. ### on_text
|
58
|
+
当用户向微信公众发送消息的时候,微信会POST一段XML到公众号的后台服务器,`on_text`方法中定义的代码会处理这个请求,这`on_text`方法中可以访问到的请求参数:
|
59
|
+
|
60
|
+
* `params[:ToUserName]`: 发送请求的用户
|
61
|
+
* `params[:FromUserName]`: 公众号用户
|
62
|
+
* `params[:CreateTime]`: 创建时间
|
63
|
+
* `params[:MsgType]`: 消息类型,在这里是text
|
64
|
+
* `params[:Content]`: 消息内容
|
65
|
+
|
66
|
+
1. ### on_event
|
67
|
+
处理微信发送过来的event请求:
|
68
|
+
|
69
|
+
* `params[:ToUserName]`: 发送请求的用户
|
70
|
+
* `params[:FromUserName]`: 公众号用户
|
71
|
+
* `params[:CreateTime]`: 创建时间
|
72
|
+
* `params[:MsgType]`: 消息类型,在这里是event
|
73
|
+
* `params[:Event]`: 消息内容,如_**subscribe**_, _**unsubscribe**_
|
17
74
|
|
18
|
-
访问`http://localhost:9393?echostr=test`,页面会显示**echostr**的值
|
19
75
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
76
|
+
1. ### on_location
|
77
|
+
当用户想微信公众号分享位置信息时,微信会POST相应的位置信息到公众号后台服务器
|
78
|
+
* `params[:ToUserName]`: 发送请求的用户
|
79
|
+
* `params[:FromUserName]`: 公众号用户
|
80
|
+
* `params[:CreateTime]`: 创建时间
|
81
|
+
* `params[:MsgType]`: 消息类型,在这里是location
|
82
|
+
* `params[:Latitude]`: 地理位置纬度
|
83
|
+
* `params[:Longitude]`: 地理位置经度 * `params[:Precision]`: 地理位置精度
|
84
|
+
|
File without changes
|
data/lib/wei-backend/base.rb
CHANGED
@@ -8,6 +8,10 @@ module WeiBackend
|
|
8
8
|
create_model results
|
9
9
|
end
|
10
10
|
|
11
|
+
def handle_event_message
|
12
|
+
send(:"handle_#{params[:Event].downcase}_message")
|
13
|
+
end
|
14
|
+
|
11
15
|
def create_model data
|
12
16
|
data.is_a?(Hash) || data.is_a?(Array) ? image_text_message(data) : text_message(data)
|
13
17
|
end
|
@@ -52,6 +56,14 @@ module WeiBackend
|
|
52
56
|
define_method(:handle_location_message, &block)
|
53
57
|
end
|
54
58
|
|
59
|
+
def self.on_subscribe &block
|
60
|
+
define_method(:handle_location_message, &block)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.on_unsubscribe &block
|
64
|
+
define_method(:handle_location_message, &block)
|
65
|
+
end
|
66
|
+
|
55
67
|
|
56
68
|
end
|
57
69
|
|
@@ -65,7 +77,7 @@ module WeiBackend
|
|
65
77
|
end
|
66
78
|
end
|
67
79
|
|
68
|
-
delegate :on_text, :on_event, :on_voice, :on_location
|
80
|
+
delegate :on_text, :on_event, :on_voice, :on_location, :on_subscribe, :on_unsubscribe
|
69
81
|
|
70
82
|
class << self
|
71
83
|
attr_accessor :target
|
data/lib/wei-backend/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -15,6 +15,25 @@ EVENT_MESSAGE_REQUEST='<xml><ToUserName><![CDATA[toUser]]></ToUserName>'+
|
|
15
15
|
'<FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>123456789</CreateTime>'+
|
16
16
|
'<MsgType><![CDATA[event]]></MsgType><Event><![CDATA[subscribe]]></Event></xml>'
|
17
17
|
|
18
|
+
LOCATION_EVENT_REQUEST='<xml>
|
19
|
+
<ToUserName><![CDATA[toUser]]></ToUserName>
|
20
|
+
<FromUserName><![CDATA[fromUser]]></FromUserName>
|
21
|
+
<CreateTime>123456789</CreateTime>
|
22
|
+
<MsgType><![CDATA[event]]></MsgType>
|
23
|
+
<Event><![CDATA[LOCATION]]></Event>
|
24
|
+
<Latitude>23.137466</Latitude>
|
25
|
+
<Longitude>113.352425</Longitude>
|
26
|
+
<Precision>119.385040</Precision>
|
27
|
+
</xml>'
|
28
|
+
|
29
|
+
SUBSCRIBE_EVENT_REQUEST='<xml>
|
30
|
+
<ToUserName><![CDATA[toUser]]></ToUserName>
|
31
|
+
<FromUserName><![CDATA[FromUser]]></FromUserName>
|
32
|
+
<CreateTime>123456789</CreateTime>
|
33
|
+
<MsgType><![CDATA[event]]></MsgType>
|
34
|
+
<Event><![CDATA[subscribe]]></Event>
|
35
|
+
</xml>'
|
36
|
+
|
18
37
|
PARSED_PARAMS={
|
19
38
|
:ToUserName => 'toUser',
|
20
39
|
:FromUserName => 'fromUser',
|
data/spec/wei_backend_spec.rb
CHANGED
@@ -39,14 +39,24 @@ describe 'app' do
|
|
39
39
|
last_response.body.should include '<Title><![CDATA[title1]]></Title>'
|
40
40
|
end
|
41
41
|
|
42
|
-
it 'should echo
|
43
|
-
WeiBackend::MessageDispatcher.
|
44
|
-
|
42
|
+
it 'should echo location message when receive a location event message' do
|
43
|
+
WeiBackend::MessageDispatcher.on_location do
|
44
|
+
"location event: #{params[:Latitude]}"
|
45
45
|
end
|
46
46
|
|
47
|
-
post '/',
|
47
|
+
post '/', LOCATION_EVENT_REQUEST, 'CONTENT_TYPE' => 'text/xml'
|
48
48
|
last_response.body.should include '<ToUserName><![CDATA[fromUser]]></ToUserName>'
|
49
|
-
last_response.body.should include '<Content><![CDATA[
|
49
|
+
last_response.body.should include '<Content><![CDATA[location event: 23.137466]]></Content>'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should echo location message when receive a location event message' do
|
53
|
+
WeiBackend::MessageDispatcher.on_subscribe do
|
54
|
+
'Thank you for subscribe!'
|
55
|
+
end
|
56
|
+
|
57
|
+
post '/', LOCATION_EVENT_REQUEST, 'CONTENT_TYPE' => 'text/xml'
|
58
|
+
last_response.body.should include '<ToUserName><![CDATA[fromUser]]></ToUserName>'
|
59
|
+
last_response.body.should include '<Content><![CDATA[Thank you for subscribe!]]></Content>'
|
50
60
|
end
|
51
61
|
|
52
62
|
end
|
data/wei-backend.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
require 'wei-backend/version'
|
3
|
+
|
4
|
+
Gem::Specification.new 'wei-backend', WeiBackend::VERSION do |s|
|
5
|
+
s.description = 'wei-backend is a DSL for quickly creating weixin open platform backend system.'
|
6
|
+
s.summary = 'Best DSL for weixin development'
|
7
|
+
s.authors = ['Wang Chao']
|
8
|
+
s.email = 'cwang8023@gmail.com'
|
9
|
+
s.homepage = 'https://github.com/charleyw/weixin-sinatra'
|
10
|
+
s.license = 'MIT'
|
11
|
+
s.files = `git ls-files`.split("\n") - %w[.gitignore .travis.yml .ruby-version .ruby-gemset]
|
12
|
+
s.test_files = s.files.select { |p| p =~ /^spec\/.*_spec.rb/ }
|
13
|
+
|
14
|
+
s.add_dependency 'sinatra', '~> 1.4.4'
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wei-backend
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wang Chao
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -34,7 +34,7 @@ files:
|
|
34
34
|
- Gemfile
|
35
35
|
- Gemfile.lock
|
36
36
|
- README.md
|
37
|
-
- Rakefile
|
37
|
+
- examples/waiting_bus/Rakefile
|
38
38
|
- examples/waiting_bus/ai_bang_client.rb
|
39
39
|
- examples/waiting_bus/app.rb
|
40
40
|
- examples/waiting_bus/bus_helper.rb
|
@@ -56,7 +56,8 @@ files:
|
|
56
56
|
- spec/wei_backend_spec.rb
|
57
57
|
- spec/wei_backend_utils_spec.rb
|
58
58
|
- spec/weixin_message_handler_spec.rb
|
59
|
-
|
59
|
+
- wei-backend.gemspec
|
60
|
+
homepage: https://github.com/charleyw/weixin-sinatra
|
60
61
|
licenses:
|
61
62
|
- MIT
|
62
63
|
metadata: {}
|