Skip to content

Instantly share code, notes, and snippets.

@zmilojko
Created January 20, 2021 07:51
Show Gist options
  • Save zmilojko/f208a6216a6fb40e08906234601b831a to your computer and use it in GitHub Desktop.
Save zmilojko/f208a6216a6fb40e08906234601b831a to your computer and use it in GitHub Desktop.
Posti postcode fetch and parse
class PostcodesRequest
require 'open-uri'
require 'nokogiri'
def postcode_file_name
page = Nokogiri::HTML(open('http://www.posti.fi/webpcode/unzip/'))
href = page.css('p').css('a')
text = href.text
splits = text.split('webpcode/')
names = splits[1].split('.dat')
"#{names.detect { |name| name =~ /PCF/ }}.dat"
end
def dat_file_split_by_lines
url = "http://www.posti.fi/webpcode/unzip/#{postcode_file_name}"
URI.open(url, 'r:ISO-8859-1:UTF-8') do |io|
io.read.each_line
end
end
def fetch_and_insert_into_db
dat_file_split_by_lines.each do |row|
args = { postcode: row[13, 5],
city_fi: row[18, 30].strip.capitalize,
city_sv: row[48, 30].strip.capitalize,
province_fi: row[116, 30].strip,
province_sv: row[146, 30].strip,
town_fi: row[179, 20].strip,
town_sv: row[199, 20].strip }
Postcode.update(args)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment