From c3248aac85d706dad1cc38d3527ae104ccb0ee8e Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Sun, 5 Mar 2017 11:39:24 +0100 Subject: [PATCH 01/10] add search functionality --- lib/hscode.rb | 23 +++++++++++++++++++++-- lib/hscode/input_parser.rb | 15 ++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/hscode.rb b/lib/hscode.rb index 515cc28..e70ea14 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 'optparse' require 'ostruct' @@ -8,13 +9,13 @@ module Hscode class CliController def self.call(args) options = Hscode::InputParser.new.parse(args) - print_code(options) + options.title ? print_title_code(options) : print_code(options) end private_class_method def self.print_code(options) - status_code = HTTP_STATUS_CODES[options.status_code.to_i] + 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'." @@ -28,6 +29,24 @@ def self.print_code(options) print_description(status_code) if options.verbose end + def self.print_title_code(options) + status_object = HTTP_STATUS_CODES.detect {|_, value| value[:title].downcase == options.title.downcase} + + unless status_object + puts "#{options.title} is not a valid HTTP status. See 'hscode --list' to see the list of valid HTTP codes." + exit + end + + status_code = status_object.first + + PrettyPrint.print( + "#{status_object[1][:title]} - #{status_code}", + status_code.to_s[0] + ) + + print_description(status_object[1]) if options.verbose + end + def self.print_description(status_code) status_code[:description].each do |desc| PrettyPrint.print("\n#{desc}", '0') diff --git a/lib/hscode/input_parser.rb b/lib/hscode/input_parser.rb index 6ffc15b..c0d0280 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| @@ -59,11 +66,13 @@ 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 -s 'ok' + hscode -s 'ok' -v + " exit end end From ec0c66cd06e1a40ccfc7ca21307f135ee4ef3964 Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Sun, 5 Mar 2017 16:29:17 +0100 Subject: [PATCH 02/10] address test errors --- lib/hscode.rb | 17 ++++++++++------- lib/hscode/input_parser.rb | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/hscode.rb b/lib/hscode.rb index e70ea14..cdd32a5 100644 --- a/lib/hscode.rb +++ b/lib/hscode.rb @@ -9,13 +9,13 @@ module Hscode class CliController def self.call(args) options = Hscode::InputParser.new.parse(args) - options.title ? print_title_code(options) : 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] + 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'." @@ -29,17 +29,20 @@ def self.print_code(options) print_description(status_code) if options.verbose end - def self.print_title_code(options) - status_object = HTTP_STATUS_CODES.detect {|_, value| value[:title].downcase == options.title.downcase} + def self.print_title(options) + status_object = HTTP_STATUS_CODES.detect do |_, value| + value[:title].casecmp(options.title) == 0 + end unless status_object - puts "#{options.title} is not a valid HTTP status. See 'hscode --list' to see the list of valid HTTP codes." - exit + puts "#{options.title} is not a valid HTTP status. " \ + "See 'hscode --list' to see the list of valid HTTP codes." + exit 1 end status_code = status_object.first - PrettyPrint.print( + PrettyPrint.print( "#{status_object[1][:title]} - #{status_code}", status_code.to_s[0] ) diff --git a/lib/hscode/input_parser.rb b/lib/hscode/input_parser.rb index c0d0280..6602219 100644 --- a/lib/hscode/input_parser.rb +++ b/lib/hscode/input_parser.rb @@ -70,8 +70,9 @@ def display_help_message(opts) hscode -c 200 hscode -c 200 -v hscode -l - hscode -s 'ok' - hscode -s 'ok' -v + hscode -s ok + hscode -s 'not found' + hscode -s ok -v " exit end From 545c637f8acacf4e82d32a5578cf2b886bac44f1 Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Sun, 5 Mar 2017 17:27:07 +0100 Subject: [PATCH 03/10] a little refactor --- lib/hscode.rb | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/hscode.rb b/lib/hscode.rb index cdd32a5..5c96d6d 100644 --- a/lib/hscode.rb +++ b/lib/hscode.rb @@ -22,36 +22,38 @@ def self.print_code(options) exit end - PrettyPrint.print( - "#{options.status_code} - #{status_code[:title]}", - options.status_code.to_s[0] + print_result(options.status_code, status_code[:title], + status_code[:description], options.verbose ) - print_description(status_code) if options.verbose end def self.print_title(options) - status_object = HTTP_STATUS_CODES.detect do |_, value| - value[:title].casecmp(options.title) == 0 - end + title_data = get_title_data(options.title) - unless status_object + unless title_data puts "#{options.title} is not a valid HTTP status. " \ "See 'hscode --list' to see the list of valid HTTP codes." exit 1 end - status_code = status_object.first - - PrettyPrint.print( - "#{status_object[1][:title]} - #{status_code}", - status_code.to_s[0] + print_result(title_data.first, title_data[1][:title], + title_data[1][:description], options.verbose ) + end + + def self.get_title_data(title) + HTTP_STATUS_CODES.detect do |_, value| + value[:title].casecmp(title) == 0 + end + end - print_description(status_object[1]) if options.verbose + 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 From c4a1c9461f899357c126b2bd8752dbf0241f370b Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Sun, 5 Mar 2017 18:08:26 +0100 Subject: [PATCH 04/10] fix rubocop --- lib/hscode.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/hscode.rb b/lib/hscode.rb index 5c96d6d..08f70db 100644 --- a/lib/hscode.rb +++ b/lib/hscode.rb @@ -23,8 +23,7 @@ def self.print_code(options) end print_result(options.status_code, status_code[:title], - status_code[:description], options.verbose - ) + status_code[:description], options.verbose) end def self.print_title(options) @@ -37,18 +36,17 @@ def self.print_title(options) end print_result(title_data.first, title_data[1][:title], - title_data[1][:description], options.verbose - ) + title_data[1][:description], options.verbose) end def self.get_title_data(title) - HTTP_STATUS_CODES.detect do |_, value| - value[:title].casecmp(title) == 0 + HTTP_STATUS_CODES.detect do |_, value| + value[:title].casecmp(title).zero? end end def self.print_result(code, title, desc, verbose) - PrettyPrint.print( "#{code} - #{title}", code.to_s[0]) + PrettyPrint.print("#{code} - #{title}", code.to_s[0]) print_description(desc) if verbose end From 4da23048eeda282ea46bbabd5a53ddf1ae01a0e8 Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Mon, 6 Mar 2017 10:01:56 +0100 Subject: [PATCH 05/10] add test --- lib/hscode.rb | 4 ++-- spec/hscode/input_parser_spec.rb | 16 ++++++++++++++ spec/hscode_spec.rb | 38 ++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/lib/hscode.rb b/lib/hscode.rb index 4aad9a4..1a77391 100644 --- a/lib/hscode.rb +++ b/lib/hscode.rb @@ -17,7 +17,6 @@ def self.call(args) 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 @@ -29,7 +28,6 @@ def self.print_code(options) def self.print_title(options) title_data = get_title_data(options.title) - unless title_data puts "#{options.title} is not a valid HTTP status. " \ "See 'hscode --list' to see the list of valid HTTP codes." @@ -49,6 +47,8 @@ def self.get_title_data(title) def self.print_result(code, title, desc, verbose) PrettyPrint.print("#{code} - #{title}", code.to_s[0]) print_description(desc) if verbose + + exit end def self.print_description(descriptions) diff --git a/spec/hscode/input_parser_spec.rb b/spec/hscode/input_parser_spec.rb index 88a48c9..35eb9a1 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 'sees -s as a valid request' 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..29a31e6 100644 --- a/spec/hscode_spec.rb +++ b/spec/hscode_spec.rb @@ -15,9 +15,37 @@ 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) + expect(verbose_cmd) + .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(:invalid_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) + expect(search_verbose_cmd).to match_array(Hscode::HTTP_STATUS_CODES[200][:description]) + end.to terminate.with_code(0) + end + + it 'prints an error message' do + expect do + expect(invalid_search_cmd).to match error_msg + end.to terminate.with_code(1) end end @@ -25,7 +53,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 From 445c9140343ba4fb27f882c26ef33aac07b4a484 Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Mon, 6 Mar 2017 10:03:31 +0100 Subject: [PATCH 06/10] remove mistake --- spec/hscode_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/hscode_spec.rb b/spec/hscode_spec.rb index 29a31e6..55609df 100644 --- a/spec/hscode_spec.rb +++ b/spec/hscode_spec.rb @@ -17,7 +17,6 @@ it 'prints full status code documentation' do expect do expect(verbose_cmd).to be_an_instance_of(Array) - expect(verbose_cmd) .to match_array(Hscode::HTTP_STATUS_CODES[200][:description]) end.to terminate.with_code(0) end From a228902f6148fd439b4ac9e8d0e504b7fef5db5d Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Mon, 6 Mar 2017 10:05:13 +0100 Subject: [PATCH 07/10] remove unnecessary code --- spec/hscode_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/hscode_spec.rb b/spec/hscode_spec.rb index 55609df..4022e72 100644 --- a/spec/hscode_spec.rb +++ b/spec/hscode_spec.rb @@ -37,7 +37,6 @@ it 'prints full status code documentation with ' do expect do expect(search_verbose_cmd).to be_an_instance_of(Array) - expect(search_verbose_cmd).to match_array(Hscode::HTTP_STATUS_CODES[200][:description]) end.to terminate.with_code(0) end From f535e4b796f4f207665c66885f6fae9d9b93c120 Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Mon, 6 Mar 2017 10:09:57 +0100 Subject: [PATCH 08/10] fix rubocop issues --- spec/hscode_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/hscode_spec.rb b/spec/hscode_spec.rb index 4022e72..a2262e1 100644 --- a/spec/hscode_spec.rb +++ b/spec/hscode_spec.rb @@ -25,8 +25,8 @@ 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(:invalid_search_cmd) { described_class.call(['-s', 'notfound', '-v']) } - let(:error_msg) { "notfound is not a valid HTTP status." } + 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 @@ -40,9 +40,9 @@ end.to terminate.with_code(0) end - it 'prints an error message' do + it 'prints an error message' do expect do - expect(invalid_search_cmd).to match error_msg + expect(bad_search_cmd).to match error_msg end.to terminate.with_code(1) end end From 56e4bcba711ded7a90808315b87b506907df51bf Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Wed, 8 Mar 2017 12:29:17 +0100 Subject: [PATCH 09/10] make search more flexible --- lib/hscode.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/hscode.rb b/lib/hscode.rb index 1a77391..e557b51 100644 --- a/lib/hscode.rb +++ b/lib/hscode.rb @@ -24,31 +24,36 @@ def self.print_code(options) 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) - unless title_data + + if 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 - print_result(title_data.first, title_data[1][:title], - title_data[1][:description], options.verbose) + title_data.each do |data| + print_result(data.first, data[1][:title], + data[1][:description], options.verbose) + end + exit end def self.get_title_data(title) - HTTP_STATUS_CODES.detect do |_, value| - value[:title].casecmp(title).zero? + 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 - exit end def self.print_description(descriptions) From 0ab58eced03b620caddb31a18d72ab7f8f74c273 Mon Sep 17 00:00:00 2001 From: Adepoju Adebayo Date: Wed, 8 Mar 2017 12:53:02 +0100 Subject: [PATCH 10/10] refactor as per PR comments --- lib/hscode.rb | 16 +++++++++------- lib/hscode/input_parser.rb | 2 +- spec/hscode/input_parser_spec.rb | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/hscode.rb b/lib/hscode.rb index e557b51..d36b830 100644 --- a/lib/hscode.rb +++ b/lib/hscode.rb @@ -30,19 +30,22 @@ def self.print_code(options) def self.print_title(options) title_data = get_title_data(options.title) - if 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 + validate_title_data(title_data, options) title_data.each do |data| print_result(data.first, data[1][:title], - data[1][:description], options.verbose) + 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 @@ -53,7 +56,6 @@ def self.get_title_data(title) 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(descriptions) diff --git a/lib/hscode/input_parser.rb b/lib/hscode/input_parser.rb index 965d9dc..fb05417 100644 --- a/lib/hscode/input_parser.rb +++ b/lib/hscode/input_parser.rb @@ -73,10 +73,10 @@ def display_help_message(opts) hscode -c 200 hscode -c 200 -v hscode -l + hscode -l 2xx hscode -s ok hscode -s 'not found' hscode -s ok -v - hscode -l 2xx " exit end diff --git a/spec/hscode/input_parser_spec.rb b/spec/hscode/input_parser_spec.rb index 35eb9a1..0cd2545 100644 --- a/spec/hscode/input_parser_spec.rb +++ b/spec/hscode/input_parser_spec.rb @@ -69,7 +69,7 @@ end context 'search for code with title' do - it 'sees -s as a valid request' 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