lhj-tools 0.1.87 → 0.1.88

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
  SHA256:
3
- metadata.gz: 88a12084e737e12b3a0e10a428407cc1e83dfd29a6831709b40bbc330ce2c183
4
- data.tar.gz: fb31614816bf573b70f2fd03fbdab218f8ce215b512e5d63acaf29001a357893
3
+ metadata.gz: add25469f790212700cc67823a7a1709bd5dc530b3d954d0ce33b133e59d8429
4
+ data.tar.gz: 662a20b818f0f57a2d0eee758c8165021cd73fe44b18ad405a63f8a4f3c0fab7
5
5
  SHA512:
6
- metadata.gz: 344983fb4d1b4f9d60bf94f85279a17db67a1e12ee5dd5d20c1efd000d5f37ec472aec6d4a93dbd786d7cb84a033d83a96f02cc612894cf3142f5fa3169c7088
7
- data.tar.gz: 4936c0a4f26d79e879737447ec9c7ca408925b007624a1eb7e78187021ad7069d2b1d9f7dc65130202609356e87a7f4975c41f7826ebea20cb2a6da77c3909b7
6
+ metadata.gz: b7e0e45bbb6c9fb2af985537440ae9de3a34d3beeac5337670814a5ba3f8b1b324c301ee27211d029b9d2abe283d4292a35dfcd89fb80eb799c033242af2b631
7
+ data.tar.gz: 8d47349754781091db87c22003f853373e94a0a0e3a3f2bae932b4e33656d9a21f2fdde54523d140cd270a466cf4705770a5c73a85f3a6b7da8c08fc2ca4c931
@@ -7,41 +7,10 @@ module Lhj
7
7
  self.summary = '更新yapi请求cookie'
8
8
  self.description = '更新 ~/.lhj/yapi.yml 文件配置'
9
9
 
10
- # @return [String]
11
- def interface_url
12
- url = []
13
- url << 'h'
14
- url << 't'
15
- url << 't'
16
- url << 'p'
17
- url << ':'
18
- url << '/'
19
- url << '/'
20
- url << 'y'
21
- url << 'a'
22
- url << 'p'
23
- url << 'i'
24
- url << '.'
25
- url << 'm'
26
- url << 'i'
27
- url << 'g'
28
- url << 'u'
29
- url << 'a'
30
- url << 't'
31
- url << 'e'
32
- url << 'c'
33
- url << 'h'
34
- url << '.'
35
- url << 'c'
36
- url << 'o'
37
- url << 'm'
38
- url.join('')
39
- end
40
-
41
- def request_cookie
10
+ def request_cookie(url)
42
11
  options = Selenium::WebDriver::Chrome::Options.new(args: %w[start-maximized])
43
12
  driver = Selenium::WebDriver.for(:chrome, capabilities: options)
44
- driver.get interface_url
13
+ driver.get url
45
14
  manage = driver.manage
46
15
  manage.timeouts.implicit_wait = 10
47
16
  manage.timeouts.page_load = 10
@@ -51,6 +20,7 @@ module Lhj
51
20
  login_btn&.click
52
21
  # 2.获取cookies
53
22
  wait = Selenium::WebDriver::Wait.new(timeout: 60)
23
+ need_notify = true
54
24
  wait.until do
55
25
  res = {}
56
26
  manage.all_cookies.each do |cookie|
@@ -58,21 +28,60 @@ module Lhj
58
28
  res[:uid] = cookie[:value] if cookie[:name] == '_yapi_uid'
59
29
  res[:token] = cookie[:value] if cookie[:name] == '_yapi_token'
60
30
  end
31
+ if need_notify
32
+ notify_robot(driver.current_url)
33
+ need_notify = false
34
+ end
61
35
  return res if res.values.count == 3
62
36
  end
63
37
  end
64
38
 
39
+ def robot_url
40
+ 'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807'
41
+ end
42
+
43
+ def notify_robot(url)
44
+ template = Lhj::ErbTemplateHelper.load('yapi_qr_code_notify')
45
+ output = Lhj::ErbTemplateHelper.render(template, { url: url }, '-')
46
+ Lhj::Dingtalk.post_message_robot(robot_url, 'yapi qrcode', output)
47
+ end
48
+
65
49
  def config_file
66
50
  File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
67
51
  end
68
52
 
53
+ def user_info_file
54
+ File.join(Lhj::Config.instance.home_dir, 'yapi_user.yml')
55
+ end
56
+
57
+ def load_config(file)
58
+ YAML.load_file(file)
59
+ end
60
+
61
+ def save_user(result)
62
+ user_info = load_config(user_info_file) if File.exist?(user_info_file)
63
+ user_info ||= []
64
+ current = user_info.find { |k| k['_yapi_uid'].eql?(result['_yapi_uid']) }
65
+ if current
66
+ current['__wpkreporterwid_'] = result['__wpkreporterwid_']
67
+ current['_yapi_token'] = result['_yapi_token']
68
+ else
69
+ user_info << result
70
+ end
71
+ file_to_save = File.open(user_info_file, 'w+')
72
+ YAML.dump(user_info, file_to_save)
73
+ file_to_save.close
74
+ end
75
+
69
76
  def handle
