Skip to content

Commit

Permalink
add status code lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Kabiru committed Nov 10, 2016
1 parent 6c7e377 commit 1931ed8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
12 changes: 11 additions & 1 deletion lib/hscode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ def self.call(args)
private

def self.print_code(options)
if options.verbose
status_code = HTTP_STATUS_CODES[options.status_code.to_i]

if status_code
PrettyPrint.print(
"#{options.status_code} - #{status_code[:title]}",
options.status_code.to_s[0]
)
else
PrettyPrint.print(
"#{options.status_code} is not a valid code. See 'hscode --help'.",
"5"
)
end
end
end
Expand Down
21 changes: 3 additions & 18 deletions lib/hscode/input_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,10 @@ def display_version_number(opts)

def print_all_codes
HTTP_STATUS_CODES.map do |code, description|
if code.to_s.start_with? "1"
code_description(:info, code, description)
elsif code.to_s.start_with? "2"
code_description(:success, code, description)
elsif code.to_s.start_with? "3"
code_description(:neutral, code, description)
elsif code.to_s.start_with? "4"
code_description(:warning, code, description)
elsif code.to_s.start_with? "5"
code_description(:error, code, description)
end
colour_code = code.to_s[0]
PrettyPrint.print("#{code} - #{description[:title]}", colour_code)
end
end

def code_description(type, code, description)
puts PrettyPrint.make_pretty(
"#{code} - #{description[:title]}",
COLORS[type]
)
exit
end
end # class OptionsParser
end
22 changes: 14 additions & 8 deletions lib/hscode/pretty_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
# encoding: UTF-8

module Hscode
COLORS = {
info: 6,
success: 2,
neutral: 3,
error: 1,
warning: 9
}

class PrettyPrint
COLORS = {
"1" => 6,
"2" => 2,
"3" => 3,
"4" => 9,
"5" => 1
}

def self.print(text, colour_code)
puts make_pretty(text, COLORS[colour_code])
end

private

def self.make_pretty(text, color_code)
"\x1b[38;5;#{color_code}m#{text}\n"
end
Expand Down

0 comments on commit 1931ed8

Please sign in to comment.