Sha256: 01be0976dc152596461050a10cc323a82c5526f629b3e5ef54cd82316d4b9037

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

module GeonamesRails
  module Writers
    class ActiveRecord
      
      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,
                                             :continent,
                                             :geonames_id)
        c.save!
        
        "#{created_or_updating} db record for #{iso_code}"
      end
      

      
      def write_cities(country_code, city_mappings)    
        country = Country.find_by_iso_code_two_letter(country_code)
        
        #puts country
        #puts city_mappings
        
        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,
                                               :latitude,
                                               :longitude,
                                               :country_iso_code_two_letters,
                                               :geonames_timezone_id)
        
          city.save!
        end
        
        "Processed #{country.name}(#{country_code}) with #{city_mappings.length} cities"
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
geonames_rails-0.1.3 lib/geonames_rails/writers/active_record.rb
geonames_rails-0.1.2 lib/geonames_rails/writers/active_record.rb
geonames_rails-0.1.1 lib/geonames_rails/writers/active_record.rb