Class: Pod::Command::Bin::Yapi

Inherits:
Pod::Command::Bin show all
Defined in:
lib/cocoapods-lhj-bin/command/bin/yapi.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pod::Command::Bin

#validate!

Methods included from CBin::SpecFilesHelper

#binary_spec, #binary_spec_files, #binary_template_spec, #binary_template_spec_file, #binary_template_spec_files, #clear_binary_spec_file_if_needed, #code_spec, #code_spec_files, #create_binary_spec_file, #find_spec_file, #spec_files

Methods included from CBin::SourcesHelper

#binary_source, #code_source, #sources_manager, #sources_option, #valid_sources

Constructor Details

#initialize(argv) ⇒ Yapi

Returns a new instance of Yapi.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 19

def initialize(argv)
  @id = argv.option('id')
  @model_pre_name = argv.option('model-pre')
  @save = argv.flag?('save', false)
  @http_url = ''
  @http_headers = []
  @data_json = {}
  @models = []
  @config_id = ''
  @config_model_pre = 'ML'
  @model_default_suffix = 'Model'
  @type_trans = {}
  @config_model_names = []
  @model_names = []
  super
end

Class Method Details

.optionsObject



11
12
13
14
15
16
17
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 11

def self.options
  [
    %w[--id api的id],
    %w[--model-pre 模型的前缀],
    %w[--save 保存生成文件]
  ]
end

Instance Method Details

#api_idObject



90
91
92
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 90

def api_id
  @id || @config_id.to_s
end

#fetch_modelObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 113

def fetch_model
  res_json = req_model
  begin
    puts "\n<===============打印返回数据模型-Begin=====================>\n"
    fetch_res_boy(res_json)
    print_models
    print_models_implementation
    puts "\n<===============打印返回数据模型-End=====================>\n"
  end
  begin
    puts "\n<===============打印请求模型-Begin=====================>\n"
    @models = []
    @model_names = []
    fetch_req_body(res_json)
    print_models
    print_models_implementation
    puts "\n<===============打印请求模型-End=====================>\n"
  end
end

#fetch_req_body(res_json) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 149

def fetch_req_body(res_json)
  if res_json && res_json['data']
    @data_json = res_json['data']
    if @data_json['req_body_other']
      begin
        res_body = JSON.parse(@data_json['req_body_other'])
        res_body['name'] = gen_model_name('')
        handle_model(res_body)
      rescue => ex
        puts ex
      end
    end
  end
end

#fetch_res_boy(res_json) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 133

def fetch_res_boy(res_json)
  if res_json && res_json['data']
    @data_json = res_json['data']
    if @data_json['res_body']
      begin
        res_body = JSON.parse(@data_json['res_body'])
        detail_obj = res_body['properties']['detailMsg'] || {}
        detail_obj['name'] = gen_model_name('')
        handle_model(detail_obj)
      rescue => ex
        puts ex
      end
    end
  end
end

#gen_model_name(name) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 164

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.any?{ |n| n.gsub(/#{model_pre}(.*)Model/, '\1').eql?(c) } }
  end
  model_name = "#{model_pre}#{n}#{model_suffix}"
  @model_names << model_name
  model_name
end

#handle_model(model) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 174

def handle_model(model)
  p_type = model['type']
  p_name = model['name']
  p_properties = model['properties']
  p_model = { name: p_name }

  properties = []
  if p_type.eql?('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)
        end
      end
      properties << c_model
    end
    p_model[:properties] = properties
    @models << p_model
  elsif p_type.eql?('array')
    t = model['items']
    t['name'] = p_name
    handle_model(t)
  end
end

#load_configObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 76

def load_config
  yml = File.join(Pod::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'))
  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_preObject



94
95
96
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 94

def model_pre
  @model_pre_name || @config_model_pre
end

#model_suffixObject



98
99
100
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 98

def model_suffix
  @config_model_suffix || @model_default_suffix
end


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 267

def print_methods
  puts "\n<===============方法调用=====================>\n"
  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"
  puts_m "static NSString * const #{key} = @\"#{@data_json['path']}\";"
  puts_m "\n\n"
  puts_h "@interface MLParamModel : NSObject"
  @data_json['req_query'].each do |h|
    des = h['desc'].gsub(/\n/, '  ')
    puts_h "///#{des}  #{h['example']}"
    puts_h "@property (nonatomic, copy) NSString *#{h['name']};"
  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 "            #{model[:name]} *model = [#{model[:name]} yy_modelWithDictionary:detailMsg];" if model
    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 "            #{model[:name]} *model = [#{model[:name]} yy_modelWithDictionary:detailMsg];" if model
    puts_m "        }"
    puts_m "    }];"
  end
end


232
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
265
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 232

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}"
  if type.eql?('integer')
    puts_h "@property (nonatomic, assign) NSInteger #{key};"
    if des.include?('')
      puts_h "/////////==========删掉其中一个属性"
      puts_h "@property (nonatomic, strong) MLCentNumber *#{key};"
    end
  elsif type.eql?('cent')
    puts_h "@property (nonatomic, strong) MLCentNumber *#{key};"
  elsif type.eql?('string')
    puts_h "@property (nonatomic, copy) NSString *#{key};"
  elsif type.eql?('number')
    puts_h "@property (nonatomic, strong) NSNumber *#{key};"
  elsif type.eql?('float')
    puts_h "@property (nonatomic, assign) CGFloat #{key};"
  elsif type.eql?('double')
    puts_h "@property (nonatomic, assign) double #{key};"
  elsif type.eql?('boolean')
    puts_h "@property (nonatomic, assign) BOOL #{key};"
  elsif type.eql?('object')
    puts_h "@property (nonatomic, strong) #{type_name} *#{key};"
  elsif type.eql?('array')
    puts_h "@property (nonatomic, strong) NSArray<#{type_name} *> *#{key};"
  else
    puts_h "@property (nonatomic, copy) NSString *#{key};"
  end
end


206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 206

def print_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


218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 218

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 "  return @{#{str}};"
      puts_m "}"
    end
    puts_m "@end\n"
    puts "\n\n"
  end
end

#puts_h(str) ⇒ Object



50
51
52
53
54
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 50

def puts_h(str)
  puts str
  @h_file_array ||= []
  @h_file_array << str
end

#puts_m(str) ⇒ Object



56
57
58
59
60
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 56

def puts_m(str)
  puts str
  @m_file_array ||= []
  @m_file_array << str
end

#req_modelObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 102

def req_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
  puts res.body
  JSON.parse(res.body)
end

#runObject



36
37
38
39
40
41
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 36

def run
  load_config
  fetch_model
  print_methods
  save_to_file if @save
end

#save_to_fileObject



62
63
64
65
66
67
68
69
70
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 62

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)}"
end

#test_dingObject



43
44
45
46
47
48
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 43

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_strObject



72
73
74
# File 'lib/cocoapods-lhj-bin/command/bin/yapi.rb', line 72

def url_str
  "#{@http_url}#{api_id}"
end