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

#establish_connection(options) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/xi_wechat_corp/api/cli.rb', line 52

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



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
# File 'lib/xi_wechat_corp/api/cli.rb', line 72

def parse(args)
  options = {
    corp_id: ENV['XI_WECHAT_CORP_ID'],
    secret: ENV['XI_WECHAT_CORP_SECRET'],
    input: $stdin,
    path: nil,
    method: 'post',
    params: {}
  }

  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 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
    end

    opts.on('-j', '--json JSON', 'JSON body for POST request') do |json|
      options[:input] = StringIO.new(json)
    end

    opts.on('-q', '--query QUERY', 'QUERY string for GET request') do |query|
      options[:params].merge! Rack::Utils.parse_nested_query(query)
    end

    opts.on('-f', '--file FILE', 'FILE contains the JSON to be send via POST') do |f|
      options[:input] = File.open(f) if f != '-'
    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?

  case options[:method]
  when 'post'
    if args.size > 2
      $stderr.puts 'Two many positional arguments'
      exit 1
    end
    options[:path] = args.first if args.size == 2
    arg = args.last.strip
    if arg[0] == '{'
      options[:input] = StringIO.new(args.first)
    elsif arg != '-'
      options[:input] = File.open(arg)
    end
  when 'get'
    options[:params].merge! Rack::Utils.parse_nested_query(args.join('&'))
  end

  options
end

#run(args) ⇒ Object



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

def run(args)
  options = verify(parse(args))
  conn = establish_connection(options)
  if options[:method] == 'post'
    if options[:input].tty?
      puts 'Input JSON Body Below (Ctrl+D to send request):'
    end
    puts conn.post(options[:path], options[:input].read).body
  else
    puts conn.get(options[:path], options[:params]).body
  end
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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/xi_wechat_corp/api/cli.rb', line 60

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