Class: HttpCodesFetcher::Httpcodes

Inherits:
Object
  • Object
show all
Defined in:
lib/http_codes/http_codes_fetcher.rb

Instance Method Summary collapse

Constructor Details

#initializeHttpcodes

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_listObject



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.message
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.message
end

#sizeObject



39
40
41
# File 'lib/http_codes/http_codes_fetcher.rb', line 39

def size
  @list.size
end