Class: Ekylibre::CultivableZonesJsonExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/cultivable_zones_json_exchanger.rb

Overview

Import a GeoJSON file (as FeatureCollection) with `name` and `number` properties for each feature.

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, exchanger_name, export, exporters, find, human_name, import, import!, importers, importers_selection, inherited, #initialize, register_exchanger

Constructor Details

This class inherits a constructor from ActiveExchanger::Base

Instance Method Details

#importObject

def check

valid = true
clusters = JSON.parse(file.read).deep_symbolize_keys
unless clusters[:type] == 'FeatureCollection'
  w.error 'Invalid format'
  valid = false
end
valid

end


15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/exchangers/ekylibre/cultivable_zones_json_exchanger.rb', line 15

def import
  clusters = JSON.parse(file.read)
  if clusters['type'] == 'FeatureCollection'
    clusters['features'].each do |feature|
      properties = feature['properties']
      shape = ::Charta.from_geojson(feature)
      CultivableZone.create!(properties.slice('name', 'work_number').merge(shape: shape))
    end
  else
    raise ActiveExchanger::NotWellFormedFileError, 'File seems to be JSON but not GeoJSON.'
  end
end