Skip to content

Commit 0c507f9

Browse files
committed
Tools(Spectrum): Finish spectrum inspector
1 parent fc23c4a commit 0c507f9

File tree

2 files changed

+83
-1432
lines changed

2 files changed

+83
-1432
lines changed

spectrum.rb

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,54 @@ def show(item_id = nil)
298298
nil
299299
end
300300

301-
def list
302-
apis.each { |a| puts a.name }
303-
nil
301+
def list(limit = -1, &predicate)
302+
if limit == -1
303+
apis.each { |a| puts a.name }
304+
else
305+
apis.first(limit).find_all(&predicate).each { |a| puts a.name }
306+
end
307+
return nil
308+
end
309+
310+
def list_from(item, limit = 10)
311+
list = apis.drop_while { |a| a.name != item.to_s }
312+
list.first(limit).each { |i| puts i }
313+
return apis.size - list.size
314+
end
315+
316+
def all_params
317+
apis.collect { |a| a.args }.flatten
318+
end
319+
320+
def all_return
321+
apis.collect { |a| a.return }
322+
end
323+
324+
def all_obj_return
325+
all_return.find_all { |rt| rt.is_a? Spectrum::Interface::ReturnTypeAndObject }
326+
end
327+
328+
def status
329+
nn_banner
330+
puts "Total #{apis.size} APIs, method count: #{apis.group_by { |a| a.method }.map { |m, c| "#{m}: #{c.size}" }.join('; ')}"
331+
puts "receiving #{all_params.collect { |it| it.extra[:type] }.uniq}\nand returning #{all_return.size} types (#{all_obj_return.collect { |it| it.name }.uniq})"
332+
nn_banner
333+
puts "#{all_params.find_all { |it| it.extra[:param_location] == 'path' }.size} path params, #{all_params.find_all { |it| it.extra[:param_location] == 'body' }.size} body params"
334+
return nil
335+
end
336+
337+
def tree(type = :plain)
338+
case type
339+
when :plain then apis.each { |a| puts "#{a.method} #{a.location}" }
340+
when :return then apis.each { |a| puts "#{a.method} #{a.location}\n = #{a.return}" }
341+
when :args then apis.each { |a| puts "#{a.method} #{a.location}\n * #{a.args.join("\n * ")}" }
342+
else
343+
apis.each do |a|
344+
puts "#{a.method} #{a.location} (#{a.args.join(';')})"
345+
puts " = #{a.return}"
346+
end
347+
end
348+
return nil
304349
end
305350

306351
def nil?
@@ -355,7 +400,42 @@ def ClientShowcase.from_file(path = Dir.glob('*.geekspec').first)
355400
end
356401
end
357402

403+
class SpringError
404+
attr_accessor :time, :status, :error, :message, :trace, :path
405+
406+
def initialize(json)
407+
@time = Time.at(0, json['timestamp'], :millisecond)
408+
@status = json['status']
409+
@error = json['error']
410+
@trace = json['trace'].split("\n\t")
411+
@path = json['path']
412+
@message = json['message']
413+
end
414+
415+
def to_s
416+
"[#{status}] Spring Application #{error} - #{path}: #{message} @ #{time}\n#{trace.first} #{trace[1]}"
417+
end
418+
end
419+
358420
def ClientShowcase.map_response(spec, resp)
421+
if (high_digit = resp.status / 100) != 2
422+
begin
423+
json = JSON.parse(resp.body)
424+
rescue
425+
return resp
426+
end
427+
428+
# 为什么要用这种滥用弱类型的手段... 异常系统不好么
429+
return SpringError.new(json) if json.each_key.to_a == %w[timestamp status error message trace path]
430+
431+
case high_digit
432+
when 4
433+
return json
434+
when 5
435+
return json
436+
end
437+
end
438+
359439
if (r = spec.return).is_a? Spectrum::Interface::ReturnTypeAndObject
360440
json = JSON.parse(resp.body)
361441
mapper = "map_resp_#{r.name}"

0 commit comments

Comments
 (0)