70
- cookie = request_cookie
71
- config = YAML.load_file(config_file) if File.exist?(config_file)
77
+ config = load_config(config_file) if File.exist?(config_file)
78
+ # base_url: 'http://yapi.xx.com'
79
+ cookie = request_cookie(config['base_url'])
72
80
  if cookie && config
73
81
  config['__wpkreporterwid_'] = cookie[:wid]
74
82
  config['_yapi_uid'] = cookie[:uid].to_i
75
83
  config['_yapi_token'] = cookie[:token]
84
+ save_user({ '__wpkreporterwid_' => cookie[:wid], '_yapi_uid' => cookie[:uid].to_i, '_yapi_token' => cookie[:token] })
76
85
  file_to_save = File.open(config_file, 'w+')
77
86
  YAML.dump(config, file_to_save)
78
87
  file_to_save.close
@@ -51,8 +51,20 @@ module Lhj
51
51
  end
52
52
 
53
53
  def process(config_file)
54
- load_config(config_file)
55
- res_body = get_interface_api_model
54
+ config = load_config(config_file)
55
+ process_config(config)
56
+ res_body = get_interface_api_model(config)
57
+ unless res_body['errcode'].to_i.zero?
58
+ if File.exist?(user_yml_file)
59
+ user_infos = load_config(user_yml_file)
60
+ user_infos.each do |c|
61
+ res_body = get_interface_api_model(c)
62
+ config = c
63
+ break if res_body['errcode'].to_i.zero?
64
+ end
65
+ save_config(config) if res_body['errcode'].to_i.zero?
66
+ end
67
+ end
56
68
  unless res_body['errcode'].to_i.zero?
57
69
  puts '请执行 lhj yapi-init 初始化配置'.red
58
70
  return
@@ -61,7 +73,7 @@ module Lhj
61
73
 
62
74
  # get project info
63
75
  project_id = res_body['data']['project_id']
64
- project_info = get_project_info(project_id)
76
+ project_info = get_project_info(project_id, config)
65
77
  # 1.print request result
66
78
  res_models = print_res_body_model(res_body)
67
79
  # 2.print request json body
@@ -176,11 +188,15 @@ module Lhj
176
188
  File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
177
189
  end
178
190
 
191
+ def user_yml_file
192
+ File.join(Lhj::Config.instance.home_dir, 'yapi_user.yml')
193
+ end
194
+
179
195
  def load_config(file)
180
- config = YAML.load_file(file)
181
- config.each do |k, v|
182
- @http_headers << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
183
- end
196
+ YAML.load_file(file)
197
+ end
198
+
199
+ def process_config(config)
184
200
  @config_base_url = config['base_url']
185
201
  @config_id = config['id']
186
202
  @config_model_pre = config['model_pre']
@@ -191,6 +207,16 @@ module Lhj
191
207
  @config_property_mapper = config['property_mapper']
192
208
  end
193
209
 
210
+ def save_config(config)
211
+ yml = load_config(yml_file)
212
+ yml['__wpkreporterwid_'] = config['__wpkreporterwid_']
213
+ yml['_yapi_uid'] = config['_yapi_uid']
214
+ yml['_yapi_token'] = config['_yapi_token']
215
+ file_to_save = File.open(yml_file, 'w+')
216
+ YAML.dump(yml, file_to_save)
217
+ file_to_save.close
218
+ end
219
+
194
220
  def api_id
195
221
  @id || @config_id.to_s
196
222
  end
@@ -219,10 +245,14 @@ module Lhj
219
245
  @config_property_mapper || { 'id' => 'gid' }
220
246
  end
221
247
 
222
- def get_interface_api_model
248
+ def get_interface_api_model(config)
223
249
  uri = URI.parse(interface_url_str)
224
250
  req = Net::HTTP::Get.new(uri)
225
- req['Cookie'] = @http_headers.join('; ')
251
+ header_arr = []
252
+ config.each do |k, v|
253
+ header_arr << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
254
+ end
255
+ req['Cookie'] = header_arr.join('; ')
226
256
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
227
257
  http.request(req)
228
258
  end
@@ -232,11 +262,15 @@ module Lhj
232
262
  res_json
233
263
  end
234
264
 
235
- def get_project_info(project_id)
265
+ def get_project_info(project_id, config)
236
266
  url = project_url_str(project_id)
237
267
  uri = URI.parse(url)
238
268
  req = Net::HTTP::Get.new(uri)
239
- req['Cookie'] = @http_headers.join('; ')
269
+ header_arr = []
270
+ config.each do |k, v|
271
+ header_arr << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
272
+ end
273
+ req['Cookie'] = header_arr.join('; ')
240
274
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
241
275
  http.request(req)
242
276
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lhj
4
4
  module Tools
5
- VERSION = "0.1.87"
5
+ VERSION = "0.1.88"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhj-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.87
4
+ version: 0.1.88
5
5
  platform: ruby
6
6
  authors:
7
7
  - lihaijian