class IpHistory < ActiveRecord::Base

  belongs_to :element, :polymorphic => true
  belongs_to :annonce_account
  
  after_create :generate_infos
  
  def generate_infos
    begin  
      
        http_url = "http://sideplace.com:8080/json/#{self.remote_ip}"
        puts "on tente #{http_url}"
            http = Curl::Easy.http_get(http_url)
            #http = Curl.get("https://freegeoip.lwan.ws/json/#{self.remote_ip}")
            result = JSON.parse(http.body_str)
            puts result
    
             if result
               self.city = result["city"]
               self.country = result["country_code"]
               self.latitude = result["latitude"]
               self.longitude = result["longitude"]
             end
            self.save
      
    rescue
      puts "timeout geoip : #{self.remote_ip}"
    end
  end

end
