wei-backend 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efd7c139fbed96e67acc306ca3ead44d1b86cbde
4
- data.tar.gz: d237dceed8014aa44670e6ab152c5b01a95dad05
3
+ metadata.gz: 23dbcfa965bc6620b9c97fa48fbc177cdd3a3c1b
4
+ data.tar.gz: 6b06a2ffe5d7d029f1b5fbae6982fcfc123a22ac
5
5
  SHA512:
6
- metadata.gz: 6858cfc02322e9f18a3c5bc332b112ec6d66f0ff54ce6240aa52bf56faff531ec85e6a1a1592520422030f7bf95f5bbbf59ae6db691fa5fcffba18ae634a232d
7
- data.tar.gz: 2f926684b91a5d02f79acedca03dbb201b3c9204a0022381cd80d9ff02c6aa5a39fcf4ff59559c6e95d625e937e0302199f2631eb92c7edc326cf6a8b30b62cf
6
+ metadata.gz: 35a59017b78eb7949354ba531d4dcef446ca2a3758f51e1597a057d03c81ba7b6b46d5b203c850bdc5a94dd46019c77142294a364c6718ad294a6e39442c4fad
7
+ data.tar.gz: 84d270c3beaa61d60bde04e396cf4c542912cf592e38ce3e9f5652d5936a3c13c575a783213461c1c844a29cfdcddcba870a0f4bd58e848b8f9def598e58b806
data/README.md CHANGED
@@ -1,17 +1,54 @@
1
- 微信公众平台后台框架
2
- ========
3
- ## 如何安装:
4
- * ### 使用bundler
5
- 将`wei-backend`添加到`Gemfile`里:
1
+ ## 微信公众平台后台框架
2
+ 微信公众平台 后台框架,基于sinatra,使用DSL的思想帮助你以最快的**速度**开启微信公众平台开发.
3
+
4
+ ## 仅需3步,一个文件创建一个微信后台程序
5
+ 1. 安装Gem
6
+
7
+ gem install 'wei-backend'
8
+
9
+ 1. 创建微信后台主程序**app.rb**, 内容如下:
6
10
 
7
- gem 'wei-backend'
11
+ require 'sinatra'
12
+ require 'wei-backend'
13
+
14
+ on_text do
15
+ "你发送了如下内容: #{params[:Content]}!!"
16
+ end
17
+
18
+ on_subscribe do
19
+ "感谢您的订阅"
20
+ end
21
+
22
+ on_unsubscribe do
23
+ "欢迎您再次订阅"
24
+ end
25
+
26
+ 1. 启动
27
+
28
+ ruby app.rb
29
+
30
+ ## 与微信接口兼容情况:
8
31
 
9
- * ### 使用`gem`直接安装:
32
+ * 现在可以处理的消息类型(被动接受用户消息类型):
10
33
 
11
- gem install wei-backend
34
+ | 消息类型 | 接口 |
35
+ | ------------ | ------------- |
36
+ | 文本消息 | on_text |
37
+ | 订阅事件 | on_subscribe |
38
+ | 取消订阅事件 | on_unsubscribe |
39
+ | 用户被动分享地理位置 | on_location |
40
+
41
+ 后续还会加入消息类型支持
42
+
43
+ * 可以发送的消息类型:
44
+
45
+ | 消息类型 | 格式 |
46
+ | ------------ | ------------- |
47
+ | 文本消息 | ruby 字符串 |
48
+ | 图文事件 | ruby hash值 |
12
49
 
13
50
  ## 如何使用
14
- * 创建一个文件,如app.js,写入如下内容:
51
+ * 创建一个文件,如app.rb,写入如下内容:
15
52
 
16
53
  require ‘sinatra’
17
54
  require 'wei-backend'
