Class: XiWechatCorp::API::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/xi_wechat_corp/api/cli.rb

Defined Under Namespace

Classes: FileCache

Instance Method Summary collapse

Instance Method Details

#deep_merge(a, b) ⇒ Object



192
193
194
195
196
197
198
199
200
# File 'lib/xi_wechat_corp/api/cli.rb', line 192

def deep_merge(a, b)
  a.merge(b) do |key, oldval, newval|
    if oldval.is_a?(Hash) && newval.is_a?(Hash)
      deep_merge(oldval, newval)
    else
      newval
    end
  end
end

#establish_connection(options) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/xi_wechat_corp/api/cli.rb', line 49

def establish_connection(options)
  connection_options = {}
  [:access_token, :corp_id, :secret].each do |key|
    connection_options[key] = options[key] if options.key?(key)
  end
  Connection.new(connection_options)
end

#parse(args) ⇒ Object



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

    # Another typical switch to print the version.
    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

#read_json(input) ⇒ Object



185
186
187
188
189
190
# File 'lib/xi_wechat_corp/api/cli.rb', line 185

def read_json(input)
  if input.tty?
    puts 'Input JSON Body Below (Ctrl+D to send request):'
  end
  MultiJson.load(input.read)
end

#run(args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/xi_wechat_corp/api/cli.rb', line 30

def run(args)
  options = verify(parse(args))
  conn = establish_connection(options)
  puts conn.send(options[:method], options[:path], options[:params]).body
rescue MultiJson::ParseError => e
  $stderr.puts 'BAD JSON: ' + e.message
  exit 1
rescue SystemCallError => e
  $stderr.puts 'ERROR: ' + e.message
  exit e.errno
rescue Faraday::ClientError => e
  $stderr.puts 'ERROR: ' + e.message
  $stderr.puts e.response
  exit 2
rescue Faraday::Error => e
  $stderr.puts 'ERROR: ' + e.message
  exit 2
end

#verify(options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xi_wechat_corp/api/cli.rb', line 57

def verify(options)
  if options[:access_token].nil? && (options[:corp_id].nil? || options[:secret].nil?)
    $stderr.puts <<-MSG
Cannot get access token.  Set `--corp-id' and `--secret', or give the token directly
using `--access-token'.
    MSG
    exit 1
  end

  options
end