<commit_before># namespace :db do
#   desc "import graetzl from geojson"
#   task import_graetzl: :environment do
#     Graetzl.destroy_all
#     geojson = File.read(Rails.root+"public/graetzl.json")
#     feature_collection = JSON.parse(geojson)['features']

#     feature_collection.each do |feature|
#       polygon = RGeo::GeoJSON.decode(feature['geometry'], :json_parser => :json)
#       name = feature['properties']['name']
#       Graetzl.create(name: name, area: polygon).save!
#     end
#   end
# end

require 'httparty'

namespace :db do
  desc 'import graetzl from mapbox api'
  task import_graetzl: :environment do
    api_response = query_api
    if api_response
      parse_features(api_response)
    else
      puts 'Cannot import districts from api'
    end
  end

  def query_api    
    access_token = '<KEY>'
    map_id = 'peckomingo.lb8m2cga'
    query = "http://api.tiles.mapbox.com/v4/#{map_id}/features.json?access_token=#{access_token}"
    uri = URI.parse(URI.encode(query))
    begin
      HTTParty.get(uri)
    rescue
      nil
    end
  end

  def parse_features(geojson)
    features = JSON.parse(geojson)['features']
    features.each do |feature|
      polygon = RGeo::GeoJSON.decode(feature['geometry'], :json_parser => :json)
      name = feature['properties']['name']
      name = name.partition(',').first if name
      graetzl = Graetzl.find_or_create_by(name: name, area: polygon) if polygon.geometry_type.type_name == 'Polygon'
    end<commit_msg>change import_graetzl<commit_after># namespace :db do
#   desc "import graetzl from geojson"
#   task import_graetzl: :environment do
#     Graetzl.destroy_all
#     geojson = File.read(Rails.root+"public/graetzl.json")
#     feature_collection = JSON.parse(geojson)['features']

#     feature_collection.each do |feature|
#       polygon = RGeo::GeoJSON.decode(feature['geometry'], :json_parser => :json)
#       name = feature['properties']['name']
#       Graetzl.create(name: name, area: polygon).save!
#     end
#   end
# end

require 'httparty'

namespace :db do
  desc 'import graetzl from mapbox api'
  task import_graetzl: :environment do
    api_response = query_api
    if api_response
      parse_features(api_response)
    else
      puts 'Cannot import districts from api'
    end
  end

  def query_api    
    access_token = '<KEY>'
    map_id = 'peckomingo.lb8m2cga'
    query = "http://api.tiles.mapbox.com/v4/#{map_id}/features.json?access_token=#{access_token}"
    uri = URI.parse(URI.encode(query))
    begin
      HTTParty.get(uri)
    rescue
      nil
    end
  end

  def parse_features(geojson)
    features = JSON.parse(geojson)['features']
    features.each do |feature|
      if feature['geometry'].present?
        polygon = RGeo::GeoJSON.decode(feature['geometry'], :json_parser => :json)
        name = feature['properties']['name']
        name = name.partition(',').first if name
        graetzl = Graetzl.find_or_create_by(name: name, area: polygon) if polygon.geometry_type.type_name == 'Polygon'
      end
    end
{'fraction_non_alphanumeric': 0.12384716732542819, 'fraction_numerical': 0.001976284584980237, 'mean_word_length': 3.388728323699422, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '906064', 'n_tokens_mistral': 1016, 'n_tokens_neox': 1005, 'n_words': 215}