+ redirect_to "/index.html?mlat=#{lat}&mlon=#{lon}&zoom=14"
+ return
+ end
+ elsif postcode.match(/(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})
+ /)
+ #its a UK postcode
+ begin
+ Net::HTTP.start('www.freethepostcode.org') do |http|
+ resp = http.get("/geocode?postcode=#{postcode}")
+ lat = resp.body.scan(/[4-6][0-9]\.?[0-9]+/)
+ lon = resp.body.scan(/[-+][0-9]\.?[0-9]+/)
+ redirect_to "/index.html?mlat=#{lat}&mlon=#{lon}&zoom=14"
+ return
+ end
+ rescue
+ redirect_to "/index.html"
+ #redirect to somewhere else
+ end
+ redirect_to "/index.html?mlat=#{lat}&mlon=#{lon}&zoom=14"
+ #redirect_to "/index.html?error=unknown_postcode_or_zip"
+ elsif
+ # Some other postcode / zip code
+ # Throw it at geonames, and see if they have any luck with it
+ Net::HTTP.start('ws.geonames.org') do |http|
+ resp = http.get("/postalCodeSearch?postalcode=#{escaped_postcode}&maxRows=1")
+ hits = resp.body.slice(/totalResultsCount>.*?</).split(/[<>]/)[1]
+ if hits == "0"
+ # Geonames doesn't know, it's probably wrong
+ redirect_to "/index.html?error=unknown_postcode_or_zip"
+ return
+ end
+ data_lat = resp.body.slice(/lat>.*?</)
+ data_lon = resp.body.slice(/lng>.*?</)
+ lat = data_lat.split(/[<>]/)[1]
+ lon = data_lon.split(/[<>]/)[1]
+ redirect_to "/index.html?mlat=#{lat}&mlon=#{lon}&zoom=14"