Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add search functionality #25

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions lib/hscode.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions lib/hscode/input_parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'hscode/pretty_print'

module Hscode
class InputParser
Expand Down Expand Up @@ -29,6 +28,7 @@ def option_parser
run_verbosely(opts)
show_status_code(opts)
list_status_codes(opts)
search_status_code(opts)

opts.separator ''

Expand All @@ -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|
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/hscode/input_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']) }

Expand Down
36 changes: 32 additions & 4 deletions spec/hscode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,45 @@
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

context 'when options is non verbose' do
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

Expand Down