@@ -53,8 +90,9 @@ EOF
53
90
  ```
54
91
 
55
92
  ## 接口说明:
93
+ 各个接口只能定义一次,重复定义会覆盖之前定义的接口
56
94
 
57
- 1. ### on_text
95
+ * ### on_text
58
96
  当用户向微信公众发送消息的时候,微信会POST一段XML到公众号的后台服务器,`on_text`方法中定义的代码会处理这个请求,这`on_text`方法中可以访问到的请求参数:
59
97
 
60
98
  * `params[:ToUserName]`: 发送请求的用户
@@ -63,17 +101,27 @@ EOF
63
101
  * `params[:MsgType]`: 消息类型,在这里是text
64
102
  * `params[:Content]`: 消息内容
65
103
 
66
- 1. ### on_event
67
- 处理微信发送过来的event请求:
104
+ * ### on_subscribe
105
+ 当用户关注时,处理消息的接口:
106
+
107
+ * `params[:ToUserName]`: 发送请求的用户
108
+ * `params[:FromUserName]`: 公众号用户
109
+ * `params[:CreateTime]`: 创建时间
110
+ * `params[:MsgType]`: 消息类型,在这里是event
111
+ * `params[:Event]`: 事件类型,_**subscribe**_
112
+
113
+ * ### on_unsubscribe
114
+ 当用户取消关注时,处理消息的接口:
68
115
 
69
116
  * `params[:ToUserName]`: 发送请求的用户
70
117
  * `params[:FromUserName]`: 公众号用户
71
118
  * `params[:CreateTime]`: 创建时间
72
119
  * `params[:MsgType]`: 消息类型,在这里是event
73
- * `params[:Event]`: 消息内容,如_**subscribe**_, _**unsubscribe**_
120
+ * `params[:Event]`: 事件类型,_**unsubscribe**_
121
+
74
122
 
75
123
 
76
- 1. ### on_location
124
+ * ### on_location
77
125
  当用户想微信公众号分享位置信息时,微信会POST相应的位置信息到公众号后台服务器
78
126
  * `params[:ToUserName]`: 发送请求的用户
79
127
  * `params[:FromUserName]`: 公众号用户
@@ -81,4 +129,31 @@ EOF
81
129
  * `params[:MsgType]`: 消息类型,在这里是location
82
130
  * `params[:Latitude]`: 地理位置纬度
83
131
  * `params[:Longitude]`: 地理位置经度 * `params[:Precision]`: 地理位置精度
84
-
132
+
133
+ ## 返回消息
134
+ * 返回文本消息:
135
+
136
+ on_text do
137
+ "Received a text message: #{params[:Content]}!!, and send back a text message!"
138
+ end
139
+
140
+ * 返回图文消息
141
+
142
+ on_text do
143
+ [{
144
+ :title => '收到一个文本消息,返回两个图文消息',
145
+ :description => 'desc',
146
+ :picture_url => 'pic url',
147
+ :url => 'url'
148
+ },
149
+ {
150
+ :title => '这是第二个图文消息',
151
+ :description => 'desc1',
152
+ :picture_url => 'pic url1',
153
+ :url => 'url1'
154
+ }]
155
+ end
156
+
157
+ ## Liscense
158
+
159
+ © 2013 Wang Chao. This code is distributed under the MIT license.
@@ -0,0 +1,16 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'thin'
4
+ gem 'hash_validator'
5
+ gem 'wei-backend'
6
+
7
+ group :test do
8
+ gem 'rspec'
9
+ gem 'rspec-html-matchers'
10
+ gem 'rack-test'
11
+ end
12
+
13
+ group :development do
14
+ gem 'rerun'
15
+ gem 'shotgun'
16
+ end
@@ -0,0 +1,93 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ capistrano (3.0.1)
5
+ i18n
6
+ rake (>= 10.0.0)
7
+ sshkit (>= 0.0.23)
8
+ daemons (1.1.9)
9
+ diff-lcs (1.2.5)
10
+ eventmachine (1.0.3)
11
+ ffi (1.9.3)
12
+ haml (4.0.4)
13
+ tilt
14
+ hash_validator (0.2.7)
15
+ httparty (0.12.0)
16
+ json (~> 1.8)
17
+ multi_xml (>= 0.5.2)
18
+ i18n (0.6.9)
19
+ json (1.8.1)
20
+ listen (1.0.3)
21
+ rb-fsevent (>= 0.9.3)
22
+ rb-inotify (>= 0.9)
23
+ rb-kqueue (>= 0.2)
24
+ mini_portile (0.5.2)
25
+ multi_xml (0.5.5)
26
+ net-scp (1.1.2)
27
+ net-ssh (>= 2.6.5)
28
+ net-ssh (2.7.0)
29
+ nokogiri (1.6.0)
30
+ mini_portile (~> 0.5.0)
31
+ rack (1.5.2)
32
+ rack-protection (1.5.1)
33
+ rack
34
+ rack-test (0.6.2)
35
+ rack (>= 1.0)
36
+ rake (10.1.0)
37
+ rb-fsevent (0.9.3)
38
+ rb-inotify (0.9.2)
39
+ ffi (>= 0.5.0)
40
+ rb-kqueue (0.2.0)
41
+ ffi (>= 0.5.0)
42
+ rerun (0.8.2)
43
+ listen (~> 1.0.3)
44
+ rspec (2.14.1)
45
+ rspec-core (~> 2.14.0)
46
+ rspec-expectations (~> 2.14.0)
47
+ rspec-mocks (~> 2.14.0)
48
+ rspec-core (2.14.7)
49
+ rspec-expectations (2.14.4)
50
+ diff-lcs (>= 1.1.3, < 2.0)
51
+ rspec-html-matchers (0.4.3)
52
+ nokogiri (>= 1.4.4)
53
+ rspec (>= 2.0.0)
54
+ rspec-mocks (2.14.4)
55
+ shotgun (0.9)
56
+ rack (>= 1.0)
57
+ sinatra (1.4.4)
58
+ rack (~> 1.4)
59
+ rack-protection (~> 1.4)
60
+ tilt (~> 1.3, >= 1.3.4)
61
+ sshkit (1.2.0)
62
+ net-scp (>= 1.1.2)
63
+ net-ssh
64
+ term-ansicolor
65
+ term-ansicolor (1.2.2)
66
+ tins (~> 0.8)
67
+ thin (1.6.1)
68
+ daemons (>= 1.0.9)
69
+ eventmachine (>= 1.0.0)
70
+ rack (>= 1.0.0)
71
+ tilt (1.4.1)
72
+ tins (0.13.1)
73
+ wei-backend (0.0.2)
74
+ sinatra (~> 1.4.4)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ capistrano
81
+ haml
82
+ hash_validator
83
+ httparty
84
+ json
85
+ nokogiri
86
+ rack-test
87
+ rerun
88
+ rspec
89
+ rspec-html-matchers
90
+ shotgun
91
+ sinatra
92
+ thin
93
+ wei-backend
@@ -0,0 +1,17 @@
1
+ require 'sinatra'
2
+ require 'wei-backend'
3
+
4
+ on_location do
5
+ "You location:\n\t lat: #{params[:Latitude]} \n\t long: #{params[:Longitude]}"
6
+ end
7
+
8
+ on_subscribe do
9
+ "Please share your location information"
10
+ end
11
+
12
+ on_text do
13
+
14
+ #"hello world #{params[:Latitude]}"
15
+ [{:title=>"", :description=>"", :picturl=>"", :url=>""},{:title=>"", :description=>"", :picturl=>"", :url=>""}]
16
+ end
17
+
@@ -0,0 +1,12 @@
1
+ require './app'
2
+
3
+ ENV['RACK_ENV'] ||= development
4
+
5
+ root_dir = File.dirname(__FILE__)
6
+
7
+ set :environment, ENV['RACK_ENV'].to_sym
8
+ set :root, root_dir
9
+ set :app_file, File.join(root_dir, 'app.rb')
10
+ disable :run
11
+
12
+ run Sinatra::Application
@@ -0,0 +1,22 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'json'
4
+ gem 'haml'
5
+ gem 'sinatra'
6
+ gem 'nokogiri'
7
+ gem 'capistrano'
8
+ gem 'httparty'
9
+ gem 'thin'
10
+ gem 'hash_validator'
11
+ gem 'wei-backend'
12
+
13
+ group :test do
14
+ gem 'rspec'
15
+ gem 'rspec-html-matchers'
16
+ gem 'rack-test'
17
+ end
18
+
19
+ group :development do
20
+ gem 'rerun'
21
+ gem 'shotgun'
22
+ end
@@ -0,0 +1,93 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ capistrano (3.0.1)
5
+ i18n
6
+ rake (>= 10.0.0)
7
+ sshkit (>= 0.0.23)
8
+ daemons (1.1.9)
9
+ diff-lcs (1.2.5)
10
+ eventmachine (1.0.3)
11
+ ffi (1.9.3)
12
+ haml (4.0.4)
13
+ tilt
14
+ hash_validator (0.2.7)
15
+ httparty (0.12.0)
16
+ json (~> 1.8)
17
+ multi_xml (>= 0.5.2)
18
+ i18n (0.6.9)
19
+ json (1.8.1)
20
+ listen (1.0.3)
21
+ rb-fsevent (>= 0.9.3)
22
+ rb-inotify (>= 0.9)
23
+ rb-kqueue (>= 0.2)
24
+ mini_portile (0.5.2)
25
+ multi_xml (0.5.5)
26
+ net-scp (1.1.2)
27
+ net-ssh (>= 2.6.5)
28
+ net-ssh (2.7.0)
29
+ nokogiri (1.6.0)
30
+ mini_portile (~> 0.5.0)
31
+ rack (1.5.2)
32
+ rack-protection (1.5.1)
33
+ rack
34
+ rack-test (0.6.2)
35
+ rack (>= 1.0)
36
+ rake (10.1.0)
37
+ rb-fsevent (0.9.3)
38
+ rb-inotify (0.9.2)
39
+ ffi (>= 0.5.0)
40
+ rb-kqueue (0.2.0)
41
+ ffi (>= 0.5.0)
42
+ rerun (0.8.2)
43
+ listen (~> 1.0.3)
44
+ rspec (2.14.1)
45
+ rspec-core (~> 2.14.0)
46
+ rspec-expectations (~> 2.14.0)
47
+ rspec-mocks (~> 2.14.0)
48
+ rspec-core (2.14.7)
49
+ rspec-expectations (2.14.4)
50
+ diff-lcs (>= 1.1.3, < 2.0)
51
+ rspec-html-matchers (0.4.3)
52
+ nokogiri (>= 1.4.4)
53
+ rspec (>= 2.0.0)
54
+ rspec-mocks (2.14.4)
55
+ shotgun (0.9)
56
+ rack (>= 1.0)
57
+ sinatra (1.4.4)
58
+ rack (~> 1.4)
59
+ rack-protection (~> 1.4)
60
+ tilt (~> 1.3, >= 1.3.4)
61
+ sshkit (1.2.0)
62
+ net-scp (>= 1.1.2)
63
+ net-ssh
64
+ term-ansicolor
65
+ term-ansicolor (1.2.2)
66
+ tins (~> 0.8)
67
+ thin (1.6.1)
68
+ daemons (>= 1.0.9)
69
+ eventmachine (>= 1.0.0)
70
+ rack (>= 1.0.0)
71
+ tilt (1.4.1)
72
+ tins (0.13.1)
73
+ wei-backend (0.0.2)
74
+ sinatra (~> 1.4.4)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ capistrano
81
+ haml
82
+ hash_validator
83
+ httparty
84
+ json
85
+ nokogiri
86
+ rack-test
87
+ rerun
88
+ rspec
89
+ rspec-html-matchers
90
+ shotgun
91
+ sinatra
92
+ thin
93
+ wei-backend
@@ -40,31 +40,12 @@ module WeiBackend
40
40
  }
41
41
  end
42
42
 
43
- def self.on_text &block
44
- define_method(:handle_text_message, &block)
45
- end
46
-
47
- def self.on_event &block
48
- define_method(:handle_event_message, &block)
49
- end
50
-
51
- def self.on_voice &block
52
- define_method(:handle_voice_message, &block)
53
- end
54
-
55
- def self.on_location &block
56
- define_method(:handle_location_message, &block)
57
- end
58
-
59
- def self.on_subscribe &block
60
- define_method(:handle_subscribe_message, &block)
61
- end
62
-
63
- def self.on_unsubscribe &block
64
- define_method(:handle_unsubscribe_message, &block)
43
+ %w(text event voice location subscribe unsubscribe).each do |type|
44
+ define_singleton_method(:"on_#{type}") do |&block|
45
+ define_method(:"handle_#{type}_message", &block)
46
+ end
65
47
  end
66
48
 
67
-
68
49
  end
69
50
 
70
51
  module Delegator
@@ -1,3 +1,3 @@
1
1
  module WeiBackend
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -28,7 +28,7 @@ LOCATION_EVENT_REQUEST='<xml>
28
28
 
29
29
  SUBSCRIBE_EVENT_REQUEST='<xml>
30
30
  <ToUserName><![CDATA[toUser]]></ToUserName>
31
- <FromUserName><![CDATA[FromUser]]></FromUserName>
31
+ <FromUserName><![CDATA[fromUser]]></FromUserName>
32
32
  <CreateTime>123456789</CreateTime>
33
33
  <MsgType><![CDATA[event]]></MsgType>
34
34
  <Event><![CDATA[subscribe]]></Event>
@@ -29,8 +29,18 @@ describe 'app' do
29
29
 
30
30
  it 'should return news message when receive a text request message and user defined a multi-news' do
31
31
  WeiBackend::MessageDispatcher.on_text do
32
- [{:title => 'title', :description => 'desc', :picture_url => 'pic url', :url => 'url'},
33
- {:title => 'title1', :description => 'desc1', :picture_url => 'pic url1', :url => 'url1'}]
32
+ [{
33
+ :title => 'title',
34
+ :description => 'desc',
35
+ :picture_url => 'pic url',
36
+ :url => 'url'
37
+ },
38
+ {
39
+ :title => 'title1',
40
+ :description => 'desc1',
41
+ :picture_url => 'pic url1',
42
+ :url => 'url1'
43
+ }]
34
44
  end
35
45
 
36
46
  post '/', TEXT_MESSAGE_REQUEST, 'CONTENT_TYPE' => 'text/xml'
@@ -54,7 +64,7 @@ describe 'app' do
54
64
  'Thank you for subscribe!'
55
65
  end
56
66
 
57
- post '/', LOCATION_EVENT_REQUEST, 'CONTENT_TYPE' => 'text/xml'
67
+ post '/', SUBSCRIBE_EVENT_REQUEST, 'CONTENT_TYPE' => 'text/xml'
58
68
  last_response.body.should include '<ToUserName><![CDATA[fromUser]]></ToUserName>'
59
69
  last_response.body.should include '<Content><![CDATA[Thank you for subscribe!]]></Content>'
60
70
  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
4
+ version: 0.0.5
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-10 00:00:00.000000000 Z
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -76,6 +76,12 @@ files:
76
76
  - Gemfile
77
77
  - Gemfile.lock
78
78
  - README.md
79
+ - examples/locations/Gemfile
80
+ - examples/locations/Gemfile.lock
81
+ - examples/locations/app.rb
82
+ - examples/locations/config.ru
83
+ - examples/waiting_bus/Gemfile
84
+ - examples/waiting_bus/Gemfile.lock
79
85
  - examples/waiting_bus/Rakefile
80
86
  - examples/waiting_bus/ai_bang_client.rb
81
87
  - examples/waiting_bus/app.rb