Class: Apidae::Town

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/apidae/town.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import(towns_json) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/apidae/town.rb', line 3

def self.import(towns_json)
  towns_hashes = JSON.parse(towns_json, symbolize_names: true)
  if count == 0 || 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.description = town_data[:complement]
      town.save!
    end
    Town.first.touch
  end
end

.import_file(json_file) ⇒ Object



20
21
22
23
# File 'app/models/apidae/town.rb', line 20

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

Instance Method Details

#labelObject



25
26
27
# File 'app/models/apidae/town.rb', line 25

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