Code Monkey home page Code Monkey logo

rack-geoipcity's Introduction

Rack GeoIPCity

Rack::GeoIPCity uses the geoip gem and the GeoIP database to lookup the geographical info of a request by its IP address.
The database can be downloaded from:

(Lite version) http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz, instructions are here http://www.maxmind.com/app/geolitecity.

http://www.maxmind.com/app/city for full version.

NOTE! If you're using the country database you'll get a different struct returned, so use the GeoIPCountry gem, rack-geoipcountry. I'd make this middleware do both but:

a) It would be slower
b) there's already a country gem and
c) you can do the branching yourself if you really want, but why would you?

Usage:

use Rack::GeoIPCity, :db => "path/to/GeoIP.dat"

By default all requests are looked up and the X_GEOIP_* headers are added to the request. The headers can then be read in the application.
The country name is added to the request header as X_GEOIP_COUNTRY, eg:
X_GEOIP_COUNTRY: United Kingdom

The full set of GEOIP request headers is below:
X_GEOIP_COUNTRY_CODE - The ISO3166-1 two-character country code, if not found set to --
X_GEOIP_COUNTRY_CODE3 - The ISO3166-2 three-character country code, if not found set to --
X_GEOIP_COUNTRY - The ISO3166 English-language name of the country, if not found set to an empty string
X_GEOIP_CONTINENT if not found set to an empty string
X_GEOIP_REGION_NAME if not found set to an empty string
X_GEOIP_CITY_NAME if not found set to an empty string
X_GEOIP_POSTAL_CODE if not found set to an empty string
X_GEOIP_LATITUDE if not found won't be returned as I can't think of another good return value to signify "not found"
X_GEOIP_LONGITUDE if not found won't be returned as I can't think of another good return value to signify "not found"
X_GEOIP_DMA_CODE The metropolitan code (this is for the USA, see http://code.google.com/apis/adwords/docs/appendix/metrocodes.html if you're interested), default 0 for not found.
X_GEOIP_AREA_CODE if not found set to an empty string
X_GEOIP_TIMEZONE if not found set to an empty string
X_GEOIP_CONTINENT - The two-character continent code, if not found set to an empty string

You can use the included Mapping class to trigger lookup only for certain requests by specifying matching path prefix in options, eg:
use Rack::GeoIPCity::Mapping, :prefix => '/video_tracking'
The above will lookup IP addresses only for requests matching /video_tracking etc.

Running the specs and examples

You'll need a copy of the database, so download it and place it in:

  • examples/assets for the examples
  • spec/support/assets for the specs

License

MIT License -
Originally by Karol Hosiawa ( http://twitter.com/hosiawak )
Converted to a gem by Thomas Maurer
This one by Iain Barnett

rack-geoipcity's People

Stargazers

Andrea avatar Iain Barnett avatar Alan Garcia avatar

Watchers

Iain Barnett avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.