Program.in

By Santosh Wadghule

Archive for the ‘Ruby’ Category

IP address location in Ruby and Rails…

with 3 comments

Here,  I have made code that converts the IP address to the location using IP location tools. Code parses the XML file of IP Address Location and returns the string that contains location  of IP Address.  Code is below…

require 'net/http'
require 'rexml/document'
include REXML

class MapsController < ApplicationController
	def index
		@location = locateIp()

	end

	def locateIp
		#ip = "123.123.123.123";
		ip = request.remote_ip
		ips = ip.to_s
		url = "http://iplocationtools.com/ip_query.php?ip="+ips

		xml_data = Net::HTTP.get_response(URI.parse(url)).body

                xmldoc = REXML::Document.new(xml_data)

		# Now get the root element
		root = xmldoc.root
		city = ""
		regionName = ""
		countryName = ""

		# This will take country name...
		xmldoc.elements.each("Response/CountryName") {
		|e| countryName << e.text
	    }

		# Now get city name...
		xmldoc.elements.each("Response/City") {
   		|e| city << e.text
	    }

		# This will take regionName...
		xmldoc.elements.each("Response/RegionName") {
   		|e| regionName << e.text
	    }

     	ipLocation = city +", "+regionName+", "+countryName

	 return ipLocation
   end #end of method locateIp

end

Written by Santosh Wadghule

April 11, 2009 at 7:49 pm

Posted in Ruby

Learnig Ruby…

without comments

I had decided that I will learn ruby language in this new year, but can’t wait. So I started to learn ruby. For that first I want basic knowledge about the ruby language,  so I have downloaded pdf of ” The little book of Ruby” . It is very simple to learn and understand. It covered the all basic concept. My next project will be in ruby language.

Written by Santosh Wadghule

December 25, 2008 at 7:36 am

Posted in Ruby