lib/lhj/command/yapi.rb in lhj-tools-0.1.6 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.7
- old
+ new
@@ -3,10 +3,11 @@
require 'json'
require 'yaml'
module Lhj
class Command
+ # generate model from yapi
class Yapi < Command
self.summary = '通过yapi接口生成请求'
self.description = '更新 ~/.lhj/yapi.yml 文件配置后执行`lhj api`生成接口模型'
def self.options
@@ -32,11 +33,11 @@
@config_model_names = []
@model_names = []
super
end
- def run
+ def handle
load_config
fetch_model
print_methods
save_to_file if @save
end
@@ -47,40 +48,40 @@
body = { 'msgtype' => 'text', 'text' => { 'content' => 'error:上传蒲公英超时失败!' } }.to_json
Net::HTTP.post(URI('https://oapi.dingtalk.com/robot/send?access_token=6a3519057170cdb1b7274edfe43934c84a0062ffe2c9bcced434699296a7e26e'), body, 'Content-Type' => 'application/json')
end
def puts_h(str)
- puts str
+ puts str.magenta
@h_file_array ||= []
@h_file_array << str
end
def puts_m(str)
- puts str
+ puts str.blue
@m_file_array ||= []
@m_file_array << str
end
def save_to_file
@model_names = []
file_name = gen_model_name('')
h_file = File.join('.', "#{file_name}.h")
m_file = File.join('.', "#{file_name}.m")
- File.write(h_file, @h_file_array.join("\n")) if @h_file_array.count > 0
- File.write(m_file, @m_file_array.join("\n")) if @m_file_array.count > 0
- puts "\n\n生成文件成功!所在路径:\n#{File.expand_path(h_file)} \n#{File.expand_path(m_file)}"
+ File.write(h_file, @h_file_array.join("\n")) if @h_file_array.count.positive?
+ File.write(m_file, @m_file_array.join("\n")) if @m_file_array.count.positive?
+ puts "\n\n生成文件成功!所在路径:\n#{File.expand_path(h_file)} \n#{File.expand_path(m_file)}".green
end
def url_str
"#{@http_url}#{api_id}"
end
def load_config
yml = File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
config = YAML.load_file(yml)
config.each do |k, v|
- @http_headers << "#{k}=#{v}" if (k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid'))
+ @http_headers << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
end
@http_url = config['url']
@config_id = config['id']
@config_model_pre = config['model_pre']
@config_model_suffix = config['model_suffix']
@@ -105,31 +106,31 @@
req = Net::HTTP::Get.new(uri)
req['Cookie'] = @http_headers.join('; ')
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
- puts res.body
+ puts res.body unless res.body['errcode'].to_i.zero?
JSON.parse(res.body)
end
def fetch_model
res_json = req_model
begin
- puts "\n<===============打印返回数据模型-Begin=====================>\n"
+ puts "\n<===============打印返回数据模型-Begin=====================>\n".green
fetch_res_boy(res_json)
print_models
print_models_implementation
- puts "\n<===============打印返回数据模型-End=====================>\n"
+ puts "\n<===============打印返回数据模型-End=====================>\n".green
end
begin
- puts "\n<===============打印请求模型-Begin=====================>\n"
+ puts "\n<===============打印请求模型-Begin=====================>\n".green
@models = []
@model_names = []
fetch_req_body(res_json)
print_models
print_models_implementation
- puts "\n<===============打印请求模型-End=====================>\n"
+ puts "\n<===============打印请求模型-End=====================>\n".green
end
end
def fetch_res_boy(res_json)
if res_json && res_json['data']
@@ -219,11 +220,11 @@
def print_models_implementation
@models.each do |model|
puts_m "@implementation #{model[:name]}"
str = model[:properties].filter { |p| p[:type].eql?('array') && !p[:type_name].eql?('NSString') }.map { |p| "@\"#{p[:key]}\": #{p[:type_name]}.class" }.join(', ')
- if str && str.length > 0
+ if str && str.length.positive?
puts_m '+(NSDictionary *)modelContainerPropertyGenericClass {'
puts_m " return @{#{str}};"
puts_m '}'
end
puts_m "@end\n"
@@ -266,10 +267,10 @@
puts_h "@property (nonatomic, copy) NSString *#{key};"
end
end
def print_methods
- puts "\n<===============方法调用=====================>\n"
+ puts "\n<===============方法调用=====================>\n".green
puts_m '/**'
puts_m " * #{@data_json['title']} -- #{@data_json['username']}"
puts_m ' */'
key_str = @data_json['path'].split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { $&.upcase } }.join('')
key = "k#{key_str}URL"