Class: Lhj::Command::YapiInit

Inherits:
Lhj::Command
  • Object
show all
Defined in:
lib/lhj/command/yapi/yapi_init.rb

Overview

generate model from yapi

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #begin_title, #initialize, #run, #stop

Constructor Details

This class inherits a constructor from Lhj::Command

Instance Method Details

#config_fileObject



66
67
68
# File 'lib/lhj/command/yapi/yapi_init.rb', line 66

def config_file
  File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
end

#generate_callback_param(url) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lhj/command/yapi/yapi_init.rb', line 44

def generate_callback_param(url)
  template = Lhj::ErbTemplateHelper.load('yapi_qr_code_notify')
  output = Lhj::ErbTemplateHelper.render(template, { url: url }, nil)
  body = Lhj::Dingtalk.http_body_message('yapi qrcode', output)
  {
    url: 'https://oapi.dingtalk.com/robot/send',
    query: { access_token: 'fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807' },
    body: body,
    content_type: 'application/json'
  }
end

#handleObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lhj/command/yapi/yapi_init.rb', line 93

def handle
  time = Time.now
  qr_code_name = "#{time.strftime('%Y%m%d%H%M%S')}.png"
  config = load_config(config_file) if File.exist?(config_file)
  # base_url: 'http://yapi.xx.com'
  cookie = request_cookie(config['base_url'], qr_code_name)
  if cookie && config
    config['__wpkreporterwid_'] = cookie[:wid]
    config['_yapi_uid'] = cookie[:uid].to_i
    config['_yapi_token'] = cookie[:token]
    # save user info
    save_user({ '__wpkreporterwid_' => cookie[:wid], '_yapi_uid' => cookie[:uid].to_i, '_yapi_token' => cookie[:token] })
    # save config
    file_to_save = File.open(config_file, 'w+')
    YAML.dump(config, file_to_save)
    file_to_save.close
    # remove temp file
    qr_code_image = File.join(Lhj::Config.instance.home_dir, qr_code_name)
    FileUtils.rm qr_code_image if File.exist?(qr_code_image)
    # remove oss
    Lhj::OSS::Helper.instance.delete(qr_code_name)
  end
end

#load_config(file) ⇒ Object



74
75
76
# File 'lib/lhj/command/yapi/yapi_init.rb', line 74

def load_config(file)
  YAML.load_file(file)
end

#notify_robot(url) ⇒ Object



60
61
62
63
64
# File 'lib/lhj/command/yapi/yapi_init.rb', line 60

def notify_robot(url)
  template = Lhj::ErbTemplateHelper.load('yapi_qr_code_notify')
  output = Lhj::ErbTemplateHelper.render(template, { url: url }, nil)
  Lhj::Dingtalk.post_message_robot(robot_url, 'yapi qrcode code', output)
end


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lhj/command/yapi/yapi_init.rb', line 10

def request_cookie(url, qr_code_key)
  options = Selenium::WebDriver::Chrome::Options.new(args: %w[start-maximized])
  driver = Selenium::WebDriver.for(:chrome, capabilities: options)
  driver.get url
  manage = driver.manage
  window = manage.window
  manage.timeouts.implicit_wait = 10
  manage.timeouts.page_load = 10
  # 1.登录
  all_btn = driver.all(tag_name: 'button')
   = all_btn.find { |ele| ele.text =~ /钉钉登录/ } if all_btn
  &.click
  window.resize_to(200, 500)
  # 2.获取cookies
  wait = Selenium::WebDriver::Wait.new(timeout: 120)
  need_notify = true
  wait.until do
    res = {}
    manage.all_cookies.each do |cookie|
      res[:wid] = cookie[:value] if cookie[:name] == '__wpkreporterwid_'
      res[:uid] = cookie[:value] if cookie[:name] == '_yapi_uid'
      res[:token] = cookie[:value] if cookie[:name] == '_yapi_token'
    end
    if need_notify
      upload_file = File.join(Lhj::Config.instance.home_dir, qr_code_key)
      driver.save_screenshot(upload_file)
      callback_param = generate_callback_param("#{Lhj::OSS::Helper.instance.url_path}/#{qr_code_key}")
      Lhj::OSS::Helper.instance.upload_file_callback(qr_code_key, upload_file, callback_param)
      need_notify = false
    end
    return res if res.values.count == 3
  end
end

#robot_urlObject



56
57
58
# File 'lib/lhj/command/yapi/yapi_init.rb', line 56

def robot_url
  'https://oapi.dingtalk.com/robot/send?access_token=fe879fd3e7a3b5e59d5719b2384845b7884901919be5a78fe443cbf777869807'
end

#save_user(result) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lhj/command/yapi/yapi_init.rb', line 78

def save_user(result)
   = load_config() if File.exist?()
   ||= []
  current = .find { |k| k['_yapi_uid'].eql?(result['_yapi_uid']) }
  if current
    current['__wpkreporterwid_'] = result['__wpkreporterwid_']
    current['_yapi_token'] = result['_yapi_token']
  else
     << result
  end
  file_to_save = File.open(, 'w+')
  YAML.dump(, file_to_save)
  file_to_save.close
end

#user_info_fileObject



70
71
72
# File 'lib/lhj/command/yapi/yapi_init.rb', line 70

def 
  File.join(Lhj::Config.instance.home_dir, 'yapi_user.yml')
end