Skip to content

Commit

Permalink
List status codes by type (#24)
Browse files Browse the repository at this point in the history
* method for listing codes by type
* specify type
* include type description
* optimize slow code
* refactor methods with high branch size
* add regex to throw error if type entered is wrong
* fix bug in grouping
* separate code type constant into file
* refactor tests for system exits
* remove redundant code
* add more test coverage
* add rspec matchers to test for actual status code

* bump version
  • Loading branch information
Yusuf Daniju authored and Austin Kabiru committed Mar 5, 2017
1 parent e4b78bb commit 9be5d74
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/tmp/
/config/env.yaml
/*.gem
test.log
3 changes: 2 additions & 1 deletion 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/status_code_types'
require 'optparse'
require 'ostruct'

Expand All @@ -18,7 +19,7 @@ def self.print_code(options)

unless status_code
puts "#{options.status_code} is not a valid code. See 'hscode --help'."
exit
exit 1
end

PrettyPrint.print(
Expand Down
33 changes: 31 additions & 2 deletions lib/hscode/input_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ def run_verbosely(opts)
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

Expand All @@ -63,6 +66,7 @@ def display_help_message(opts)
hscode -c 200
hscode -c 200 -v
hscode -l
hscode -l 2xx
'
exit
end
Expand All @@ -75,6 +79,31 @@ def display_version_number(opts)
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|
colour_code = code.to_s[0]
Expand Down
9 changes: 9 additions & 0 deletions lib/hscode/status_code_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Hscode
STATUS_CODE_TYPES = {
'1xx' => 'Informational',
'2xx' => 'Sucess',
'3xx' => 'Redirection',
'4xx' => 'Client Error',
'5xx' => 'Server Error'
}.freeze
end
2 changes: 1 addition & 1 deletion lib/hscode/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Hscode
VERSION = '0.1.1'.freeze
VERSION = '0.1.2'.freeze
end
37 changes: 37 additions & 0 deletions spec/exit_code_matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
RSpec::Matchers.define :terminate do |_code|
actual = nil

def supports_block_expectations?
true
end

match do |block|
begin
block.call
rescue SystemExit => e
actual = e.status
end
actual && (actual == status_code)
end

chain :with_code do |status_code|
@status_code = status_code
end

failure_message do |_block|
"expected block to call exit(#{status_code}) but exit" +
(actual.nil? ? ' not called' : "(#{actual}) was called")
end

failure_message_when_negated do |_block|
"expected block not to call exit(#{status_code})"
end

description do
"expect block to call exit(#{status_code})"
end

def status_code
@status_code ||= 0
end
end
54 changes: 52 additions & 2 deletions spec/hscode/input_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,61 @@
end
end

context 'when option is `-l 5xx`' do
it 'returns all http codes belonging to type' do
expect do
options = new_input_parser.parse(['-l', '5xx'])
expect(options).to be_an_instance_of(OpenStruct)
expect(options.verbose).to be nil
expect(options.status_type).to eq('5xx')
end.to terminate.with_code(0)
end
end

context 'when option is `-l 5xc`' do
it 'should exit with status 1' do
expect do
options = new_input_parser.parse(['-l', '5xc'])
expect(options).to be_an_instance_of(OpenStruct)
expect(options.verbose).to be nil
expect(options.status_type).to eq('5xx')
end.to terminate.with_code(1)
end
end

context 'list all http codes' do
it 'returns all http codes' do
expect do
options = new_input_parser.parse(['-l'])
expect(options).to be_an_instance_of(OpenStruct)
expect(options.status_type).to be nil
end.to terminate.with_code(0)
end
end

context 'when option is --help' do
it 'displays help message' do
expect do
options = new_input_parser.parse(['--help'])
expect(options).to be_an_instance_of(OpenStruct)
end.to terminate.with_code(0)
end
end

context 'when option is --version' do
it 'displays version' do
expect do
options = new_input_parser.parse(['--version'])
expect(options).to be_an_instance_of(OpenStruct)
end.to terminate.with_code(0)
end
end

context 'Invalid requests' do
let(:options) { new_input_parser.parse(['-f']) }

it 'raises an error' do
expect { options }.to raise_error
it 'exit with status 1' do
expect { options }.to terminate.with_code(1)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/hscode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
let(:error_msg) { "800 is not a valid code. See 'hscode --help'." }

it 'prints an error message' do
expect(invalid_code).to be_eql error_msg
expect do
expect(invalid_code).to be_eql error_msg
end.to terminate.with_code(1)
end
end
end
Expand Down
14 changes: 12 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'simplecov'
require 'coveralls'
require 'pry'
require 'exit_code_matchers'

Coveralls.wear!

Expand All @@ -20,7 +21,16 @@
require 'rspec'

RSpec.configure do |config|
# custom RSpec configurations
config.before(:all) do
@original_stdout = $stdout
$stdout = File.new(File.join(File.dirname(__FILE__), 'test.log'), 'w')
Pry.output = @original_stdout
end

config.after(:all) do
$stdout = @original_stdout
@original_stdout = nil
end
end

ENV["RUBY_ENV"] = "test"
ENV['RUBY_ENV'] = 'test'

0 comments on commit 9be5d74

Please sign in to comment.