Hi everybody,
I was trying to create a rake task to import geo data by accessing
geonmae API's xml data , I wanna create a list of countries , cities ,
sub cities that r related to each other through nested list , anyways, I
have a problem with the recursion i made , i didn't get it , I'll list
the code hopefully i will get some help from any expert here around ,
Any help will b highly appreciated .

----------------------------------------------------------
require File.dirname(__FILE__) + '/../config/boot'
require "#{RAILS_ROOT}/config/environment"
conf = YAML::load(File.open(File.dirname(__FILE__) +
'/../config/database.yml'))
ActiveRecord::Base.establish_connection(conf['development'])


module Geonames

        @@langs={}
        Language.all.each{|Lan| @@langs[Lan.iso_639_1] = Lan.id}
        @@continent = {'AF' => 6255146}#, 'AS' => 6255147, 'EU' =>
6255148, 'NA' => 6255149 , 'OC' => 6255151,'SA' => 6255150,'AN' =>
6255152}

        def self.import
           puts "lets start the game"
           self.import_continents
           self.import_countries_and_sub
        end


        def self.import_continents
              puts '*** add globe to tables'
              self.add_new_tag({:tag_type =>1,:status => 0, :user_id =>
666666},{:content => 'Globe', :language =>@@langs['en'], :primary =>
true},{})

              puts '*** add continent  tables'
              @@continent.each_key{|x,y| self.add_new_tag({:tag_type
=>1,:status => 0, :user_id => 666666, :sub_type =>0},{:content => x,
:language =>@@langs['en'], :primary =>true}, {:parent_id =>
Region.find_by_word_name('Globe').id} )}
        end



        def self.import_countries_and_sub

            @@continent.each do |cont_name,cont_geo_id|
               puts "importing countiries in #{cont_name} continents"
               @@cont_id = Region.find_by_name(cont_name)
               self.do_nasty_stuff(cont_geo_id, @@cont_id, 1)
            end

        end



     def self.add_new_tag(tag_options={}, word_options={},
region_options={}, alt={})
        # not gonna list all the code since u don't have all the time to
read it :) anyways this function for adding the geo as a tag and save it
with different language and different spelling as non primary words for
this tag

        end

        def self.do_nasty_stuff(geo_id, prt_id, sub_type)
          # the xml data of that data given by geo id number
          @@baba_xml =
Hpricot.parse(open("http://ws.geonames.org/children?geonameId=#{geo_id}&style=full";))
          p "***********************     importing xml of #{geo_id}
*********************"

             (@@baba_xml/:geoname).each do |thing|

                lat = CGI.escapeHTML(thing.at(:lat).children.first.to_s)
                lng = CGI.escapeHTML(thing.at(:lng).children.first.to_s)

                # add the main country
                extra_info = {:lat => lat, :lng => lng }

                # add the sub alternative names as non primary words
                alt_hash = Hash.new
                thing.search(:alternatename).each do |alt|
                  alt_hash[alt.children.first.to_s] =
@@langs[alt[:lang]]
                end

                # forget about the parameters here , it has nothing with
the problem
                self.add_new_tag({:tag_type =>1,:status => 0, :user_id
=> 666666, :sub_type =>sub_type},{:content => name, :language
=>@@langs['en'], :primary =>true}, {:parent_id => prt_id, :data =>
extra_info} ,alt_hash)
                # HERE IS WHERE I NEED UR HELP
                if  ((sub_type.to_i < 3))
                  prt_id = Region.find_by_word_name(name)
                  geo_id =
CGI.escapeHTML(thing.at(:geonameid).children.first.to_s)
                  p " do nasty stuff #{geo_id} #{prt_id} , #{sub_type}"
                  self.do_nasty_stuff(geo_id, prt_id, sub_type + 1)
                end

             end

          end


end

----------------------------------------------------------------------
in do_nasty_stuff -> if  ((sub_type.to_i < 2)) , I don't know why but
the returning data is not accurate , as 4 example , in Africa , the
first loop in working perfectly but then it will list Angola for example
as child of the previous country (algeria) ...


Thnx
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to