easy_weixin 0.0.9 → 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: 9206ee70846db436d192ff9fb4c2764a1c48df5b
4
- data.tar.gz: 72756ef2564b95fbe36f3015b4d712799b9d76da
3
+ metadata.gz: 0441570dd87ce9e4cf8311ed51603591805e773d
4
+ data.tar.gz: 4e1988ce9b0501884b545cf70cdea68584e5faec
5
5
  SHA512:
6
- metadata.gz: 4d10ecaca75544322f6de133d2a891386bde6a73ad519c6893b5be721df2692051e7d77d0e352f484e00e7faaa17663c25123c7a429673fdaeb914d13fd422f0
7
- data.tar.gz: 6d52b70b5ddde6433d2c240126731f000996254f174b5290655fa911ab70c3fd04f88b5f4d386211fd7364c7e6fd21eb5107737a0b39c1a6a2fd384bbd61a5a3
6
+ metadata.gz: be0eb93ce57ecd370ada2097c7927fece940d064a2688260147e5958aa4566ae536d1d8ebf0824dba3f1ecd20a0b34b3309421ed4b9d0f59f906f8bb7fe26b33
7
+ data.tar.gz: 5d6dc102a8bd1b199004f45e275ebf9761b292a08d0fd2db747e7e3ead87fe139f4048c4516830dcf19f22e3a3ff1c7212550d5dee7cb6a49a08970189e52f1f
data/README.md CHANGED
@@ -1,37 +1,51 @@
1
1
  # EasyWeixin
2
2
 
3
- A Simple gem for weixin
3
+ A Simple gem for weixin
4
4
 
5
- ## Installation
5
+ # Installation
6
6
 
7
7
  gem install easy_weixin
8
8
 
9
- ## Usage
9
+ ### Rails use redis
10
10
 
11
- check_valid
11
+ ```
12
+ EasyWeixin.configure do |config|
13
+ config.redis = Redis.new
14
+ end
15
+ ```
12
16
 
13
- ### check
17
+ # Usage
14
18
 
15
- timestamp = params[:timestamp]
16
- nonce = params[:nonce]
17
- signature = params[:signature]
18
- echostr = params[:echostr]
19
- token = 'xxxxx'
20
- easy_weixin = EasyWeixin::Auth.new(token, timestamp, nonce, signature, echostr) # => echostr or false
19
+ ### 验证
21
20
 
22
- ### analysis xml
23
- request_xml = Nokogiri::XML(request.body.read).to_s
24
- msg = EasyWeixin.analysis_xml(request_xml)
21
+ ```
22
+ easy_weixin = EasyWeixin::Auth.new(@token, @timestamp, @nonce, @signature, @echostr)
23
+ easy_weixin.check_valid # => echostr or false
24
+ ```
25
25
 
26
- ### reply
26
+ ### 创建菜单
27
+
28
+ ```
29
+ $client = EasyWeixin::Api.new(ENV["APPID"], ENV["APPSECRET"])
30
+ $client.create_menu(data)
31
+ ```
32
+
33
+ ### 解析XML
34
+ ```
35
+ request_xml = Nokogiri::XML(request.body.read).to_s
36
+ msg = EasyWeixin::Response.analysis_xml(request_xml)
37
+ ```
38
+
39
+ ### 回复
27
40
 
28
41
  * ReplyText
29
42
  ```
30
43
  EasyWeixin.text_msg(msg.ToUserName, msg.FromUserName, 'i am a text reply')
31
44
  ```
45
+
32
46
  * ReplyNews
33
47
  ```
34
- msg = EasyWeixin::Message.factory(xml)
48
+ msg = EasyWeixin::Response.analysis_xml(request_xml)
35
49
  if msg.Content == 'Hello2BizUser'
36
50
  item1 = EasyWeixin::Item.new
37
51
  item1.Title = 'title1'
@@ -42,14 +56,9 @@ check_valid
42
56
  Articles = [item1, item2]
43
57
  ArticleCount = Articles.size
44
58
  end
45
-
46
- EasyWeixin.news_msg(msg.ToUserName, msg.FromUserName, Articles, ArticleCount)
59
+ EasyWeixin::Response.news_msg(msg.ToUserName, msg.FromUserName, Articles, ArticleCount)
60
+ ```
61
+ ### 测试
62
+ ```
63
+ rake test
47
64
  ```
