xi_wechat_corp 1.2.2 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e816780236b5dc5a5fe6626e42f122339718979b
4
- data.tar.gz: f2a650a4f354967c9e0263352525cc1b5e658363
3
+ metadata.gz: beaff6e4d5ef6eaa878a785b55d4fb0741f85bb5
4
+ data.tar.gz: 635c19daf807474e610d21e0cb4209636ff0e47c
5
5
  SHA512:
6
- metadata.gz: b1aa2f0f400e1cdb262519fb5c7f228ead12211ac9c78a69c7f9b0f605a853898a24d2232f61c6e61ea788be25d6949f62972a699cb9ee6e22166c1c69537bb1
7
- data.tar.gz: 96dde77fd40f2a4405d847be95a33722bc8cfb0b212495a3b9dcb41e9f1e08c0c3b6873238c0f4b268b3be347c2622066b11a967110c30da7a81accff99f6f3e
6
+ metadata.gz: 502dc58825e9407ad5a6adfeefefd3a052ac8fd9bad969ed966f22b7289e1a348f0ffba990cf5660f0c235c92d5dc5284cc43b5d60a8110660471c06f0f9dbdb
7
+ data.tar.gz: d2eeb6f2e12b3b1ea658dfc58de0245b7297674fc0c817c4333cd7f491b4ea76e3ea448ca315e90ed4feb6bb8e33130d3737e1eebdcad6a1e75697b5a784b720
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'xi_wechat_corp/api/cli'
4
+
5
+ puts 'Input JSON Body Below (Ctrl+D to send request):' if $stdin.tty?
6
+ text = $stdin.read
7
+ ARGV.unshift text
8
+ ARGV.unshift '--text'
9
+
10
+ XiWechatCorp::API::CLI.new.run(ARGV)
@@ -3,6 +3,7 @@ require 'optparse'
3
3
  require 'xi_wechat_corp/version'
4
4
  require 'xi_wechat_corp/api/connection'
5
5
  require 'rack/utils'
6
+ require 'multi_json'
6
7
 
7
8
  module XiWechatCorp
8
9
  module API
@@ -29,14 +30,10 @@ module XiWechatCorp
29
30
  def run(args)
30
31
  options = verify(parse(args))
31
32
  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
33
+ puts MultiJson.load(conn.send(options[:method], options[:path], options[:params]).body).inspect
34
+ rescue MultiJson::ParseError => e
35
+ $stderr.puts 'BAD JSON: ' + e.message
36
+ exit 1
40
37
  rescue SystemCallError => e
41
38
  $stderr.puts 'ERROR: ' + e.message
42
39
  exit e.errno
