Reputation: 41
I want to Write a command line program that accepts an IP address, and returns the country it's associate with.
For ex: 67.99.163.76 will output 'United States'.
Upvotes: 1
Views: 151
Reputation: 3431
You can use the geocode gem. It's very feature rich and have great support for rails and other rack-based frameworks. You can of cause also use it outside those frameworks both as an api and from the console. A small sample from the console:
geocode 67.99.163.76
Latitude: 42.7684
Longitude: -78.8871
Full address: Buffalo, NY 14260, United States
City: Buffalo
State/province: New York
Postal code: 14260
Country: United States
Google map: http://maps.google.com/maps?q=42.7684,-78.8871
You can do it using nothing but the standard library by querying freegeoip:
require 'net/http'
require 'json'
ip = "67.99.163.76"
uri = URI.parse("http://freegeoip.net/json/#{ip}")
client = Net::HTTP.new(uri.host, uri.port)
res = JSON.parse(client.get(uri.request_uri).body)
puts res["country_name"]
Upvotes: 1