lib/lhj/command/yapi.rb in lhj-tools-0.1.2 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.3
- old
+ new
@@ -5,10 +5,11 @@
module Lhj
class Command
class Yapi < Command
self.summary = '通过yapi接口生成请求'
+ self.description = '更新 ~/.lhj/yapi.yml 文件配置后执行`lhj api`生成接口模型'
def self.options
[
%w[--id api的id],
%w[--model-pre 模型的前缀],
@@ -41,12 +42,12 @@
end
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")
+ 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
@h_file_array ||= []
@@ -176,29 +177,30 @@
p_name = model['name']
p_properties = model['properties']
p_model = { name: p_name }
properties = []
- if p_type.eql?('object')
+ 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"
+ c_model[:type_name] = 'NSString'
else
c_model[:type_name] = o['name']
handle_model(o)
end
end
properties << c_model
end
p_model[:properties] = properties
@models << p_model
- elsif p_type.eql?('array')
+ when 'array'
t = model['items']
t['name'] = p_name
handle_model(t)
end
end
@@ -218,13 +220,13 @@
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
- puts_m "+(NSDictionary *)modelContainerPropertyGenericClass {"
+ puts_m '+(NSDictionary *)modelContainerPropertyGenericClass {'
puts_m " return @{#{str}};"
- puts_m "}"
+ puts_m '}'
end
puts_m "@end\n"
puts "\n\n"
end
end
@@ -235,68 +237,69 @@
type = m[:type]
des = m[:description] || ''
des.gsub!(/\n/, ' ')
default = m[:default]
puts_h "///#{des} #{default}"
- if type.eql?('integer')
+ case type
+ when 'integer'
puts_h "@property (nonatomic, assign) NSInteger #{key};"
if des.include?('分')
- puts_h "/////////==========删掉其中一个属性"
+ puts_h '/////////==========删掉其中一个属性'
puts_h "@property (nonatomic, strong) MLCentNumber *#{key};"
end
- elsif type.eql?('cent')
+ when 'cent'
puts_h "@property (nonatomic, strong) MLCentNumber *#{key};"
- elsif type.eql?('string')
+ when 'string'
puts_h "@property (nonatomic, copy) NSString *#{key};"
- elsif type.eql?('number')
+ when 'number'
puts_h "@property (nonatomic, strong) NSNumber *#{key};"
- elsif type.eql?('float')
+ when 'float'
puts_h "@property (nonatomic, assign) CGFloat #{key};"
- elsif type.eql?('double')
+ when 'double'
puts_h "@property (nonatomic, assign) double #{key};"
- elsif type.eql?('boolean')
+ when 'boolean'
puts_h "@property (nonatomic, assign) BOOL #{key};"
- elsif type.eql?('object')
+ when 'object'
puts_h "@property (nonatomic, strong) #{type_name} *#{key};"
- elsif type.eql?('array')
+ when 'array'
puts_h "@property (nonatomic, strong) NSArray<#{type_name} *> *#{key};"
else
puts_h "@property (nonatomic, copy) NSString *#{key};"
end
end
def print_methods
puts "\n<===============方法调用=====================>\n"
- puts_m "/**"
+ puts_m '/**'
puts_m " * #{@data_json['title']} -- #{@data_json['username']}"
- puts_m " */"
+ 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"
puts_m "static NSString * const #{key} = @\"#{@data_json['path']}\";"
puts_m "\n\n"
- puts_h "@interface MLParamModel : NSObject"
+ puts_h '@interface MLParamModel : NSObject'
@data_json['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_h '@end'
puts "\n\n"
model = @models.last
if @data_json['method'].eql?('GET')
puts_m " [MLNetworkingManager getWithUrl:#{key} params:nil response:^(MLResponseMessage *responseMessage) {"
- puts_m " if (response.resultCode == 0 && !response.error){"
- puts_m " NSDictionary *detailMsg = response.detailMsg"
+ puts_m ' if (response.resultCode == 0 && !response.error){'
+ puts_m ' NSDictionary *detailMsg = response.detailMsg'
puts_m " #{model[:name]} *model = [#{model[:name]} yy_modelWithDictionary:detailMsg];" if model
- puts_m " }"
- puts_m " }];"
+ puts_m ' }'
+ puts_m ' }];'
else
puts_m " [MLNetworkingManager postWithUrl:#{key} params:nil response:^(MLResponseMessage *responseMessage) {"
- puts_m " if (response.resultCode == 0 && !response.error){"
- puts_m " NSDictionary *detailMsg = response.detailMsg"
+ puts_m ' if (response.resultCode == 0 && !response.error){'
+ puts_m ' NSDictionary *detailMsg = response.detailMsg'
puts_m " #{model[:name]} *model = [#{model[:name]} yy_modelWithDictionary:detailMsg];" if model
- puts_m " }"
- puts_m " }];"
+ puts_m ' }'
+ puts_m ' }];'
end
end
end
end
end