wechat-adapter 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/.coveralls.yml +1 -0
- data/.gitignore +22 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +8 -0
- data/lib/wechat/adapter/version.rb +5 -0
- data/lib/wechat/adapter/wechatapi.rb +70 -0
- data/lib/wechat/adapter.rb +8 -0
- data/spec/adapter_spec.rb +111 -0
- data/spec/spec_helper.rb +1 -0
- data/wechat-adapter.gemspec +27 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dbe7b615dc12a98bdf144643ffd9a7f5178b7f10
|
4
|
+
data.tar.gz: 827227a345b0ee939f99be0044a0e987d88d087f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 334bdfb49fb251899d547ff59f2e18327bab65ed0ab0f2aa191fc6420be6d0223a2e56e31248583de9b7cc399d3b84feb1dc4a08b2a887f87abc1bb1d1746047
|
7
|
+
data.tar.gz: 6291907a4c85f9d3c8201b7fccb9fafda56ba08634d2787e036b859d88be837d6d635656bb72b5a209ef8ac51ed7fee0c6474e341f96872021e99da99ef22363
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Lu, Jun
|
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,41 @@
|
|
1
|
+
# Wechat::Adapter
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/luj1985/wechat-adapter.svg?branch=master)](https://travis-ci.org/luj1985/wechat-adapter)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/wechat-adapter.svg)](http://badge.fury.io/rb/wechat-adapter)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/luj1985/wechat-adapter/badge.png)](https://coveralls.io/r/luj1985/wechat-adapter)
|
6
|
+
|
7
|
+
A wrapper for Tencent Wechat JSON API, currently only "menu" operation are tested, others should work also.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'wechat-adapter'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install wechat-adapter
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'wechat/adapter'
|
27
|
+
|
28
|
+
appid = "wxb3fae34df644fbf7"
|
29
|
+
appsecret = "d0aae6a89949c8c789068397f9e1c59c"
|
30
|
+
api = WechatAPI.new(appid, appsecret)
|
31
|
+
|
32
|
+
api.get 'menu/get'
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it ( https://github.com/[my-github-username]/wechat-adapter/fork )
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'restclient'
|
2
|
+
|
3
|
+
module Wechat
|
4
|
+
module Adapter
|
5
|
+
class TokenExpiredError < StandardError; end
|
6
|
+
|
7
|
+
class ResponseError < StandardError
|
8
|
+
attr_reader :error_code
|
9
|
+
def initialize(code, message)
|
10
|
+
error_code = code
|
11
|
+
super "#{code} (#{message})"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class WechatAPI
|
16
|
+
def initialize appid, appsecret, base = 'https://api.weixin.qq.com/cgi-bin'
|
17
|
+
@appid, @appsecret, @base = appid, appsecret, base
|
18
|
+
end
|
19
|
+
|
20
|
+
def handle_err_response values
|
21
|
+
code = values["errcode"]
|
22
|
+
return if code == 0
|
23
|
+
raise TokenExpiredError if [42001, 40014].include? code
|
24
|
+
raise ResponseError.new(code, values['errmsg'])
|
25
|
+
end
|
26
|
+
|
27
|
+
def raw_json_request method, path, params = {}, payload = nil
|
28
|
+
params[:access_token] = @access_token if @access_token
|
29
|
+
url = "#{@base}/#{path}"
|
30
|
+
|
31
|
+
response = ::RestClient::Request.execute(
|
32
|
+
:method => method,
|
33
|
+
:url => url,
|
34
|
+
:headers => { :params => params },
|
35
|
+
:payload => (payload.to_json if payload),
|
36
|
+
:content_type => :json)
|
37
|
+
|
38
|
+
values = JSON.parse(response.body)
|
39
|
+
values["errcode"] ? handle_err_response(values) : values
|
40
|
+
end
|
41
|
+
|
42
|
+
def json_request *args, &block
|
43
|
+
begin
|
44
|
+
aquire_access_token unless @access_token
|
45
|
+
response = raw_json_request *args, &block
|
46
|
+
rescue TokenExpiredError
|
47
|
+
aquire_access_token
|
48
|
+
retry
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def aquire_access_token
|
53
|
+
values = raw_json_request :get, 'token', {
|
54
|
+
:grant_type => 'client_credential',
|
55
|
+
:appid => @appid,
|
56
|
+
:secret => @appsecret
|
57
|
+
}
|
58
|
+
@access_token = values["access_token"]
|
59
|
+
end
|
60
|
+
|
61
|
+
def post path, options = {}, payload = nil
|
62
|
+
json_request(:post, path, options, payload)
|
63
|
+
end
|
64
|
+
|
65
|
+
def get path, options = {}
|
66
|
+
json_request(:get, path, options)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Wechat::Adapter do
|
4
|
+
access_token = "jDBx_Q_ClVPDCxgcqdzVdf4GqiWlW3xhxphm2VIszvpsPKVZextLULiZKWX5OMAozhW6kxvBB7Z7DKiv5qRF6A"
|
5
|
+
appid = "wxb3fae34df644fbf7"
|
6
|
+
appsecret = "d0aae6a89949c8c789068397f9e1c59c"
|
7
|
+
base_url = "https://api.weixin.qq.com/cgi-bin"
|
8
|
+
wechat_menu = {
|
9
|
+
:button => [
|
10
|
+
{ :type => "click", :name => "今日歌曲", :key => "V1001_TODAY_MUSIC", :sub_button => [] },
|
11
|
+
{ :type => "click", :name => "歌手简介", :key => "V1001_TODAY_SINGER", :sub_button => [] },
|
12
|
+
{ :name => "菜单", :sub_button => [
|
13
|
+
{ :type => "view", :name => "搜索", :url => "http://www.soso.com/", :sub_button => [] },
|
14
|
+
{ :type => "view", :name => "视频", :url => "http://v.qq.com/", :sub_button => [] },
|
15
|
+
{ :type => "click", :name => "赞一下我们", :key => "V1001_GOOD", :sub_button => []
|
16
|
+
}]
|
17
|
+
}]
|
18
|
+
}
|
19
|
+
|
20
|
+
let (:client) { RestClient::Request = double }
|
21
|
+
let (:app) { Wechat::Adapter::WechatAPI.new(appid, appsecret, base_url)}
|
22
|
+
|
23
|
+
it "should be able to get access token" do
|
24
|
+
|
25
|
+
access_token_response = double
|
26
|
+
allow(access_token_response).to receive(:body).and_return({ :access_token => access_token, :expires_in => 7200}.to_json)
|
27
|
+
allow(client).to receive(:execute) { access_token_response }
|
28
|
+
expect(app.aquire_access_token).to eq(access_token)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be able to get menu token" do
|
32
|
+
access_token_response = double
|
33
|
+
allow(access_token_response).to receive(:body).and_return({:access_token => access_token, :expires_in => 7200}.to_json)
|
34
|
+
|
35
|
+
menu_get_response = double
|
36
|
+
allow(menu_get_response).to receive(:body).and_return({:menu => wechat_menu}.to_json)
|
37
|
+
|
38
|
+
access_token_retrieve_method = double
|
39
|
+
expect(access_token_retrieve_method).to receive(:invoke).once
|
40
|
+
|
41
|
+
menu_retrieve_method = double
|
42
|
+
expect(menu_retrieve_method).to receive(:invoke).exactly(10).times
|
43
|
+
|
44
|
+
allow(client).to receive(:execute) do |args|
|
45
|
+
case args[:url]
|
46
|
+
when "https://api.weixin.qq.com/cgi-bin/token" then
|
47
|
+
expect(args[:method]).to eq(:get)
|
48
|
+
params = args[:headers][:params]
|
49
|
+
expect(params[:grant_type]).to eq("client_credential")
|
50
|
+
expect(params[:appid]).to eq(appid)
|
51
|
+
expect(params[:secret]).to eq(appsecret)
|
52
|
+
access_token_retrieve_method.send(:invoke)
|
53
|
+
access_token_response
|
54
|
+
when "https://api.weixin.qq.com/cgi-bin/menu/get" then
|
55
|
+
expect(args[:method]).to eq(:get)
|
56
|
+
params = args[:headers][:params]
|
57
|
+
expect(params[:access_token]).to eq(access_token)
|
58
|
+
menu_retrieve_method.send(:invoke)
|
59
|
+
menu_get_response
|
60
|
+
else fail
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
10.times {
|
65
|
+
app.get 'menu/get'
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should be able to delete menu" do
|
70
|
+
access_token_response = double
|
71
|
+
allow(access_token_response).to receive(:body).and_return({:access_token => access_token, :expires_in => 7200}.to_json)
|
72
|
+
|
73
|
+
menu_delete_response = double
|
74
|
+
allow(menu_delete_response).to receive(:body).and_return({:errcode => 0, :errmsg => "ok"}.to_json)
|
75
|
+
|
76
|
+
allow(client).to receive(:execute) do |args|
|
77
|
+
case args[:url]
|
78
|
+
when "https://api.weixin.qq.com/cgi-bin/token" then access_token_response
|
79
|
+
when "https://api.weixin.qq.com/cgi-bin/menu/delete" then
|
80
|
+
expect(args[:method]).to eq(:get)
|
81
|
+
params = args[:headers][:params]
|
82
|
+
expect(params[:access_token]).to eq(access_token)
|
83
|
+
menu_delete_response
|
84
|
+
else fail
|
85
|
+
end
|
86
|
+
end
|
87
|
+
app.get 'menu/delete'
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should be able to create menu" do
|
91
|
+
access_token_response = double
|
92
|
+
allow(access_token_response).to receive(:body).and_return({:access_token => access_token, :expires_in => 7200}.to_json)
|
93
|
+
|
94
|
+
menu_create_response = double
|
95
|
+
allow(menu_create_response).to receive(:body).and_return({:errcode => 0, :errmsg => "ok"}.to_json)
|
96
|
+
|
97
|
+
allow(client).to receive(:execute) do |args|
|
98
|
+
case args[:url]
|
99
|
+
when "https://api.weixin.qq.com/cgi-bin/token" then access_token_response
|
100
|
+
when "https://api.weixin.qq.com/cgi-bin/menu/create" then
|
101
|
+
expect(args[:method]).to eq(:post)
|
102
|
+
params = args[:headers][:params]
|
103
|
+
expect(params[:access_token]).to eq(access_token)
|
104
|
+
expect(args[:payload]).to eq(wechat_menu.to_json)
|
105
|
+
menu_create_response
|
106
|
+
else fail
|
107
|
+
end
|
108
|
+
end
|
109
|
+
app.post 'menu/create', {}, wechat_menu
|
110
|
+
end
|
111
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'wechat/adapter'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'wechat/adapter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wechat-adapter"
|
8
|
+
spec.version = Wechat::Adapter::VERSION
|
9
|
+
spec.authors = ["Lu, Jun"]
|
10
|
+
spec.email = ["luj1985@gmail.com"]
|
11
|
+
spec.summary = "Communicate with Tencent wechat API"
|
12
|
+
spec.description = "Use HTTP protocol to communicate with wechat API"
|
13
|
+
spec.homepage = "http://github.com/luj1985/wechat-adapter"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_dependency "rest-client"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "rspec-mocks"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wechat-adapter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lu, Jun
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-mocks
|
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
|
+
description: Use HTTP protocol to communicate with wechat API
|
84
|
+
email:
|
85
|
+
- luj1985@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".coveralls.yml"
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/wechat/adapter.rb
|
98
|
+
- lib/wechat/adapter/version.rb
|
99
|
+
- lib/wechat/adapter/wechatapi.rb
|
100
|
+
- spec/adapter_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- wechat-adapter.gemspec
|
103
|
+
homepage: http://github.com/luj1985/wechat-adapter
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.2.2
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Communicate with Tencent wechat API
|
127
|
+
test_files:
|
128
|
+
- spec/adapter_spec.rb
|
129
|
+
- spec/spec_helper.rb
|