Rubyでは、どのようにCSVファイルを解析して情報を出力できますか?たとえば:特定のIPアドレスのCSVファイルの解析
require 'csv'
class CountryFilter
class << self
def find_specs_by_ip_address(ip)
CSV.foreach('GeoIPCountry.csv') do |row|
if row =~ Regexp.union(ip)
data = row.split(',')
return data[5]
else
return 'Unable to find country specifications'
end
end
end
end
end
puts CountryFilter.find_specs_by_ip_address('72.247.167.255')
CSVファイル:
...
"72.247.88.0","72.247.89.255","1224169472","1224169983","US","United States"
"72.247.90.0","72.247.103.255","1224169984","1224173567","NL","Netherlands"
"72.247.104.0","72.247.144.255","1224173568","1224184063","US","United States"
"72.247.145.0","72.247.145.255","1224184064","1224184319","NL","Netherlands"
"72.247.146.0","72.247.167.255","1224184320","1224189951","US","United States"
"72.247.168.0","72.247.179.255","1224189952","1224193023","NL","Netherlands"
"72.247.180.0","72.247.181.255","1224193024","1224193535","US","United States"
"72.247.182.0","72.247.182.255","1224193536","1224193791","NL","Netherlands"
"72.247.183.0","72.247.183.255","1224193792","1224194047","US","United States"
...
にはどうすればIPアドレスおよび出力のため、このCSVファイル、それが由来する国を解析することができますか?
最初の2つの列は実際にはIP範囲を表していると思います。 – Felix