Rails: Identify a city by IP Address

Source: Internet
Author: User
Tags geoip

1. For rails, the mainstream method should be to use the Google library plug-in geoip

GitHub address: https://github.com/simplificator/geoip

 

Require 'geoip' geoip. new ('geolitecity. dat '). country ('www. atlanta. SK ') => ["www. atlanta. SK "," 217.67.18.26 "," SK "," SVK "," Slovakia "," EU "," 02 "," Bratislava "," ", 48.15, 17.1167, nil, nil, "Europe/Bratislava"] returned values are the requested hostname, the IP address as a dotted quad, maxmind's country code, the ISO3166-1 country code, the ISO3166-2 country code, the iso00006 country name, and the continent code. geoip. new ('geocity. dat '). city ('github. com ') => ["github.com", "207.97.227.239", "us", "USA", "United States", "Na", "ca", "San Francisco ", "94110", 37.7484,-122.4156, 807,415, "America/los_angeles"] returned values are the country values followed by region or State name, city name, postal_code/zipcode, latitude, longpolling, usa dma code, USA area code, timezone name. sorry it's not a hash... historical. geoip. new ('geoipasnum. dat '). asn ("www.fsb.ru") => ["as8342", "rtcomm. ru Autonomous System "]

 

2. Another geo_ip

Before using it, you need to register a user in the http://ipinfodb.com/and get your own key when activating it. You need to use your own key to access the interface of the website.

GitHub address: https://github.com/jeroenj/geo_ip

 
#209.85.227.104 = Google. be (US) geoip. geolocation ('2017. 85.227.104 ') # returns: {: Status => "OK",: IP => "209.85.227.104": country_code => "us",: country_name => "United States ",: region_code => "06",: region_name => "California",: City => "Mountain View",: zip_postal_code => "94043",: latitude => "37.4192 ",: longpolling => "-122.057 "}

 

Geokit (http://geokit.rubyforge.org/) is a geographic tool, such as determining the city and distance based on latitude and longitude, can be combined with geo_ip for geographic analysis

 
# Find near latitude and longpolling: store. find (: All,: Origin => [37.792,-122.393],: Within => 10) # Find near an address: store. find (: All,: Origin => '1970 spear St, San Francisco, ca',: Within => 10) # order by distance from the center of a zipcode: store. find (: All,: Origin => '20140901',: Within => 10,: Order => 'distance ASC ') # combine distance conditions with regular conditions store. find (: All,: Origin => '20140901',: Within => 10,: conditions = >{: store_type => 'cafe '})

 

3. querying the network IP address API

In this way, the IP database is updated quickly. There are several common libraries, such as Google.

XML processing pages can be completely replaced by specialized processing tools such as nokogiri.

Many APIs provide IP address query, such as Netease:

1. http://www.youdao.com/smartresult-xml/search.s? Type = IP & Q = IP Address

2. http://ip2loc.appspot.com/# contains detailed examples, but the gwf address may not be resolved

3. Sina IP address query interface: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = JS

4. Sina multi-region Test method: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = JS & IP = 218.192.3.42

# This is comprehensive, but some IP addresses are not resolved correctly. Do not update a database ....

5. Sohu IP address query interface (default GBK): http://pv.sohu.com/cityjson

6. Sohu IP address query interface (can be set encoding): http://pv.sohu.com/cityjson? Ie = UTF-8

7. Sohu another IP address query interface: http://txt.go.sohu.com/ip/soip

 

Example:

Require "open-Uri" require 'rubygems 'require 'json' # If a GET request parameter is included in the URI address, uri = 'HTTP: // int.dpool.sina.com.cn/iplookup/iplookup.php? Format = JSON & IP = 218.192.3.42 'response = nilopen (URI) Do | HTTP | response = http. readend # response = iconv. conv ("gb2312", "UTF-8", response) @ res = JSON: parse (response) P @ res

Reference: http://hlee.iteye.com/blog/745533

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.