69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/xi_wechat_corp/api/cli.rb', line 69
def parse(args)
options = {
corp_id: ENV['XI_WECHAT_CORP_ID'],
secret: ENV['XI_WECHAT_CORP_SECRET'],
path: nil,
method: 'post',
json: [],
query: []
}
opt_parser = OptionParser.new do |opts|
opts.banner = <<-BANNER
Usage: xi_wechat_corp_api [options]
POST: xi_wechat_corp_api [options] PATH FILE
xi_wechat_corp_api [options] PATH KEY=VALUE ['NEST[KEY]'=VALUE]...
xi_wechat_corp_api [options] PATH JSON
GET: xi_wechat_corp_api [options] --get PATH KEY=VALUE [KEY=VALUE]...
BANNER
opts.separator ''
opts.separator 'Authentication options:'
opts.on('-i', '--corp-id CORPID') do |corp_id|
options[:corp_id] = corp_id
end
opts.on('-s', '--secret SECRET') do |secret|
options[:secret] = secret
end
opts.on('-c', '--cache DIR', 'Cache access token in the directory') do |cache_dir|
XiWechatCorp.cache = FileCache.new(cache_dir)
end
opts.on('-a', '--access-token ACCESS_TOKEN', 'Use given access token') do |access_token|
options[:access_token] = access_token
end
opts.separator ''
opts.separator 'Request options:'
opts.on('-p', '--post [PATH]', 'Send POST request, it is default') do |path|
options[:method] = 'post'
options[:path] = path if path
end
opts.on('-g', '--get [PATH]', 'Send GET request') do |path|
options[:method] = 'get'
options[:path] = path if path
end
opts.on('-j', '--json JSON', 'JSON body for POST request') do |json|
options[:json] << StringIO.new(json)
end
opts.on('-q', '--query QUERY', 'QUERY string such as a=10&b[nested]=10, it will be converted to JSON for POST') do |query|
options[:query] << query.strip if query && query.strip.size > 0
end
opts.on('-f', '--file FILE', 'FILE contains the JSON to be send via POST') do |f|
options[:json] << (f != '-' ? File.open(f) : $stdin)
end
opts.on('-t', '--text CONTENT', 'Set text[content] for text message') do |content|
options[:query] << "text[content]=#{content.gsub('%', '%25').gsub('&', '%26')}"
end
opts.separator ''
opts.separator 'Common options:'
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
opts.on_tail('-v', '--version', 'Show version') do
puts VERSION
exit
end
end
opt_parser.parse!(args)
return options if args.empty?
if options[:path].nil?
options[:path] = args.shift
end
args.each do |arg|
arg = arg.strip
if arg[0] == '{'
options[:json] << StringIO.new(arg)
elsif arg == '-'
options[:json] << $stdin
elsif !File.exists?(arg) && arg.include?('=')
options[:query] << arg
else
options[:json] << File.open(arg)
end
end
if options[:query].empty?
params = {}
else
params = Rack::Utils.parse_nested_query(options[:query].join('&'))
end
options[:json].each do |input|
params = deep_merge(params, read_json(input))
end
options.merge(params: params)
end
|