Today I’ve encountered a small problem with Geokit:

Geokit::Geocoders::IpGeocoder.do_geocode('31.174.47.24')
Psych::SyntaxError: (<unknown>): invalid trailing UTF-8 octet at line 1 column 1

I don’t have time (because it happened on production) to investigate this issue, so I’ve developed a quick temporary fix, which just removes invalid characters:

# Temporary workaround for crazy not UTF-8 characters in response
module Geokit
  module Geocoders
    class IpGeocoder
      require 'iconv'

      def self.parse_body(body)
        yaml = YAML.load(Iconv.conv('ASCII//IGNORE', 'UTF8', body))
        res = GeoLoc.new
        res.provider = 'hostip'
        res.city, res.state = yaml['City'].split(', ')
        country, res.country_code = yaml['Country'].split(' (')
        res.lat = yaml['Latitude']
        res.lng = yaml['Longitude']
        res.country_code.chop!
        res.success = !(res.city =~ /\(.+\)/)
        res
      end
    end
  end
end