Sha256: 1525299ee63be9325b02c05df8ef7d0844fa9cb271255acd4f1572e4082b1eb7

Contents?: true

Size: 817 Bytes

Versions: 1

Compression:

Stored size: 817 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.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

1 entries across 1 versions & 1 rubygems

Version Path
apidae-0.2.9 app/models/apidae/town.rb