Class: Lhj::Command::Yapi
- Inherits:
-
Lhj::Command
- Object
- CLAide::Command
- Lhj::Command
- Lhj::Command::Yapi
- Defined in:
- lib/lhj/command/yapi.rb
Overview
generate model from yapi
Class Method Summary collapse
Instance Method Summary collapse
- #api_id ⇒ Object
- #begin_title ⇒ Object
- #fetch_req_body(res_json) ⇒ Object
- #fetch_res_boy(res_json) ⇒ Object
- #gen_model_name(name) ⇒ Object
- #handle ⇒ Object
- #handle_model(model, &block) ⇒ Object
-
#initialize(argv) ⇒ Yapi
constructor
A new instance of Yapi.
- #load_config(file) ⇒ Object
- #model_pre ⇒ Object
- #model_suffix ⇒ Object
- #print_http_method(data) ⇒ Object
- #print_model(m) ⇒ Object
- #print_model_for_java(m) ⇒ Object
- #print_models(models) ⇒ Object
- #print_models_for_java(models) ⇒ Object
- #print_models_impl(models) ⇒ Object
- #print_req_body_model(res_json) ⇒ Object
- #print_req_query(data) ⇒ Object
- #print_res_body_model(res_json) ⇒ Object
- #process(config_file) ⇒ Object
- #puts_h(str) ⇒ Object
- #puts_h_red(str) ⇒ Object
- #puts_m(str) ⇒ Object
- #req_api_model ⇒ Object
- #save_to_file ⇒ Object
- #test_ding ⇒ Object
- #url_str ⇒ Object
- #yml_file ⇒ Object
Methods inherited from Lhj::Command
Constructor Details
#initialize(argv) ⇒ Yapi
Returns a new instance of Yapi.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lhj/command/yapi.rb', line 22 def initialize(argv) @id = argv.option('id') @model_pre_name = argv.option('model-pre') @save = argv.flag?('save', false) @debug = argv.flag?('debug', false) @language = argv.option('lan', 'oc') @http_url = '' @http_headers = [] @config_id = '' @config_model_pre = 'ML' @model_default_suffix = 'Model' @type_trans = {} @config_model_names = [] @model_names = [] # <<<===== model for template @result_model_name = '' @param_model_name = '' @model_template = '' @desc = '' @path = '' @path_name = '' @path_key = '' @method = 'GET' @title = '' @username = '' @req_body_is_json_schema = false # =====>>> super end |
Class Method Details
.options ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/lhj/command/yapi.rb', line 14 def self. [ %w[--id api的id], %w[--model-pre 模型的前缀], %w[--save 保存生成文件] ] end |
Instance Method Details
#api_id ⇒ Object
137 138 139 |
# File 'lib/lhj/command/yapi.rb', line 137 def api_id @id || @config_id.to_s end |
#begin_title ⇒ Object
52 53 54 |
# File 'lib/lhj/command/yapi.rb', line 52 def begin_title '读取配置文件~/.lhj/yapi.yml' end |
#fetch_req_body(res_json) ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/lhj/command/yapi.rb', line 215 def fetch_req_body(res_json) return if !res_json || !res_json['data'] || !res_json['data']['req_body_other'] result = JSON.parse(res_json['data']['req_body_other']) result['name'] = gen_model_name('') result end |
#fetch_res_boy(res_json) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/lhj/command/yapi.rb', line 202 def fetch_res_boy(res_json) return if !res_json || !res_json['data'] || !res_json['data']['res_body'] res_body = JSON.parse(res_json['data']['res_body']) return if !res_body['properties'] || !res_body['properties']['detailMsg'] result = res_body['properties']['detailMsg'] return unless result['type'] == 'object' || result['type'] == 'array' result['name'] = gen_model_name('') result end |
#gen_model_name(name) ⇒ Object
223 224 225 226 227 228 229 230 231 |
# File 'lib/lhj/command/yapi.rb', line 223 def gen_model_name(name) n = name.gsub(/vo|model|list/i, '').gsub(/(.*)s$/, '\1').gsub(/^\w/) { $&.upcase } if n.length <= 0 n = @config_model_names.detect { |c| @model_names.none? { |na| na.gsub(/#{model_pre}(.*)Model/, '\1').eql?(c) } } end model_name = "#{model_pre}#{n}#{model_suffix}" @model_names << model_name model_name end |
#handle ⇒ Object
56 57 58 |
# File 'lib/lhj/command/yapi.rb', line 56 def handle process(yml_file) if File.exist?(yml_file) end |
#handle_model(model, &block) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/lhj/command/yapi.rb', line 233 def handle_model(model, &block) p_type = model['type'] p_name = model['name'] p_properties = model['properties'] p_model = { name: p_name } properties = [] case p_type when 'object' p_properties.each do |k, v| c_type = @type_trans[v['type']] || v['type'] c_model = { key: k, type: c_type, description: v['description'], default: '' } if v['type'].eql?('object') || v['type'].eql?('array') o = v['items'] || v o['name'] = gen_model_name(k) if v['type'].eql?('array') && v['items']['type'].eql?('string') c_model[:type_name] = 'NSString' else c_model[:type_name] = o['name'] handle_model(o, &block) end end properties << c_model end p_model[:properties] = properties block[p_model] if block_given? when 'array' t = model['items'] t['name'] = p_name handle_model(t, &block) end end |
#load_config(file) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/lhj/command/yapi.rb', line 124 def load_config(file) config = YAML.load_file(file) config.each do |k, v| @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'] @config_model_names = config['model_names'] @type_trans = config['type_trans'] end |
#model_pre ⇒ Object
141 142 143 |
# File 'lib/lhj/command/yapi.rb', line 141 def model_pre @model_pre_name || @config_model_pre end |
#model_suffix ⇒ Object
145 146 147 |
# File 'lib/lhj/command/yapi.rb', line 145 def model_suffix @config_model_suffix || @model_default_suffix end |
#print_http_method(data) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/lhj/command/yapi.rb', line 390 def print_http_method(data) return unless data @path = data['path'] @method = data['method'] @title = data['title'] @username = data['username'] @desc = data['desc'] @method = 'JSON' if data['req_body_is_json_schema'] @path_name = @path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { $&.upcase } }.join('') if @path @path_key = "k#{@path_name}URL" case @language when 'oc' puts "\n<===============方法调用=====================>\n".green @model_template = Lhj::ErbFormatter::Service.new(self).render('model') puts Lhj::ErbFormatter::Service.new(self).render('yapi').blue when 'java' end end |
#print_model(m) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/lhj/command/yapi.rb', line 304 def print_model(m) key = m[:key] type_name = m[:type_name] type = m[:type] des = m[:description] || '' des.gsub!(/\n/, ' ') default = m[:default] puts_h "///#{des} #{default}" case type when 'integer' puts_h "@property (nonatomic, assign) NSInteger #{key};" if des.include?('分') || des.include?('0.01') puts_h_red '/////////==========删掉其中一个属性' puts_h_red "@property (nonatomic, strong) MLCentNumber *#{key};" end when 'cent' puts_h "@property (nonatomic, strong) MLCentNumber *#{key};" when 'string' puts_h "@property (nonatomic, copy) NSString *#{key};" when 'number' puts_h "@property (nonatomic, strong) NSNumber *#{key};" when 'float' puts_h "@property (nonatomic, assign) CGFloat #{key};" when 'double' puts_h "@property (nonatomic, assign) double #{key};" when 'boolean' puts_h "@property (nonatomic, assign) BOOL #{key};" when 'object' puts_h "@property (nonatomic, strong) #{type_name} *#{key};" when 'array' puts_h "@property (nonatomic, strong) NSArray<#{type_name} *> *#{key};" else puts_h "@property (nonatomic, copy) NSString *#{key};" end end |
#print_model_for_java(m) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/lhj/command/yapi.rb', line 340 def print_model_for_java(m) key = m[:key] type_name = m[:type_name] type = m[:type] des = m[:description] || '' des.gsub!(/\n/, ' ') default = m[:default] case type when 'integer' puts_h "int #{key};//#{des} #{default}" when 'cent' puts_h "int #{key};//#{des} #{default}" when 'string' puts_h "String #{key};//#{des} #{default}" when 'number' puts_h "double #{key};//#{des} #{default}" when 'float' puts_h "double #{key};//#{des} #{default}" when 'double' puts_h "double #{key};//#{des} #{default}" when 'boolean' puts_h "boolean #{key};//#{des} #{default}" when 'object' puts_h "#{type_name} #{key};//#{des} #{default}" when 'array' puts_h "#{type_name}[] #{key};//#{des} #{default}" else puts_h "String #{key};//#{des} #{default}" end end |
#print_models(models) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/lhj/command/yapi.rb', line 266 def print_models(models) models.each do |model| model_name = model[:name] || '' model_properties = model[:properties] puts_h "@interface #{model_name} : NSObject" model_properties.each do |m| print_model(m) end puts_h "@end\n\n\n" end end |
#print_models_for_java(models) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/lhj/command/yapi.rb', line 278 def print_models_for_java(models) models.each do |model| model_name = model[:name] || '' model_properties = model[:properties] puts_h "public class #{model_name} extends BaseModel implements BusinessEvent.ProductDataCollect {" model_properties.each do |m| print_model_for_java(m) end puts_h "}\n\n\n" end end |
#print_models_impl(models) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/lhj/command/yapi.rb', line 290 def print_models_impl(models) 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&.length&.positive? puts_m '+(NSDictionary *)modelContainerPropertyGenericClass {' puts_m " return @{#{str}};" puts_m '}' end puts_m "@end\n" puts "\n\n" end end |
#print_req_body_model(res_json) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/lhj/command/yapi.rb', line 182 def print_req_body_model(res_json) req_body = fetch_req_body(res_json) return unless req_body puts "\n<===============打印请求模型-Begin=====================>\n".green models = [] handle_model(req_body) do |model| models << model end case @language when 'oc' print_models(models) print_models_impl(models) when 'java' print_models_for_java(models) end @param_model_name = models.last[:name] puts "\n<===============打印请求模型-End=====================>\n".green end |
#print_req_query(data) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/lhj/command/yapi.rb', line 372 def print_req_query(data) return unless data && data['req_query'] case @language when 'oc' puts_h '@interface MLParamModel : NSObject' data['req_query'].each do |h| des = h['desc'] puts_h "///#{des} #{h['example']}" puts_h "@property (nonatomic, copy) NSString *#{h['name']};" end puts_h '@end' puts "\n\n" when 'java' end @param_model_name = 'MLParamModel' if @param_model_name.empty? end |
#print_res_body_model(res_json) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/lhj/command/yapi.rb', line 162 def print_res_body_model(res_json) res_body = fetch_res_boy(res_json) return unless res_body puts "\n<===============打印返回数据模型-Begin=====================>\n".green models = [] handle_model(res_body) do |model| models << model end case @language when 'oc' print_models(models) print_models_impl(models) when 'java' print_models_for_java(models) end @result_model_name = models.last[:name] puts "\n<===============打印返回数据模型-End=====================>\n".green end |
#process(config_file) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lhj/command/yapi.rb', line 60 def process(config_file) load_config(config_file) res_body = req_api_model unless res_body['errcode'].to_i.zero? puts '请修改 ~/.lhj/yapi.yml文件的对应的__wpkreporterwid_,_yapi_token,_yapi_uid这三个值'.red return end return unless res_body && !res_body.empty? # 1.print request result print_res_body_model(res_body) # 2.print request json body print_req_body_model(res_body) # 3.print request param print_req_query(res_body['data']) # 4.print request method print_http_method(res_body['data']) # 5.save to file save_to_file if @save end |
#puts_h(str) ⇒ Object
88 89 90 91 92 |
# File 'lib/lhj/command/yapi.rb', line 88 def puts_h(str) puts str.magenta @h_file_array ||= [] @h_file_array << str end |
#puts_h_red(str) ⇒ Object
94 95 96 97 98 |
# File 'lib/lhj/command/yapi.rb', line 94 def puts_h_red(str) puts str.red @h_file_array ||= [] @h_file_array << str end |
#puts_m(str) ⇒ Object
100 101 102 103 104 |
# File 'lib/lhj/command/yapi.rb', line 100 def puts_m(str) puts str.blue @m_file_array ||= [] @m_file_array << str end |
#req_api_model ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/lhj/command/yapi.rb', line 149 def req_api_model uri = URI.parse(url_str) 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 res_json = JSON.parse(res.body) puts res.body unless res_json['errcode'].to_i.zero? puts res.body if @debug res_json end |
#save_to_file ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/lhj/command/yapi.rb', line 106 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.positive? File.write(m_file, @m_file_array.join("\n")) if @m_file_array.count.positive? puts "\n\n生成文件成功!所在路径:\n#{File.(h_file)} \n#{File.(m_file)}".green end |
#test_ding ⇒ Object
81 82 83 84 85 86 |
# File 'lib/lhj/command/yapi.rb', line 81 def test_ding require 'net/http' require 'uri' 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 |
#url_str ⇒ Object
116 117 118 |
# File 'lib/lhj/command/yapi.rb', line 116 def url_str "#{@http_url}#{api_id}" end |