wechat-api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +9 -0
- data/Guardfile +11 -0
- data/Rakefile +5 -0
- data/lib/wechat-api.rb +1 -0
- data/lib/wechat/api.rb +8 -0
- data/lib/wechat/api/client.rb +124 -0
- data/lib/wechat/api/message.rb +16 -0
- data/lib/wechat/api/user.rb +10 -0
- data/lib/wechat/api/util.rb +32 -0
- data/lib/wechat/api/version.rb +7 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/wechat/api/client_spec.rb +53 -0
- data/wechat-api.gemspec +27 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 15ac3fb9d0cc42e4c68c0ec5735ebecc931fb5e2
|
4
|
+
data.tar.gz: 12e850d08d683026d0fd835210e3a098316fe7bc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40bd671ea3f24e6de3ca06c838fadbb0314f53eaab1fc8b8fbcec3fb5a9439fae97ef2875ac865d08ccdd1d51a9d70fff46ff2fba0faf6db41565dea84ee5192
|
7
|
+
data.tar.gz: ed62e611850637e810d75c21ffdc2ca9a4da40cf17bb79c94286628210dd236bb411b38971557ed9954f8b79028ba90a427d7416bb410a93e4dfbe19d146ec6b
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format doc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/Rakefile
ADDED
data/lib/wechat-api.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'wechat/api'
|
data/lib/wechat/api.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
require 'wechat/api/message'
|
3
|
+
require 'wechat/api/user'
|
4
|
+
require 'wechat/api/util'
|
5
|
+
require 'faraday'
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
module Wechat
|
9
|
+
module Api
|
10
|
+
#
|
11
|
+
class ResponseError < StandardError; end
|
12
|
+
class AccessTokenExpiredError < ResponseError; end
|
13
|
+
#
|
14
|
+
class Client
|
15
|
+
include Message
|
16
|
+
include User
|
17
|
+
include Util
|
18
|
+
|
19
|
+
API_BASE = 'https://api.weixin.qq.com/cgi-bin/'
|
20
|
+
|
21
|
+
attr_reader :app_id, :secret
|
22
|
+
attr_accessor :logger
|
23
|
+
|
24
|
+
def initialize(app_id, secret)
|
25
|
+
@app_id, @secret = app_id, secret
|
26
|
+
@logger = Logger.new(STDOUT)
|
27
|
+
@token_file = File.join('/tmp', "wechat-api-#{app_id}")
|
28
|
+
end
|
29
|
+
|
30
|
+
def access_token
|
31
|
+
@access_token ||= begin
|
32
|
+
token = MultiJson.load(File.read(@token_file))
|
33
|
+
token['access_token']
|
34
|
+
rescue
|
35
|
+
refresh
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def refresh
|
40
|
+
logger.debug { 'refresh token' }
|
41
|
+
url = format('%stoken', API_BASE)
|
42
|
+
resp = connection.get(url, token_params)
|
43
|
+
response = MultiJson.load(resp.body)
|
44
|
+
return handle_error(response) if response['errcode']
|
45
|
+
token = response['access_token']
|
46
|
+
File.open(@token_file, 'w') { |f| f.write(resp.body) } if token
|
47
|
+
token
|
48
|
+
end
|
49
|
+
|
50
|
+
def get(uri, params = {})
|
51
|
+
with_access_token(uri, params) do |url, params_with_token|
|
52
|
+
debug_request do
|
53
|
+
connection.get do |req|
|
54
|
+
req.url url, params_with_token
|
55
|
+
req.headers[:accept] = 'application/json'
|
56
|
+
req.headers[:content_type] = 'application/json'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def post(uri, data = {})
|
63
|
+
with_access_token(uri, {}) do |url, params|
|
64
|
+
debug_request do
|
65
|
+
connection.post do |req|
|
66
|
+
req.url url, params
|
67
|
+
req.headers[:accept] = 'application/json'
|
68
|
+
req.headers[:content_type] = 'application/json'
|
69
|
+
req.body = MultiJson.dump(data)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def with_access_token(uri, params, tried = 2)
|
76
|
+
url = format('%s%s', API_BASE, uri)
|
77
|
+
begin
|
78
|
+
resp = yield(url, params.merge(access_token: access_token))
|
79
|
+
response = MultiJson.load(resp.body)
|
80
|
+
handle_error(response)
|
81
|
+
rescue AccessTokenExpiredError => e
|
82
|
+
refresh
|
83
|
+
retry unless (tried -= 1).zero?
|
84
|
+
raise e
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def debug_request
|
91
|
+
response = yield
|
92
|
+
logger.debug { response }
|
93
|
+
response
|
94
|
+
end
|
95
|
+
|
96
|
+
def handle_error(response)
|
97
|
+
case response['errcode']
|
98
|
+
when 0, nil
|
99
|
+
response
|
100
|
+
when 40_001, 42_001, 40_014
|
101
|
+
fail AccessTokenExpiredError, response
|
102
|
+
else
|
103
|
+
fail ResponseError, response
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def token_params
|
108
|
+
{
|
109
|
+
grant_type: 'client_credential',
|
110
|
+
appid: app_id,
|
111
|
+
secret: secret
|
112
|
+
}
|
113
|
+
end
|
114
|
+
|
115
|
+
def connection
|
116
|
+
@connection ||= begin
|
117
|
+
Faraday.new do |faraday|
|
118
|
+
faraday.adapter Faraday.default_adapter
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Wechat
|
2
|
+
module Api
|
3
|
+
#
|
4
|
+
module Message
|
5
|
+
def send_template(tmp_id, openid, url, data = {})
|
6
|
+
params = {
|
7
|
+
touser: openid,
|
8
|
+
template_id: tmp_id,
|
9
|
+
url: url,
|
10
|
+
data: data
|
11
|
+
}
|
12
|
+
post 'message/template/send', params
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Wechat
|
2
|
+
module Api
|
3
|
+
#
|
4
|
+
module Util
|
5
|
+
def create_qrcode_temp(scene_id, expire_seconds = 60_480)
|
6
|
+
params = {
|
7
|
+
action_name: 'QR_SCENE',
|
8
|
+
expire_seconds: expire_seconds,
|
9
|
+
action_info: {
|
10
|
+
scene: {
|
11
|
+
scene_id: scene_id
|
12
|
+
}
|
13
|
+
}
|
14
|
+
}
|
15
|
+
post 'qrcode/create', params
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_qrcode(scene_str)
|
19
|
+
params = {
|
20
|
+
action_name: 'QR_LIMIT_STR_SCENE',
|
21
|
+
action_info: {
|
22
|
+
scene: {
|
23
|
+
scene_str: scene_str
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
post 'qrcode/create', params
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require "bundler"
|
5
|
+
require 'bundler/setup'
|
6
|
+
|
7
|
+
Bundler.require
|
8
|
+
|
9
|
+
require 'rspec/its'
|
10
|
+
|
11
|
+
require "faraday"
|
12
|
+
require "multi_json"
|
13
|
+
|
14
|
+
require 'webmock/rspec'
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
RSpec.describe Wechat::Api::Client do
|
4
|
+
subject { described_class.new('APPID', 'APPSECRET') }
|
5
|
+
|
6
|
+
before do
|
7
|
+
subject.logger.level = Logger::DEBUG
|
8
|
+
end
|
9
|
+
|
10
|
+
it :refresh do
|
11
|
+
token_body = '{"access_token":"ACCESS_TOKEN","expires_in":7200}'
|
12
|
+
stub_request(:get, /token/).to_return(body: token_body)
|
13
|
+
expect(subject.refresh).not_to be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it :refresh_error do
|
17
|
+
token_body = '{"errcode": 40013}'
|
18
|
+
stub_request(:get, /token/).to_return(body: token_body)
|
19
|
+
expect { subject.refresh }.to raise_error(Wechat::Api::ResponseError)
|
20
|
+
end
|
21
|
+
|
22
|
+
it :refresh_token_error do
|
23
|
+
token_body = '{"errcode": 40001}'
|
24
|
+
stub_request(:get, /token/).to_return(body: token_body)
|
25
|
+
expect { subject.refresh }
|
26
|
+
.to raise_error(Wechat::Api::AccessTokenExpiredError)
|
27
|
+
end
|
28
|
+
|
29
|
+
it :access_token do
|
30
|
+
token_body = '{"access_token":"ACCESS_TOKEN","expires_in":7200}'
|
31
|
+
expect(File).to receive(:read).and_return(token_body)
|
32
|
+
expect(subject.access_token).to eq('ACCESS_TOKEN')
|
33
|
+
end
|
34
|
+
|
35
|
+
it :get_twice do
|
36
|
+
expect(subject).to receive(:access_token).twice.and_return('Token')
|
37
|
+
expect(subject).to receive(:refresh).and_return('Token')
|
38
|
+
stub_request(:get, /some/)
|
39
|
+
.with(query: { access_token: 'Token' })
|
40
|
+
.to_return(body: '{"errcode": 40001}').then
|
41
|
+
.to_return(body: '{}')
|
42
|
+
subject.get 'some'
|
43
|
+
end
|
44
|
+
|
45
|
+
it :post do
|
46
|
+
expect(subject).to receive(:access_token).and_return('Token')
|
47
|
+
stub_request(:post, /some/)
|
48
|
+
.with(query: { access_token: 'Token' })
|
49
|
+
.to_return(body: '{}')
|
50
|
+
|
51
|
+
subject.post 'some', '{}'
|
52
|
+
end
|
53
|
+
end
|
data/wechat-api.gemspec
ADDED
@@ -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/api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wechat-api"
|
8
|
+
spec.version = Wechat::Api::VERSION
|
9
|
+
spec.authors = ["Ryan Wong"]
|
10
|
+
spec.email = ["lazing@gmail.com"]
|
11
|
+
spec.summary = %q{Wechat API wrapper}
|
12
|
+
spec.homepage = "https://github.com/lazing/wechat-api"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency "faraday", "~> 0.9"
|
21
|
+
spec.add_runtime_dependency "multi_json", "~> 1.2"
|
22
|
+
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
spec.add_development_dependency "webmock", "~> 1.18"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wechat-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Wong
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: multi_json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
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: rake
|
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: pry
|
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: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: webmock
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.18'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.18'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- lazing@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- Guardfile
|
123
|
+
- Rakefile
|
124
|
+
- lib/wechat-api.rb
|
125
|
+
- lib/wechat/api.rb
|
126
|
+
- lib/wechat/api/client.rb
|
127
|
+
- lib/wechat/api/message.rb
|
128
|
+
- lib/wechat/api/user.rb
|
129
|
+
- lib/wechat/api/util.rb
|
130
|
+
- lib/wechat/api/version.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/wechat/api/client_spec.rb
|
133
|
+
- wechat-api.gemspec
|
134
|
+
homepage: https://github.com/lazing/wechat-api
|
135
|
+
licenses:
|
136
|
+
- MIT
|
137
|
+
metadata: {}
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.2.2
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Wechat API wrapper
|
158
|
+
test_files:
|
159
|
+
- spec/spec_helper.rb
|
160
|
+
- spec/wechat/api/client_spec.rb
|
161
|
+
has_rdoc:
|