Sha256: 3a557168db0046e48e68652a718fddfbe4ec3cb75f4688ba05f90488ee7ec8a2

Contents?: true

Size: 904 Bytes

Versions: 2

Compression:

Stored size: 904 Bytes

Contents

module Apidae
  class Town < ActiveRecord::Base
    def self.import(towns_json)
      towns_hashes = JSON.parse(towns_json, symbolize_names: true)
      if towns_hashes.length != count
        countries = Hash[Reference.where(apidae_type: "Pays").map {|ref| [ref.apidae_id, ref.label(:fr)]}]
        towns_hashes.each do |town_data|
          town = Town.find_or_initialize_by(apidae_id: town_data[:id])
          town.name = town_data[:nom]
          town.postal_code = town_data[:codePostal]
          town.insee_code = town_data[:code]
          town.country = countries[town_data[:pays][:id]]
          town.description = town_data[:complement]
          town.save!
        end
      end
    end

    def self.import_file(json_file)
      towns_json = File.read(json_file)
      import(towns_json)
    end

    def label
      "#{name} (#{postal_code})"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
apidae-0.7.5 app/models/apidae/town.rb
apidae-0.7.4 app/models/apidae/town.rb