5
0
mirror of https://git.coop/cotech/website.git synced 2024-11-15 20:13:05 +00:00
cotech-website/scripts/geocode_coop_addresses.rb
2018-11-30 16:56:07 +00:00

22 lines
628 B
Ruby

require 'bundler/setup'
require 'yaml'
require 'geocoder'
Geocoder.configure(lookup: :nominatim)
root_path = Pathname.new(__dir__).join('..')
Dir[root_path.join('_coops/*.md')].each do |path|
content = File.read(path)
body = content.split("\n").reverse.take_while { |l| l != "---" }.reverse.join("\n")
metadata = YAML.safe_load(content)
address = metadata['address']
latitude, longitude = Geocoder.coordinates(address, params: { region: 'uk' })
metadata['latitude'] ||= latitude
metadata['longitude'] ||= longitude
frontmatter = YAML.dump(metadata)
File.write(path, frontmatter + "---" + "\n" + body)
end