module HttpHelp

Constants

VERSION

Public Class Methods

find(status) click to toggle source
# File lib/http_help.rb, line 9
def self.find status
  if status == "all"
    list_all_statuses
  elsif status.to_i > 0
    find_by_code status.to_i
  else
    find_by_description status
  end
end

Private Class Methods

find_by_code(status_code) click to toggle source
# File lib/http_help.rb, line 21
def self.find_by_code status_code
  status_codes.each do |category|
    category.each do |code|
      return code[status_code] unless code[status_code].nil?
    end
  end

  raise StatusCodeNotFoundException, "Status code not found."
end
find_by_description(status_description) click to toggle source
# File lib/http_help.rb, line 31
def self.find_by_description status_description
  status_codes.keys.each do |category|
    status_codes[category].each_pair do |code, description|
      return "#{code}: #{description}" if description.downcase.levenshtein(status_description.downcase) < 3
    end
  end

  raise StatusNotFoundException, "Status not found."
end
list_all_statuses() click to toggle source
# File lib/http_help.rb, line 41
def self.list_all_statuses
  File.open(File.join(__dir__, 'status_codes.yml')) do |file|
    while line = file.gets
      puts line
    end
  end
end
status_codes() click to toggle source
# File lib/http_help.rb, line 49
def self.status_codes
  YAML::load_file(File.join(__dir__, 'status_codes.yml'))
end