xi_wechat_corp 1.0.4 → 1.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 +4 -4
- data/bin/xi_wechat_corp_api +5 -0
- data/lib/xi_wechat_corp/api/cli.rb +174 -0
- data/lib/xi_wechat_corp/api/connection.rb +2 -1
- data/lib/xi_wechat_corp/api.rb +6 -2
- data/lib/xi_wechat_corp/version.rb +1 -1
- data/lib/xi_wechat_corp.rb +2 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c1aa27990d8c3bc1126d1bb0834499278e13bf9
|
4
|
+
data.tar.gz: ff85e7794cd9ecb49222cf67026b636e743ac8e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2458e53ca6922f28df0b621b400464c58c6474feec8a033e9041e0b5480282832dc9cab419072e9fabfdc3a33d1f4650b3314341354b949d732eb3003cd51f32
|
7
|
+
data.tar.gz: 09a61afcb3804a7f58721c8c613692ca8324a2cceb9e271546124cb4cd03e17d82ee2bcb9ad4795f86f419f25dc82f764a2deb91f4ed4a09d0008999cb7f4c7b
|
@@ -0,0 +1,174 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'optparse'
|
3
|
+
require 'xi_wechat_corp/version'
|
4
|
+
require 'xi_wechat_corp/api/connection'
|
5
|
+
require 'rack/utils'
|
6
|
+
|
7
|
+
module XiWechatCorp
|
8
|
+
module API
|
9
|
+
class CLI
|
10
|
+
class FileCache
|
11
|
+
def initialize(dir)
|
12
|
+
@dir = dir
|
13
|
+
end
|
14
|
+
|
15
|
+
def read(key)
|
16
|
+
path = File.join(@dir, key)
|
17
|
+
if File.exists?(path)
|
18
|
+
File.read(key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def write(key, value)
|
23
|
+
path = File.join(@dir, key)
|
24
|
+
FileUtils.mkdir_p(File.dirname(path))
|
25
|
+
File.open(path, 'w') { |f| f.write(value) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def run(args)
|
30
|
+
options = verify(parse(args))
|
31
|
+
conn = establish_connection(options)
|
32
|
+
if options[:method] == 'post'
|
33
|
+
if options[:input].tty?
|
34
|
+
puts 'Input JSON Body Below (Ctrl+D to send request):'
|
35
|
+
end
|
36
|
+
puts conn.post(options[:path], options[:input].read).body
|
37
|
+
else
|
38
|
+
puts conn.get(options[:path], options[:params]).body
|
39
|
+
end
|
40
|
+
rescue SystemCallError => e
|
41
|
+
$stderr.puts 'ERROR: ' + e.message
|
42
|
+
exit e.errno
|
43
|
+
rescue Faraday::ClientError => e
|
44
|
+
$stderr.puts 'ERROR: ' + e.message
|
45
|
+
$stderr.puts e.response
|
46
|
+
exit 2
|
47
|
+
rescue Faraday::Error => e
|
48
|
+
$stderr.puts 'ERROR: ' + e.message
|
49
|
+
exit 2
|
50
|
+
end
|
51
|
+
|
52
|
+
def establish_connection(options)
|
53
|
+
connection_options = {}
|
54
|
+
[:access_token, :corp_id, :secret].each do |key|
|
55
|
+
connection_options[key] = options[key] if options.key?(key)
|
56
|
+
end
|
57
|
+
Connection.new(connection_options)
|
58
|
+
end
|
59
|
+
|
60
|
+
def verify(options)
|
61
|
+
if options[:access_token].nil? && (options[:corp_id].nil? || options[:secret].nil?)
|
62
|
+
$stderr.puts <<-MSG
|
63
|
+
Cannot get access token. Set `--corp-id' and `--secret', or give the token directly
|
64
|
+
using `--access-token'.
|
65
|
+
MSG
|
66
|
+
exit 1
|
67
|
+
end
|
68
|
+
|
69
|
+
options
|
70
|
+
end
|
71
|
+
|
72
|
+
def parse(args)
|
73
|
+
options = {
|
74
|
+
corp_id: ENV['XI_WECHAT_CORP_ID'],
|
75
|
+
secret: ENV['XI_WECHAT_CORP_SECRET'],
|
76
|
+
input: $stdin,
|
77
|
+
path: nil,
|
78
|
+
method: 'post',
|
79
|
+
params: {}
|
80
|
+
}
|
81
|
+
|
82
|
+
opt_parser = OptionParser.new do |opts|
|
83
|
+
opts.banner = <<-BANNER
|
84
|
+
Usage: xi_wechat_corp_api [options]
|
85
|
+
|
86
|
+
POST: xi_wechat_corp_api [options] PATH FILE
|
87
|
+
xi_wechat_corp_api [options] PATH JSON
|
88
|
+
GET: xi_wechat_corp_api [options] --get PATH KEY=VALUE [KEY=VALUE]...
|
89
|
+
BANNER
|
90
|
+
|
91
|
+
opts.separator ''
|
92
|
+
opts.separator 'Authentication options:'
|
93
|
+
|
94
|
+
opts.on('-i', '--corp-id CORPID') do |corp_id|
|
95
|
+
options[:corp_id] = corp_id
|
96
|
+
end
|
97
|
+
|
98
|
+
opts.on('-s', '--secret SECRET') do |secret|
|
99
|
+
options[:secret] = secret
|
100
|
+
end
|
101
|
+
|
102
|
+
opts.on('-c', '--cache DIR', 'Cache access token in the directory') do |cache_dir|
|
103
|
+
XiWechatCorp.cache = FileCache.new(cache_dir)
|
104
|
+
end
|
105
|
+
|
106
|
+
opts.on('-a', '--access-token ACCESS_TOKEN', 'Use given access token') do |access_token|
|
107
|
+
options[:access_token] = access_token
|
108
|
+
end
|
109
|
+
|
110
|
+
opts.separator ''
|
111
|
+
opts.separator 'Request options:'
|
112
|
+
|
113
|
+
opts.on('-p', '--post [PATH]', 'Send POST request, it is default') do |path|
|
114
|
+
options[:method] = 'post'
|
115
|
+
options[:path] = path if path
|
116
|
+
end
|
117
|
+
|
118
|
+
opts.on('-g', '--get PATH', 'Send GET request') do |path|
|
119
|
+
options[:method] = 'get'
|
120
|
+
options[:path] = path
|
121
|
+
end
|
122
|
+
|
123
|
+
opts.on('-j', '--json JSON', 'JSON body for POST request') do |json|
|
124
|
+
options[:input] = StringIO.new(json)
|
125
|
+
end
|
126
|
+
|
127
|
+
opts.on('-q', '--query QUERY', 'QUERY string for GET request') do |query|
|
128
|
+
options[:params].merge! Rack::Utils.parse_nested_query(query)
|
129
|
+
end
|
130
|
+
|
131
|
+
opts.on('-f', '--file FILE', 'FILE contains the JSON to be send via POST') do |f|
|
132
|
+
options[:input] = File.open(f) if f != '-'
|
133
|
+
end
|
134
|
+
|
135
|
+
opts.separator ''
|
136
|
+
opts.separator 'Common options:'
|
137
|
+
|
138
|
+
opts.on_tail('-h', '--help', 'Show this message') do
|
139
|
+
puts opts
|
140
|
+
exit
|
141
|
+
end
|
142
|
+
|
143
|
+
# Another typical switch to print the version.
|
144
|
+
opts.on_tail('-v', '--version', 'Show version') do
|
145
|
+
puts VERSION
|
146
|
+
exit
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
opt_parser.parse!(args)
|
151
|
+
return options if args.empty?
|
152
|
+
|
153
|
+
case options[:method]
|
154
|
+
when 'post'
|
155
|
+
if args.size > 2
|
156
|
+
$stderr.puts 'Two many positional arguments'
|
157
|
+
exit 1
|
158
|
+
end
|
159
|
+
options[:path] = args.first if args.size == 2
|
160
|
+
arg = args.last.strip
|
161
|
+
if arg[0] == '{'
|
162
|
+
options[:input] = StringIO.new(args.first)
|
163
|
+
elsif arg != '-'
|
164
|
+
options[:input] = File.open(arg)
|
165
|
+
end
|
166
|
+
when 'get'
|
167
|
+
options[:params].merge! Rack::Utils.parse_nested_query(args.join('&'))
|
168
|
+
end
|
169
|
+
|
170
|
+
options
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
require 'faraday_middleware'
|
3
|
+
require 'xi_wechat_corp'
|
3
4
|
require 'xi_wechat_corp/api/access_token'
|
4
5
|
|
5
6
|
module XiWechatCorp
|
@@ -15,7 +16,7 @@ module XiWechatCorp
|
|
15
16
|
|
16
17
|
class RaiseClientError < Faraday::Response::Middleware
|
17
18
|
def parse(body)
|
18
|
-
raise ClientError.new(body) if body.nil? || body.key?('errcode')
|
19
|
+
raise ClientError.new(body) if body.nil? || (body.key?('errcode') && body['errcode'].to_i != 0)
|
19
20
|
body
|
20
21
|
end
|
21
22
|
end
|
data/lib/xi_wechat_corp/api.rb
CHANGED
data/lib/xi_wechat_corp.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require "xi_wechat_corp/version"
|
2
|
+
require "xi_wechat_corp/api"
|
3
|
+
require "xi_wechat_corp/callback"
|
2
4
|
|
3
5
|
module XiWechatCorp
|
4
|
-
autoload :API, 'xi_wechat_corp/api'
|
5
|
-
autoload :Callback, 'xi_wechat_corp/callback'
|
6
6
|
autoload :SHA1Signer, 'xi_wechat_corp/sha1_signer'
|
7
7
|
autoload :AesCrypt, 'xi_wechat_corp/aes_crypt'
|
8
8
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xi_wechat_corp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian Yang
|
@@ -167,7 +167,8 @@ dependencies:
|
|
167
167
|
description: Wechat Corp development toolkits
|
168
168
|
email:
|
169
169
|
- ian@3pjgames.com
|
170
|
-
executables:
|
170
|
+
executables:
|
171
|
+
- xi_wechat_corp_api
|
171
172
|
extensions: []
|
172
173
|
extra_rdoc_files: []
|
173
174
|
files:
|
@@ -176,10 +177,12 @@ files:
|
|
176
177
|
- LICENSE.txt
|
177
178
|
- README.md
|
178
179
|
- Rakefile
|
180
|
+
- bin/xi_wechat_corp_api
|
179
181
|
- lib/xi_wechat_corp.rb
|
180
182
|
- lib/xi_wechat_corp/aes_crypt.rb
|
181
183
|
- lib/xi_wechat_corp/api.rb
|
182
184
|
- lib/xi_wechat_corp/api/access_token.rb
|
185
|
+
- lib/xi_wechat_corp/api/cli.rb
|
183
186
|
- lib/xi_wechat_corp/api/connection.rb
|
184
187
|
- lib/xi_wechat_corp/callback.rb
|
185
188
|
- lib/xi_wechat_corp/callback/config.rb
|