wei-backend 0.0.6 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff325112bc64a9c7e4016405c46d7f74eb885f01
4
- data.tar.gz: 0ce3a35ddc850c70f6b92b9c01ff870144a52b06
3
+ metadata.gz: 284f669a73962a222fddf28616a039729db7d7e3
4
+ data.tar.gz: f6d887248c233c2722562302bbfafb75691e8ebc
5
5
  SHA512:
6
- metadata.gz: c6d0a648a635c7a49311130b2c2b3717dbe618def0427f27bb42b4e2f9a8b0ea628df5631ede4684e23a5098f21f03c071abf3ac8b5d34ecc15698f0f49bf8e5
7
- data.tar.gz: cf4c1b08ddb62a25b899ce1a0123ecf77254bf1dc31fdc587d1adfe215bedd97bc3ddaad18c2066a31939e6f913c47ec62d14b7ebab5a59ff3740ff700d3e242
6
+ metadata.gz: c8720c415ee7e9db7f648fbb5c2db427311226655fc55078f70af65326c860f9c43f2837a78c9f734f2393d31a94944f8a7e3c38b5cce9b31c8eced3c02f285c
7
+ data.tar.gz: 291580ee8bcc4ac7211fbf6cfaadbd513f040da7197c840ab3ef8685167c03a72982f46a6301dc7217dddf615bd490f7b5753c95400fe91fbb2dfd1a24a2bebe
data/README.md CHANGED
@@ -23,6 +23,8 @@
23
23
  require 'sinatra'
24
24
  require 'wei-backend'
25
25
 
26
+ token "mytoken"
27
+
26
28
  on_text do
27
29
  "你发送了如下内容: #{params[:Content]}!!"
28
30
  end
@@ -45,9 +45,16 @@ module WeiBackend
45
45
  }
46
46
  end
47
47
 
48
- %w(text event voice location subscribe unsubscribe).each do |type|
49
- define_singleton_method(:"on_#{type}") do |&block|
50
- define_method(:"handle_#{type}_message", &block)
48
+ class << self
49
+ %w(text event voice location subscribe unsubscribe).each do |type|
50
+ define_method(:"on_#{type}") do |&block|
51
+ define_method(:"handle_#{type}_message", &block)
52
+ end
53
+ end
54
+
55
+ def token str=nil
56
+ return @token if str.nil?
57
+ @token = str
51
58
  end
52
59
  end
53
60
 
@@ -63,7 +70,7 @@ module WeiBackend
63
70
  end
64
71
  end
65
72
 
66
- delegate :on_text, :on_event, :on_voice, :on_location, :on_subscribe, :on_unsubscribe
73
+ delegate :on_text, :on_event, :on_voice, :on_location, :on_subscribe, :on_unsubscribe, :token
67
74
 
68
75
  class << self
69
76
  attr_accessor :target
@@ -1,11 +1,26 @@
1
1
  require 'sinatra'
2
2
  require 'nokogiri'
3
+ require 'digest/sha1'
3
4
 
4
- get '/' do
5
+ set(:access_token) { |token_proc|
6
+ condition do
7
+ token = token_proc.call
8
+ return true if token.nil? || token.empty?
9
+ origin_signature_strings = [token, params[:timestamp], params[:nonce]]
10
+ signature = Digest::SHA1.hexdigest origin_signature_strings.sort!.join
11
+ signature.eql? params[:signature]
12
+ end
13
+ }
14
+
15
+ token_proc = proc {
16
+ WeiBackend::MessageDispatcher.token
17
+ }
18
+
19
+ get '/', :access_token => token_proc do
5
20
  params[:echostr]
6
21
  end
7
22
 
8
- post '/' do
23
+ post '/', :access_token => token_proc do
9
24
  request.body.rewind
10
25
  weixin_params = WeiBackend::Utils.parse_params request.body.read
11
26
  handler = WeiBackend::MessageDispatcher.new
@@ -1,3 +1,3 @@
1
1
  module WeiBackend
2
- VERSION = '0.0.6'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -69,4 +69,16 @@ describe 'app' do
69
69
  last_response.body.should include '<Content><![CDATA[Thank you for subscribe!]]></Content>'
70
70
  end
71
71
 
72
+ it 'should do something when user have set token' do
73
+ WeiBackend::MessageDispatcher.on_subscribe do
74
+ 'Thank you for subscribe!'
75
+ end
76
+
77
+ WeiBackend::MessageDispatcher.token 'combustest'
78
+
79
+ post '/?signature=0d144fa22f4119dbb2f6fe9710f3b732fb45092b&timestamp=1388674716&nonce=1388564676', SUBSCRIBE_EVENT_REQUEST, 'CONTENT_TYPE' => 'text/xml', 'token' => 'token'
80
+ last_response.body.should include '<ToUserName><![CDATA[fromUser]]></ToUserName>'
81
+ last_response.body.should include '<Content><![CDATA[Thank you for subscribe!]]></Content>'
82
+ end
83
+
72
84
  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.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wang Chao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-13 00:00:00.000000000 Z
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra