Sha256: c31f39d02eb99b405bc9ed3011c4349ac4e4cd6805323690c85e755e44c0bb87

Contents?: true

Size: 875 Bytes

Versions: 5

Compression:

Stored size: 875 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 && (Time.current > (maximum(:updated_at) + 1.day))
        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.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

5 entries across 5 versions & 1 rubygems

Version Path
apidae-0.9.8 app/models/apidae/town.rb
apidae-0.9.7 app/models/apidae/town.rb
apidae-0.9.6 app/models/apidae/town.rb
apidae-0.9.5 app/models/apidae/town.rb
apidae-0.9.4 app/models/apidae/town.rb