Giter Club home page Giter Club logo

clj-geoip's Introduction

clj-geoip

Build Status

clj-geoip is a thin Clojure layer on top of the MaxMind GeoIP Java API. It allows you to query information like the country, city or network provider of a given IP. Have a look at the usage section for an example.

"This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com/."

Preparation

To use clj-geoip you first need to download the newest version of the free GeoIP data files. To do so you can use the download script UpdateGeoIpFiles.sh provided in the scripts folder. It simply downloads the newest archives and extracts them into the resources folder.

MaxMind provides new versions of the data files on a monthly basis. So it's a good idea to run the script every now and then.

Furthermore you have to compile the MaxMind Java classes first. Just run the Leiningen javac command to do so.

> lein javac

Usage

This API is pretty simple, just have a look at the following code:

user> (use 'clj-geoip.core)
nil
user> (geoip-init)
true
user> (use 'clojure.pprint)
nil
user> (pprint (lookup "87.152.91.74"))
{:countryName "Germany",
 :area-code 0,
 :asn "AS3320 Deutsche Telekom AG",
 :longitude 7.399994,
 :postalCode nil,
 :latitude 50.983307,
 :city "Engelskirchen",
 :metro-code 0,
 :region "07",
 :countryCode "DE",
 :dma-code 0,
 :ip "87.152.91.74"}
nil
user> (geoip-close)
true

Use geoip-init and geoip-close to start and stop the service and lookup to lookup information about the given IP.

The data files are expected to be in the resources folder but it's possible to bind the locations in the clj-geoip.core/*dbs* symbol to a new value.

Ring Handler

You can use the provided ring handler to add location information to the request map. Here is a Noir example:

(use 'clj-geoip.handler)
(add-middleware #'geoip-handler)
(defpage "/" []
    (str (:location (ring-request))))
;; -> {:countryName "United States", :area-code 650, :longitude -122.0574, :postalCode "94043", :latitude 37.419205, :city "Mountain View", :metro-code 807, :region "CA", :countryCode "US", :dma-code 807, :asn "AS15169 Google Inc.", :dip "209.85.148.100"}

Dependencies

This library can be used as dependency in your leiningen project:

[clj-geoip "0.1"]

TODO

  • Pass through of LookupService modes.
  • Ring handler to inject location information into the request map.
  • Is the geoip-close method really necessary?
  • Add IPv6 functions.
  • Add function to calculate the distance between two IPs.
  • Noir test application on Heroku.

License

Copyright (C) 2012

Distributed under the Eclipse Public License, the same as Clojure.

clj-geoip's People

Contributors

mcav avatar philc avatar

Watchers

 avatar  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.