48
-
49
- ## Contributing
50
-
51
- 1. Fork it ( http://github.com/<my-github-username>/easy_weixin/fork )
52
- 2. Create your feature branch (`git checkout -b my-new-feature`)
53
- 3. Commit your changes (`git commit -am 'Add some feature'`)
54
- 4. Push to the branch (`git push origin my-new-feature`)
55
- 5. Create new Pull Request
data/Rakefile CHANGED
@@ -1 +1,11 @@
1
- require "bundler/gem_tasks"
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ # libs表示要添加到$LOAD_PATH(就是加载ruby文件的搜索路径)的文件夹
5
+ # 默认是"lib",现在再添加"test"
6
+ t.libs << "test"
7
+ # 要运行的测试文件的特征。匹配以test_开头的所有文件
8
+ t.pattern = 'test/*.rb'
9
+ # 不输出测试文件的信息
10
+ t.verbose = false
11
+ end
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = EasyWeixin::VERSION
9
9
  spec.authors = ["menghuanwd"]
10
10
  spec.email = ["651019063@qq.com"]
11
- spec.summary = %q{easy to use weixin.}
12
- spec.description = %q{easy to use weixin.}
11
+ spec.summary = %q{easy to use weixin public api.}
12
+ spec.description = %q{create menu and reply message.}
13
13
  spec.homepage = "https://github.com/menghuanwd/easy_weixin"
14
14
  spec.license = "MIT"
15
15
 
@@ -22,4 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "roxml"
24
24
  spec.add_development_dependency "multi_xml"
25
+ spec.add_development_dependency "faraday"
26
+ spec.add_development_dependency "typhoeus"
27
+ spec.add_development_dependency "redis"
28
+
25
29
  end
@@ -1,40 +1,43 @@
1
1
  require 'multi_xml'
2
2
  require 'ostruct'
3
- require "easy_weixin/model"
3
+ require 'redis'
4
+ require 'roxml'
5
+ require "easy_weixin/config"
4
6
  require "easy_weixin/auth"
7
+ require "easy_weixin/menu"
8
+ require "easy_weixin/response"
9
+ require "easy_weixin/message"
10
+ require "easy_weixin/api"
5
11
  require 'rack'
6
- require 'open-uri'
12
+ require 'faraday'
13
+ require 'typhoeus/adapters/faraday'
7
14
 
8
15
  module EasyWeixin
9
- extend self
16
+ # extend self
10
17
 
11
- def text_msg(from, to, content)
12
- msg = TextReplyMessage.new
13
- msg.ToUserName = to
14
- msg.FromUserName = from
15
- msg.Content = content
16
- msg.to_xml
17
- end
18
+ # def text_msg(from, to, content)
19
+ # msg = TextReplyMessage.new
20
+ # msg.ToUserName = to
21
+ # msg.FromUserName = from
22
+ # msg.Content = content
23
+ # msg.to_xml
24
+ # end
25
+ #
26
+ # def news_msg(from, to, articles, articleCount=1)
27
+ # msg = NewsReplyMessage.new
28
+ # msg.ToUserName = to
29
+ # msg.FromUserName = from
30
+ # unless articles.is_a?(Array)
31
+ # articles = [articles]
32
+ # end
33
+ # msg.Articles = articles
34
+ # msg.ArticleCount = articleCount
35
+ # msg.to_xml
36
+ # end
37
+ #
38
+ # def analysis_xml(request_xml)
39
+ # hash = MultiXml.parse(request_xml)['xml']
40
+ # source = OpenStruct.new(hash)
41
+ # end
18
42
 
19
- def news_msg(from, to, articles, articleCount=1)
20
- msg = NewsReplyMessage.new
21
- msg.ToUserName = to
22
- msg.FromUserName = from
23
- unless articles.is_a?(Array)
24
- articles = [articles]
25
- end
26
- msg.Articles = articles
27
- msg.ArticleCount = articleCount
28
- msg.to_xml
29
- end
30
-
31
- def analysis_xml(request_xml)
32
- hash = MultiXml.parse(request_xml)['xml']
33
- source = OpenStruct.new(hash)
34
- end
35
-
36
- def get_weixin_access_token(appid, appsecret)
37
- url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{appsecret}"
38
- JSON.parse(open(url).read)["access_token"]
39
- end
40
43
  end
@@ -0,0 +1,86 @@
1
+ # 接入API
2
+
3
+ module EasyWeixin
4
+ class Api
5
+ include Menu
6
+
7
+ def initialize(app_id, app_secret)
8
+ @app_id = app_id
9
+ @app_secret = app_secret
10
+ @redis_key = "weixin_access_token_#{app_id}"
11
+ @grant_type = 'client_credential'
12
+ @weixin_api_url = 'https://api.weixin.qq.com'
13
+ end
14
+
15
+ # 获取access_token,如果启用redis则使用redis记录,如果没启用就每次进行一次查询。
16
+ def get_access_token
17
+ if EasyWeixin.weixin_redis
18
+ access_token = weixin_redis.get(@redis_key)
19
+
20
+ if access_token
21
+ access_token
22
+ else
23
+ refresh_token
24
+ end
25
+
26
+ else
27
+ get_access_token_http
28
+ end
29
+ end
30
+
31
+ def get_access_token_http
32
+ conn = Faraday.new(url: "https://api.weixin.qq.com") do |faraday|
33
+ faraday.request :url_encoded
34
+ faraday.adapter :typhoeus
35
+ end
36
+ response = conn.get("cgi-bin/token", authenticate_headers)
37
+
38
+ token_hash = JSON.parse(response.body)
39
+
40
+ access_token = token_hash["access_token"]
41
+ end
42
+
43
+ def refresh_token
44
+ get_access_token_http
45
+
46
+ conn = Faraday.new(url: "https://api.weixin.qq.com") do |faraday|
47
+ faraday.request :url_encoded
48
+ faraday.adapter :typhoeus
49
+ end
50
+ response = conn.get("cgi-bin/token", authenticate_headers)
51
+ token_hash = JSON.parse(response.body)
52
+
53
+ access_token = token_hash["access_token"]
54
+ expires_in = token_hash["expires_in"]
55
+
56
+ weixin_redis.set(@redis_key, access_token)
57
+ weixin_redis.expire(@redis_key, expires_in - 5)
58
+
59
+ token_hash["access_token"]
60
+ end
61
+
62
+ def authenticate_headers
63
+ {grant_type: @grant_type, appid: @app_id, secret: @app_secret}
64
+ end
65
+
66
+ def weixin_redis
67
+ EasyWeixin.weixin_redis
68
+ end
69
+
70
+ private
71
+
72
+ def http_post(url, data)
73
+ conn = Faraday.new(url: @weixin_api_url) do |faraday|
74
+ faraday.request :url_encoded
75
+ faraday.adapter :typhoeus
76
+ end
77
+ response = conn.post do |req|
78
+ req.url url
79
+ req.params['access_token'] = get_access_token
80
+ req.headers['Content-Type'] = 'application/json'
81
+ req.body = data
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -21,4 +21,4 @@ module EasyWeixin
21
21
  end
22
22
  end
23
23
  end
24
- end
24
+ end
@@ -0,0 +1,25 @@
1
+ module EasyWeixin
2
+ class << self
3
+
4
+ attr_accessor :config
5
+
6
+ def configure
7
+ yield self.config ||= Config.new
8
+ end
9
+
10
+ def weixin_redis
11
+ # use add log
12
+ return nil if EasyWeixin.config.nil?
13
+ EasyWeixin.config.redis
14
+ end
15
+
16
+ end
17
+
18
+ class Config
19
+ attr_accessor :redis
20
+ end
21
+ end
22
+
23
+ # EasyWeixin.configure do |config|
24
+ # config.redis = Redis.new
25
+ # end
@@ -0,0 +1,11 @@
1
+ module EasyWeixin
2
+ module Menu
3
+
4
+ # 自定义菜单创建接口
5
+ # https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
6
+ def create_menu(data)
7
+ http_post('cgi-bin/menu/create', data)
8
+ end
9
+
10
+ end
11
+ end
@@ -1,20 +1,14 @@
1
- require 'multi_xml'
2
- require 'ostruct'
3
- require 'roxml'
4
-
5
1
  module EasyWeixin
6
2
  class Message
7
- attr_accessor :ToUserName, :FromUserName, :CreateTime
8
3
 
9
- def initialize
10
- @ToUserName = ToUserName
11
- @FromUserName = FromUserName
12
- @CreateTime = CreateTime
13
- end
4
+ include Response
5
+
6
+ # attr_accessor :ToUserName, :FromUserName, :CreateTime
14
7
 
15
- def self.factory(xml)
16
- hash = MultiXml.parse(xml)['xml']
17
- source = OpenStruct.new(hash)
8
+ def initialize(to,from,time)
9
+ @ToUserName = to
10
+ @FromUserName = from
11
+ @CreateTime = time
18
12
  end
19
13
 
20
14
  end
@@ -70,4 +64,4 @@ module EasyWeixin
70
64
  end
71
65
 
72
66
 
73
- end
67
+ end
@@ -0,0 +1,32 @@
1
+ module EasyWeixin
2
+ module Response
3
+ extend self
4
+
5
+ # 创建文本信息
6
+ def text_msg(from, to, content)
7
+ msg = TextReplyMessage.new
8
+ msg.ToUserName = to
9
+ msg.FromUserName = from
10
+ msg.Content = content
11
+ msg.to_xml
12
+ end
13
+
14
+ def news_msg(from, to, articles, articleCount=1)
15
+ msg = NewsReplyMessage.new
16
+ msg.ToUserName = to
17
+ msg.FromUserName = from
18
+ unless articles.is_a?(Array)
19
+ articles = [articles]
20
+ end
21
+ msg.Articles = articles
22
+ msg.ArticleCount = articleCount
23
+ msg.to_xml
24
+ end
25
+
26
+ def analysis_xml(request_xml)
27
+ hash = MultiXml.parse(request_xml)['xml']
28
+ source = OpenStruct.new(hash)
29
+ end
30
+
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyWeixin
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,54 @@
1
+ {
2
+ "button": [
3
+ {
4
+ "name": "美女如云",
5
+ "sub_button": [
6
+ {
7
+ "type": "view",
8
+ "name": "性感美女",
9
+ "url": "http://wap.guyifeng.pw/photos?photo_category_id=1"
10
+ },
11
+ {
12
+ "type": "view",
13
+ "name": "小清新",
14
+ "url": "http://wap.guyifeng.pw/photos?photo_category_id=2"
15
+ },
16
+ {
17
+ "type": "view",
18
+ "name": "甜素纯",
19
+ "url": "http://wap.guyifeng.pw/photos?photo_category_id=5"
20
+ },
21
+ {
22
+ "type": "view",
23
+ "name": "清纯",
24
+ "url": "http://wap.guyifeng.pw/photos?photo_category_id=6"
25
+ },
26
+ {
27
+ "type": "view",
28
+ "name": "嫩萝莉",
29
+ "url": "http://wap.guyifeng.pw/photos?photo_category_id=11"
30
+ }
31
+ ]
32
+ },
33
+ {
34
+ "name": "来个人",
35
+ "sub_button": [
36
+ {
37
+ "type": "click",
38
+ "name": "男人",
39
+ "key": "boy"
40
+ },
41
+ {
42
+ "type": "click",
43
+ "name": "女人",
44
+ "key": "girl"
45
+ }
46
+ ]
47
+ },
48
+ {
49
+ "name": "来个笑话",
50
+ "type": "click",
51
+ "key": "joke"
52
+ }
53
+ ]
54
+ }
@@ -0,0 +1,19 @@
1
+ require 'minitest/autorun'
2
+ require 'easy_weixin'
3
+
4
+ describe "EasyWeixin" do
5
+
6
+ before do
7
+ @token = 'dave'
8
+ @timestamp = Time.now.to_i.to_s
9
+ @nonce = '123456'
10
+ @signature = Digest::SHA1.hexdigest( [@token, @timestamp, @nonce].sort.join )
11
+ @echostr = 'caishu'
12
+ end
13
+
14
+ it 'function check_valid' do
15
+ easy_weixin = EasyWeixin::Auth.new(@token, @timestamp, @nonce, @signature, @echostr)
16
+ easy_weixin.check_valid.must_equal @echostr
17
+ end
18
+
19
+ end
@@ -0,0 +1,13 @@
1
+ require 'minitest/autorun'
2
+ require 'easy_weixin'
3
+
4
+ describe 'AccessToken' do
5
+ before do
6
+ @client = EasyWeixin::Api.new('wx61e7c4dba13cc83e', 'bfdffb358e437f3149bbbfed7e7c8ccd')
7
+ end
8
+
9
+ it 'ssss' do
10
+ puts @client.get_access_token
11
+ # @client.get_access_token.must_equal "OHAI!"
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'minitest/autorun'
2
+ require 'easy_weixin'
3
+
4
+ describe 'AccessToken' do
5
+ before do
6
+ @client = EasyWeixin::Api.new('wx61e7c4dba13cc83e', 'bfdffb358e437f3149bbbfed7e7c8ccd')
7
+ end
8
+
9
+ it 'menu' do
10
+ data = File.open(File.expand_path('../menu.json',__FILE__)).read
11
+ puts @client.create_menu(data).body
12
+ end
13
+ end
@@ -0,0 +1,44 @@
1
+ require 'minitest/autorun'
2
+ require 'easy_weixin'
3
+ require File.expand_path('../test_xml',__FILE__)
4
+
5
+ describe "EasyWeixin" do
6
+
7
+ describe "subscribe" do
8
+ it 'subscribe' do
9
+
10
+ msg = EasyWeixin::Response.analysis_xml($subscribe_xml)
11
+
12
+ puts easy_weixin = EasyWeixin::Response.text_msg(msg.ToUserName, msg.FromUserName, 'Hi 欢迎来到顾益峰的个人微信频道,能来到这里的,都是我的朋友,都是一家人,这里你可以很放松!回复joke,听个笑话.回复girl,会显示我的五个女性朋友.回复boy,会显示我的五个男性朋友,回复其他内容,有你意想不到的内容喔~')
13
+ end
14
+ end
15
+
16
+ describe "text" do
17
+ it 'text' do
18
+
19
+ msg = EasyWeixin::Response.analysis_xml($text_xml)
20
+
21
+ puts easy_weixin = EasyWeixin::Response.text_msg(msg.ToUserName, msg.FromUserName, '我只是一个回复')
22
+ end
23
+ end
24
+
25
+ describe "news" do
26
+ it 'news' do
27
+ msg = EasyWeixin::Response.analysis_xml($text_xml)
28
+ if msg.Content == 'Hello2BizUser'
29
+ item1 = EasyWeixin::Item.new
30
+ item1.Title = 'title1'
31
+ item1.Description = 'blah'
32
+ item2 = EasyWeixin::Item.new
33
+ item2.Title = 'title2'
34
+ item2.Description = 'blah blah'
35
+ Articles = [item1, item2]
36
+ ArticleCount = Articles.size
37
+ end
38
+
39
+ puts EasyWeixin::Response.news_msg(msg.ToUserName, msg.FromUserName, Articles, ArticleCount)
40
+ end
41
+ end
42
+
43
+
44
+ end
@@ -14,4 +14,4 @@ $subscribe_xml = '<?xml version="1.0"?>
14
14
  <MsgType><![CDATA[event]]></MsgType>
15
15
  <Event><![CDATA[subscribe]]></Event>
16
16
  <EventKey><![CDATA[]]></EventKey>
17
- </xml>'
17
+ </xml>'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_weixin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - menghuanwd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-18 00:00:00.000000000 Z
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,7 +66,49 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: easy to use weixin.
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: typhoeus
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: create menu and reply message.
70
112
  email:
71
113
  - 651019063@qq.com
72
114
  executables: []
@@ -81,12 +123,19 @@ files:
81
123
  - Rakefile
82
124
  - easy_weixin.gemspec
83
125
  - lib/easy_weixin.rb
126
+ - lib/easy_weixin/api.rb
84
127
  - lib/easy_weixin/auth.rb
85
- - lib/easy_weixin/model.rb
128
+ - lib/easy_weixin/config.rb
129
+ - lib/easy_weixin/menu.rb
130
+ - lib/easy_weixin/message.rb
131
+ - lib/easy_weixin/response.rb
86
132
  - lib/easy_weixin/version.rb
87
- - spec/auth_spec.rb
88
- - spec/reply_spec.rb
89
- - spec/test_xml.rb
133
+ - test/menu.json
134
+ - test/test_auth.rb
135
+ - test/test_get_access_token.rb
136
+ - test/test_menu.rb
137
+ - test/test_response.rb
138
+ - test/test_xml.rb
90
139
  homepage: https://github.com/menghuanwd/easy_weixin
91
140
  licenses:
92
141
  - MIT
@@ -110,8 +159,11 @@ rubyforge_project:
110
159
  rubygems_version: 2.2.2
111
160
  signing_key:
112
161
  specification_version: 4
113
- summary: easy to use weixin.
162
+ summary: easy to use weixin public api.
114
163
  test_files:
115
- - spec/auth_spec.rb
116
- - spec/reply_spec.rb
117
- - spec/test_xml.rb
164
+ - test/menu.json
165
+ - test/test_auth.rb
166
+ - test/test_get_access_token.rb
167
+ - test/test_menu.rb
168
+ - test/test_response.rb
169
+ - test/test_xml.rb
@@ -1,20 +0,0 @@
1
- require 'rspec'
2
- require 'digest/sha1'
3
- require 'easy_weixin/auth'
4
- describe "Weixin::Middleware" do
5
-
6
- before(:all) do
7
- @token = 'dave'
8
- @timestamp = Time.now.to_i.to_s
9
- @nonce = '123456'
10
- @signature = Digest::SHA1.hexdigest( [@token, @timestamp, @nonce].sort.join )
11
- @echostr = 'caishu'
12
- end
13
-
14
-
15
- it 'function check_valid' do
16
- easy_weixin = EasyWeixin::Auth.new(@token, @timestamp, @nonce, @signature, @echostr)
17
- easy_weixin.check_valid.should == @echostr
18
- end
19
-
20
- end
@@ -1,54 +0,0 @@
1
- require 'rspec'
2
- require 'digest/sha1'
3
- require 'easy_weixin/model'
4
- require 'easy_weixin'
5
- require File.expand_path('../test_xml',__FILE__)
6
-
7
- describe "Weixin::Message" do
8
- it 'is a text message' do
9
- puts msg = EasyWeixin.analysis_xml($subscribe_xml)
10
-
11
- case msg.MsgType
12
- when 'text'
13
- # TextMessage.new(hash)
14
- puts EasyWeixin.text_msg(msg.ToUserName, msg.FromUserName, '我只是一个回复')
15
- when 'image'
16
- # ImageMessage.new(hash)
17
- when 'location'
18
- # LocationMessage.new(hash)
19
- when 'link'
20
- # LinkMessage.new(hash)
21
- when 'event'
22
- if msg.Event == "subscribe"
23
- puts EasyWeixin.text_msg(msg.ToUserName, msg.FromUserName, 'Hi 欢迎来到顾益峰的个人微信频道,能来到这里的,都是我的朋友,都是一家人,这里你可以很放松!回复joke,听个笑话.回复girl,会显示我的五个女性朋友.回复boy,会显示我的五个男性朋友,回复其他内容,有你意想不到的内容喔~')
24
- else
25
- puts EasyWeixin.text_msg(msg.ToUserName, msg.FromUserName, '你确定?')
26
- end
27
- when 'voice'
28
- # VoiceMessage.new(hash)
29
- when 'video'
30
- # VideoMessage.new(hash)
31
- else
32
- raise 'Unknown Message'
33
- end
34
-
35
- end
36
-
37
- it 'is a news' do
38
- puts msg = EasyWeixin::Message.factory($text_xml)
39
-
40
- if msg.Content == 'Hello2BizUser'
41
- item1 = EasyWeixin::Item.new
42
- item1.Title = 'title1'
43
- item1.Description = 'blah'
44
- item2 = EasyWeixin::Item.new
45
- item2.Title = 'title2'
46
- item2.Description = 'blah blah'
47
- Articles = [item1, item2]
48
- ArticleCount = Articles.size
49
- end
50
-
51
- puts EasyWeixin.news_msg(msg.ToUserName, msg.FromUserName, Articles, ArticleCount)
52
- end
53
-
54
- end