Class: HttpCodesFetcher::Httpcodes
- Inherits:
-
Object
- Object
- HttpCodesFetcher::Httpcodes
- Defined in:
- lib/http_codes/http_codes_fetcher.rb
Instance Method Summary collapse
- #code_list ⇒ Object
- #find_by_code(code) ⇒ Object
- #find_by_desc(desc) ⇒ Object
-
#initialize ⇒ Httpcodes
constructor
A new instance of Httpcodes.
- #size ⇒ Object
Constructor Details
#initialize ⇒ Httpcodes
Returns a new instance of Httpcodes.
7 8 9 |
# File 'lib/http_codes/http_codes_fetcher.rb', line 7 def initialize @list = code_list end |
Instance Method Details
#code_list ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/http_codes/http_codes_fetcher.rb', line 11 def code_list url = 'http://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv' codes = {} CSV.new(open(url), headers: :first_row).each do |line| codes[line['Description'].upcase] = line['Value'].to_i end codes end |
#find_by_code(code) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/http_codes/http_codes_fetcher.rb', line 20 def find_by_code(code) desc = @list.key(code) fail 'Code not found' unless @list.key?(desc) @list[desc].to_s + ' - ' + desc rescue => e e. end |
#find_by_desc(desc) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/http_codes/http_codes_fetcher.rb', line 28 def find_by_desc(desc) search_val = Regexp.new desc.upcase matches = @list.keys.grep search_val fail 'Description not found' unless matches.size > 0 matches.map do |k| @list[k].to_s + ' - ' + k.to_s end rescue => e e. end |
#size ⇒ Object
39 40 41 |
# File 'lib/http_codes/http_codes_fetcher.rb', line 39 def size @list.size end |