@@ -73,17 +70,18 @@ using `--access-token'.
73
70
  options = {
74
71
  corp_id: ENV['XI_WECHAT_CORP_ID'],
75
72
  secret: ENV['XI_WECHAT_CORP_SECRET'],
76
- input: $stdin,
77
73
  path: nil,
78
74
  method: 'post',
79
- params: {}
80
- }
75
+ json: [],
76
+ query: []
77
+ }
81
78
 
82
79
  opt_parser = OptionParser.new do |opts|
83
80
  opts.banner = <<-BANNER
84
81
  Usage: xi_wechat_corp_api [options]
85
82
 
86
83
  POST: xi_wechat_corp_api [options] PATH FILE
84
+ xi_wechat_corp_api [options] PATH KEY=VALUE ['NEST[KEY]'=VALUE]...
87
85
  xi_wechat_corp_api [options] PATH JSON
88
86
  GET: xi_wechat_corp_api [options] --get PATH KEY=VALUE [KEY=VALUE]...
89
87
  BANNER
@@ -115,21 +113,25 @@ Usage: xi_wechat_corp_api [options]
115
113
  options[:path] = path if path
116
114
  end
117
115
 
118
- opts.on('-g', '--get PATH', 'Send GET request') do |path|
116
+ opts.on('-g', '--get [PATH]', 'Send GET request') do |path|
119
117
  options[:method] = 'get'
120
- options[:path] = path
118
+ options[:path] = path if path
121
119
  end
122
120
 
123
121
  opts.on('-j', '--json JSON', 'JSON body for POST request') do |json|
124
- options[:input] = StringIO.new(json)
122
+ options[:json] << StringIO.new(json)
125
123
  end
126
124
 
127
- opts.on('-q', '--query QUERY', 'QUERY string for GET request') do |query|
128
- options[:params].merge! Rack::Utils.parse_nested_query(query)
125
+ opts.on('-q', '--query QUERY', 'QUERY string such as a=10&b[nested]=10, it will be converted to JSON for POST') do |query|
126
+ options[:query] << query.strip if query && query.strip.size > 0
129
127
  end
130
128
 
131
129
  opts.on('-f', '--file FILE', 'FILE contains the JSON to be send via POST') do |f|
132
- options[:input] = File.open(f) if f != '-'
130
+ options[:json] << (f != '-' ? File.open(f) : $stdin)
131
+ end
132
+
133
+ opts.on('-t', '--text CONTENT', 'Set text[content] for text message') do |content|
134
+ options[:query] << "text[content]=#{content.gsub('%', '%25').gsub('&', '%26')}"
133
135
  end
134
136
 
135
137
  opts.separator ''
@@ -150,24 +152,51 @@ Usage: xi_wechat_corp_api [options]
150
152
  opt_parser.parse!(args)
151
153
  return options if args.empty?
152
154
 
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
155
+ if options[:path].nil?
156
+ options[:path] = args.shift
157
+ end
158
+
159
+ args.each do |arg|
160
+ arg = arg.strip
161
161
  if arg[0] == '{'
162
- options[:input] = StringIO.new(args.first)
163
- elsif arg != '-'
164
- options[:input] = File.open(arg)
162
+ options[:json] << StringIO.new(arg)
163
+ elsif arg == '-'
164
+ options[:json] << $stdin
165
+ elsif !File.exists?(arg) && arg.include?('=')
166
+ options[:query] << arg
167
+ else
168
+ options[:json] << File.open(arg)
165
169
  end
166
- when 'get'
167
- options[:params].merge! Rack::Utils.parse_nested_query(args.join('&'))
168
170
  end
169
171
 
170
- options
172
+ if options[:query].empty?
173
+ params = {}
174
+ else
175
+ params = Rack::Utils.parse_nested_query(options[:query].join('&'))
176
+ end
177
+
178
+ options[:json].each do |input|
179
+ params = deep_merge(params, read_json(input))
180
+ end
181
+
182
+ options.merge(params: params)
183
+ end
184
+
185
+ def read_json(input)
186
+ if input.tty?
187
+ puts 'Input JSON Body Below (Ctrl+D to send request):'
188
+ end
189
+ MultiJson.load(input.read)
190
+ end
191
+
192
+ def deep_merge(a, b)
193
+ a.merge(b) do |key, oldval, newval|
194
+ if oldval.is_a?(Hash) && newval.is_a?(Hash)
195
+ deep_merge(oldval, newval)
196
+ else
197
+ newval
198
+ end
199
+ end
171
200
  end
172
201
  end
173
202
  end
@@ -1,3 +1,3 @@
1
1
  module XiWechatCorp
2
- VERSION = '1.2.2'
2
+ VERSION = '1.2.4'
3
3
  end
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.2.2
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Yang
@@ -169,6 +169,7 @@ email:
169
169
  - ian@3pjgames.com
170
170
  executables:
171
171
  - xi_wechat_corp_api
172
+ - xi_wechat_corp_send_text
172
173
  extensions: []
173
174
  extra_rdoc_files: []
174
175
  files:
@@ -178,6 +179,7 @@ files:
178
179
  - README.md
179
180
  - Rakefile
180
181
  - bin/xi_wechat_corp_api
182
+ - bin/xi_wechat_corp_send_text
181
183
  - lib/xi_wechat_corp.rb
182
184
  - lib/xi_wechat_corp/aes_crypt.rb
183
185
  - lib/xi_wechat_corp/api.rb