diff --git a/lib/hscode.rb b/lib/hscode.rb index 0c00569..d36b830 100644 --- a/lib/hscode.rb +++ b/lib/hscode.rb @@ -1,6 +1,7 @@ require 'hscode/version' require 'hscode/input_parser' require 'hscode/http_status_codes' +require 'hscode/pretty_print' require 'hscode/status_code_types' require 'optparse' require 'ostruct' @@ -9,28 +10,56 @@ module Hscode class CliController def self.call(args) options = Hscode::InputParser.new.parse(args) - print_code(options) + options.title ? print_title(options) : print_code(options) end private_class_method def self.print_code(options) status_code = HTTP_STATUS_CODES[options.status_code.to_i] - unless status_code puts "#{options.status_code} is not a valid code. See 'hscode --help'." exit 1 end - PrettyPrint.print( - "#{options.status_code} - #{status_code[:title]}", - options.status_code.to_s[0] - ) - print_description(status_code) if options.verbose + print_result(options.status_code, status_code[:title], + status_code[:description], options.verbose) + exit + end + + def self.print_title(options) + title_data = get_title_data(options.title) + + validate_title_data(title_data, options) + + title_data.each do |data| + print_result(data.first, data[1][:title], + data[1][:description], options.verbose) + end + exit + end + + def self.validate_title_data(title_data, options) + return unless title_data.empty? + puts "#{options.title} is not a valid HTTP status. " \ + "See 'hscode --list' to see the list of valid HTTP codes." + exit 1 + end + + def self.get_title_data(title) + data = HTTP_STATUS_CODES.select do |_, value| + value[:title].downcase.match title.downcase + end + data + end + + def self.print_result(code, title, desc, verbose) + PrettyPrint.print("#{code} - #{title}", code.to_s[0]) + print_description(desc) if verbose end - def self.print_description(status_code) - status_code[:description].each do |desc| + def self.print_description(descriptions) + descriptions.each do |desc| PrettyPrint.print("\n#{desc}", '0') end end diff --git a/lib/hscode/input_parser.rb b/lib/hscode/input_parser.rb index 3a160ac..fb05417 100644 --- a/lib/hscode/input_parser.rb +++ b/lib/hscode/input_parser.rb @@ -1,4 +1,3 @@ -require 'hscode/pretty_print' module Hscode class InputParser @@ -29,6 +28,7 @@ def option_parser run_verbosely(opts) show_status_code(opts) list_status_codes(opts) + search_status_code(opts) opts.separator '' @@ -37,6 +37,13 @@ def option_parser end end + def search_status_code(opts) + opts.on('-s', '--search TITLE', String, + 'Search for HTTP status code with its title') do |title| + options.title = title + end + end + def show_status_code(opts) opts.on('-c', '--code CODE', Integer, 'Show HTTP status code documentation') do |code| @@ -62,12 +69,15 @@ def list_status_codes(opts) def display_help_message(opts) opts.on_tail('-h', '--help', 'Show this help message') do - puts opts, 'Examples: + puts opts, "Examples: hscode -c 200 hscode -c 200 -v hscode -l hscode -l 2xx - ' + hscode -s ok + hscode -s 'not found' + hscode -s ok -v + " exit end end diff --git a/spec/hscode/input_parser_spec.rb b/spec/hscode/input_parser_spec.rb index 88a48c9..0cd2545 100644 --- a/spec/hscode/input_parser_spec.rb +++ b/spec/hscode/input_parser_spec.rb @@ -68,6 +68,22 @@ end end + context 'search for code with title' do + it 'detects -s as a valid command' do + options = new_input_parser.parse(['-s', 'ok']) + expect(options).to be_an_instance_of(OpenStruct) + expect(options.verbose).to be nil + expect(options.title).to eql 'ok' + end + + it 'searches with verbose' do + options = new_input_parser.parse(['-s', 'not found', '-v']) + expect(options).to be_an_instance_of(OpenStruct) + expect(options.verbose).to be true + expect(options.title).to eql 'not found' + end + end + context 'Invalid requests' do let(:options) { new_input_parser.parse(['-f']) } diff --git a/spec/hscode_spec.rb b/spec/hscode_spec.rb index 1f711ed..a2262e1 100644 --- a/spec/hscode_spec.rb +++ b/spec/hscode_spec.rb @@ -15,9 +15,35 @@ let(:verbose_cmd) { described_class.call(['-c', '200', '-v']) } it 'prints full status code documentation' do - expect(verbose_cmd).to be_an_instance_of(Array) - expect(verbose_cmd) - .to match_array(Hscode::HTTP_STATUS_CODES[200][:description]) + expect do + expect(verbose_cmd).to be_an_instance_of(Array) + .to match_array(Hscode::HTTP_STATUS_CODES[200][:description]) + end.to terminate.with_code(0) + end + end + + context 'when options is search' do + let(:search_cmd) { described_class.call(['-s', 'ok']) } + let(:search_verbose_cmd) { described_class.call(['-s', 'ok', '-v']) } + let(:bad_search_cmd) { described_class.call(['-s', 'notfound', '-v']) } + let(:error_msg) { 'notfound is not a valid HTTP status.' } + + it 'prints search result' do + expect do + expect(search_cmd).to be_an_instance_of(Array) + end.to terminate.with_code(0) + end + + it 'prints full status code documentation with ' do + expect do + expect(search_verbose_cmd).to be_an_instance_of(Array) + end.to terminate.with_code(0) + end + + it 'prints an error message' do + expect do + expect(bad_search_cmd).to match error_msg + end.to terminate.with_code(1) end end @@ -25,7 +51,9 @@ let(:non_verbose_cmd) { described_class.call(['-c', '422']) } it 'prints code title' do - expect(non_verbose_cmd).to be_nil + expect do + expect(non_verbose_cmd).to be_nil + end.to terminate.with_code(0) end end