您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

fixture.rb 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. require 'openssl'
  2. class String
  3. alias :colorize_old :colorize
  4. COLORS = {
  5. critical: { color: :white, background: :red },
  6. error: :red,
  7. warning: :light_red,
  8. good: :green,
  9. perfect: :blue,
  10. best: :magenta,
  11. unknown: { background: :black }
  12. }
  13. def colorize(state)
  14. color = COLORS[state] || state
  15. self.colorize_old color
  16. end
  17. end
  18. class Exception
  19. BACKTRACE_REGEXP = /^(.*):(\d+):in `(.*)'$/
  20. def colorize
  21. $stderr.puts self.message.colorize(:red)
  22. self.backtrace.each do |line|
  23. line = BACKTRACE_REGEXP.match line
  24. line = '%s:%s:in `%s\'' % [
  25. line[1].colorize(:yellow),
  26. line[2].colorize(:blue),
  27. line[3].colorize(:magenta)
  28. ]
  29. $stderr.puts line
  30. end
  31. end
  32. end
  33. class Integer
  34. def humanize
  35. secs = self
  36. [[60, :second],
  37. [60, :minute],
  38. [24, :hour],
  39. [30, :day],
  40. [12, :month]].map do |count, name|
  41. if secs > 0
  42. secs, n = secs.divmod count
  43. n = n.to_i
  44. n > 0 ? "#{n} #{name}#{n > 1 ? 's' : ''}" : nil
  45. end
  46. end.compact.reverse.join ' '
  47. end
  48. end
  49. class ::OpenSSL::PKey::EC
  50. def type
  51. :ecc
  52. end
  53. def size
  54. self.group.degree
  55. end
  56. def to_s
  57. "ECC #{self.size} bits"
  58. end
  59. def status
  60. case self.size
  61. when 0...160
  62. :critical
  63. when 160...192
  64. :error
  65. when 192...256
  66. :warning
  67. when 384...::Float::INFINITY
  68. :good
  69. end
  70. end
  71. end
  72. class ::OpenSSL::PKey::RSA
  73. def type
  74. :rsa
  75. end
  76. def size
  77. self.n.num_bits
  78. end
  79. def to_s
  80. "RSA #{self.size} bits"
  81. end
  82. def status
  83. case self.size
  84. when 0...1024
  85. :critical
  86. when 1024...2048
  87. :error
  88. when 4096...::Float::INFINITY
  89. :good
  90. end
  91. end
  92. end
  93. class ::OpenSSL::PKey::DSA
  94. def type
  95. :dsa
  96. end
  97. def size
  98. self.p.num_bits
  99. end
  100. def to_s
  101. "DSA #{self.size} bits"
  102. end
  103. def status
  104. return :critical
  105. end
  106. end
  107. class ::OpenSSL::PKey::DH
  108. def type
  109. :dh
  110. end
  111. def size
  112. self.p.num_bits
  113. end
  114. def to_s
  115. "DH #{self.size} bits"
  116. end
  117. def status
  118. case self.size
  119. when 0...1024
  120. :critical
  121. when 1024...2048
  122. :error
  123. when 2048...4096
  124. else
  125. :good
  126. end
  127. end
  128. end