lib/hscode/input_parser.rb in hscode-0.1.1 vs lib/hscode/input_parser.rb in hscode-0.1.2
- old
+ new
@@ -50,29 +50,58 @@
options.verbose = v
end
end
def list_status_codes(opts)
- opts.on('-l', '--list', 'List all HTTP status codes') do
- print_all_codes
+ opts.on('-l', '--list [TYPE]',
+ 'List all HTTP status codes of type') do |type|
+ print_all_codes unless type
+ options.status_type = type
+ print_all_codes_by_type(type)
end
end
def display_help_message(opts)
opts.on_tail('-h', '--help', 'Show this help message') do
puts opts, 'Examples:
hscode -c 200
hscode -c 200 -v
hscode -l
+ hscode -l 2xx
'
exit
end
end
def display_version_number(opts)
opts.on_tail('--version', 'Show version') do
puts Hscode::VERSION
exit
+ end
+ end
+
+ def print_all_codes_by_type(type)
+ unless type =~ /\A[1-5]x{2}\z/
+ abort "#{type} is not a valid code type. See 'hscode --help'."
+ end
+
+ colour_code = type.to_s[0]
+ PrettyPrint.print("#{type} #{STATUS_CODE_TYPES[type]}\n", colour_code)
+
+ process_code_type(type, colour_code)
+ end
+
+ def process_code_type(type, colour)
+ code_type_group(type).map do |code, info_hash|
+ PrettyPrint.print("#{code} - #{info_hash[:title]}", colour)
+ end
+
+ exit
+ end
+
+ def code_type_group(type)
+ HTTP_STATUS_CODES.select do |code, _|
+ type.start_with? code.to_s[0]
end
end
def print_all_codes
HTTP_STATUS_CODES.map do |code, info_hash|