Class: GeonamesRails::Writers::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/geonames_rails/writers/active_record.rb

Instance Method Summary collapse

Instance Method Details

#write_cities(country_code, city_mappings) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/geonames_rails/writers/active_record.rb', line 48

def write_cities(country_code, city_mappings)    
  country = Country.find_by_iso_code_two_letter(country_code)
  
  unless country.nil?
    city_mappings.each do |city_mapping|
      city = City.find_or_initialize_by_geonames_id(city_mapping[:geonames_id])
      city.country_id = country.id
    
      city.attributes = city_mapping.slice(:name,
                                           :ascii_name,
                                           :alternate_name,
                                           :latitude,
                                           :longitude,
                                           :country_iso_code_two_letters,
                                           :population,
                                           :geonames_timezone_id,
                                           :geonames_id,
                                           :admin_1_code,
                                           :admin_2_code,
                                           :admin_3_code,
                                           :admin_4_code)

      division = Division.find_by_full_code(city.country_iso_code_two_letters+"."+city.admin_1_code )
      city.division_id = division.id unless division.blank?
    
      city.save!
    end
    
    "Processed #{country.name}(#{country_code}) with #{city_mappings.length} cities"
  end
end

#write_country(country_mapping) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/geonames_rails/writers/active_record.rb', line 5

def write_country(country_mapping)  
  iso_code = country_mapping[:iso_code_two_letter]
  c = Country.find_or_initialize_by_iso_code_two_letter(iso_code)

  created_or_updating = c.new_record? ? 'Creating' : 'Updating'
  
  c.attributes = country_mapping.slice(:iso_code_two_letter,
                                       :iso_code_three_letter,
                                       :iso_number,
                                       :name,
                                       :capital,
                                       :population,
                                       :continent,
                                       :currency_name,
                                       :currency_code,
                                       :geonames_id,
                                       :population,
                                       :phone)
  c.phone = "+"+c.phone if c.phone !~ /^\+/
  c.save!
  
  "#{created_or_updating} db record for #{iso_code}"
end

#write_division(division_mapping) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/geonames_rails/writers/active_record.rb', line 29

def write_division(division_mapping)  
  iso_code = division_mapping[:full_code][0..1]
  country = Country.find_by_iso_code_two_letter(iso_code)

  division = Division.find_or_initialize_by_geonames_id(division_mapping[:geonames_id])
  created_or_updating = division.new_record? ? 'Creating' : 'Updating'
  division.country_id = country.id
  division.code = division_mapping[:full_code][3..-1]

  division.attributes = division_mapping.slice(:full_code,
                                               :name,
                                               :ascii_name,
                                               :geonames_id)
  division.save!
  
  "#{created_or_updating} db record for #{division_mapping[:full_code]}"
end