aboutsummaryrefslogtreecommitdiff
path: root/lib/tasks/import.rake
blob: 6fedbe2348ef7a5a0f1b80d7bc1b1596f2a255e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
namespace :import do
  desc "Check doc/import for resources to import against existing database"
  task check: :environment do
    begin
      features = JSON.parse(IO.read(File.join(Rails.root, 'doc/import/Locations.geojson')), { symbolize_names: false })['features']
    rescue Exception => e
      p e
      $stderr.puts "Missing Locations.geojson. Did you run the parser.rb first?"
      exit(1)
    end

    # Make a first pass to see if we have existing records

    existing_records = {}
    i = 0 # temp
    known_records = new_records = 0


    features.each do |feature|
      i+= 1
      name = feature['name']
      phone_number = feature['phone_number']
      email = feature['email']
      website = feature['website']

      unless name.blank?
        r0 = Resource.where("feature #>> '{properties,name}' = :name", name: name)
        $stderr.puts "No existing resource with name '%s'" % name if r0.blank?
      end

      unless phone_number.blank?
        r1 = Resource.where("feature #>> '{properties,phone_number}' = :num", num: PhonyRails.normalize_number(phone_number, default_country_code: 'BE'))
        $stderr.puts "No existing resource with phone '%s'" % phone_number if r1.blank?
      end

      unless email.blank?
        r2 = Resource.where("feature #>> '{properties,email}' = :email", email: email)
        $stderr.puts "No existing resource with email = '%s'" % email if r2.blank?
      end

      unless website.blank?
        r3 = Resource.where("feature #>> '{properties,website}' = :url", url: website)
        $stderr.puts "No existing resource with website = '%s" % website if r3.blank?
      end

      unless r0.blank? && r1.blank? && r2.blank? && r3.blank?
        $stderr.print '!'
        known_records += 1
=begin
        $stderr.puts "Some potential resources found..."
        p r0
        p r1
        p r2
        p r3
=end
      else
        new_records += 1
        $stderr.print "."
      end

      # Normalize fields

      # stop at 3
      #i >= 3 && exit(0)
    end
    $stderr.puts "Task complete (known: #{known_records}, new: #{new_records}, total: #{features.count})"
  end

  desc "Import new resources"
  task import: :environment do
    dewey = Agent.find_by(name: 'Dewey')
    concertes = Agent.find_by(name: 'ConcertES')

    $stderr.puts "Resources: #{Resource.count}"

    begin
      features = JSON.parse(IO.read(File.join(Rails.root, 'doc/import/Locations.geojson')), { symbolize_names: false })
    rescue Exception => e
      p e
      $stderr.puts "Missing Locations.geojson. Did you run the parser.rb first?"
      exit(1)
    end

    $stderr.puts "Features: #{features['features'].count}"

    features['features'].each do |feat|
      ag = feat['properties']['source'] == 'ConcertES' ? concertes : dewey
      $stderr.puts "Creating Resource ( #{feat['properties']['name']} ) for #{ag.name}"
      r = Resource.new(feature: feat, agent_id: ag.id)
      p r
      p r.errors unless r.valid?
      exit unless r.save
      $stderr.puts "Created Resource #{r.uuid}"
    end
  end
end