<commit_before>require 'mongo'

class ToiletDocs

  def initialize(toilet_details)
    @toilet_details = toilet_details

    client = ::Mongo::MongoClient.new('localhost', 27017)
    @collection = client.db('toilets_for_the_disabled').collection('toilets')
  end

  def save
    query = @toilet_details.query
    puts @collection.update(query, {"$setOnInsert" => @toilet_details.to_hash}, {upsert: true})
    geocode
  end

  protected
  
    def geocode
      query = @toilet_details.query
      doc = @collection.find_one(query)
      unless doc['location']
        result = Geocoder.coordinates(doc['address'], params: {countrycodes: "de"})
        if result
          puts "geocoded:"
          puts @collection.update(query, {"$set" => location_fields(result)})
        end
      end
    end

    def location_fields(result_object)
      {
        "location" => {
          "lat" => result_object.latitude,
          "lng" => result_object.longitude,
          "burrough" => result_object.address_components_of_type(:sublocality),
          "postal_code" => result_object.postal_code
        }
      }
    end
end<commit_msg>Geocoder#search works better for us than #coordinates<commit_after>require 'mongo'

class ToiletDocs

  def initialize(toilet_details)
    @toilet_details = toilet_details

    client = ::Mongo::MongoClient.new('localhost', 27017)
    @collection = client.db('toilets_for_the_disabled').collection('toilets')
  end

  def save
    query = @toilet_details.query
    puts @collection.update(query, {"$setOnInsert" => @toilet_details.to_hash}, {upsert: true})
    geocode
  end

  protected
  
    def geocode
      query = @toilet_details.query
      doc = @collection.find_one(query)
      unless doc['location']
        result = Geocoder.search(doc['address'], params: {countrycodes: "de"}).first
        if result
          puts "geocoded:"
          puts @collection.update(query, {"$set" => location_fields(result)})
        end
      end
    end

    def location_fields(result_object)
      {
        "location" => {
          "lat" => result_object.latitude,
          "lng" => result_object.longitude,
          "burrough" => result_object.address_components_of_type(:sublocality),
          "postal_code" => result_object.postal_code
        }
      }
    end
end
{'fraction_non_alphanumeric': 0.11690569317687961, 'fraction_numerical': 0.004345936549326379, 'mean_word_length': 2.9149659863945576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5351480', 'n_tokens_mistral': 749, 'n_tokens_neox': 734, 'n_words': 133}