class Hscode::InputParser
Attributes
options[R]
Public Class Methods
new()
click to toggle source
# File lib/hscode/input_parser.rb, line 7 def initialize @options = OpenStruct.new end
Public Instance Methods
parse(args)
click to toggle source
# File lib/hscode/input_parser.rb, line 11 def parse(args) option_parser.parse!(args) options rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument => e puts e, "See 'hscode --help'." exit 1 end
Private Instance Methods
code_type_group(type)
click to toggle source
# File lib/hscode/input_parser.rb, line 101 def code_type_group(type) HTTP_STATUS_CODES.select do |code, _| type.start_with? code.to_s[0] end end
display_help_message(opts)
click to toggle source
# File lib/hscode/input_parser.rb, line 63 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
display_version_number(opts)
click to toggle source
# File lib/hscode/input_parser.rb, line 75 def display_version_number(opts) opts.on_tail('--version', 'Show version') do puts Hscode::VERSION exit end end
list_status_codes(opts)
click to toggle source
# File lib/hscode/input_parser.rb, line 54 def list_status_codes(opts) 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
option_parser()
click to toggle source
# File lib/hscode/input_parser.rb, line 23 def option_parser OptionParser.new do |opts| opts.banner = 'Usage: hscode [options]' opts.separator '' opts.separator 'Specific options:' run_verbosely(opts) show_status_code(opts) list_status_codes(opts) opts.separator '' display_help_message(opts) display_version_number(opts) end end
print_all_codes()
click to toggle source
# File lib/hscode/input_parser.rb, line 107 def print_all_codes HTTP_STATUS_CODES.map do |code, info_hash| colour_code = code.to_s[0] PrettyPrint.print("#{code} - #{info_hash[:title]}", colour_code) end exit end
print_all_codes_by_type(type)
click to toggle source
# File lib/hscode/input_parser.rb, line 82 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
process_code_type(type, colour)
click to toggle source
# File lib/hscode/input_parser.rb, line 93 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
run_verbosely(opts)
click to toggle source
# File lib/hscode/input_parser.rb, line 47 def run_verbosely(opts) opts.on('-v', '--verbose', 'Show full HTTP status code documentation') do |v| options.verbose = v end end
show_status_code(opts)
click to toggle source
# File lib/hscode/input_parser.rb, line 40 def show_status_code(opts) opts.on('-c', '--code CODE', Integer, 'Show HTTP status code documentation') do |code| options.status_code = code end end