Giter Site home page Giter Site logo

geoip2's Introduction

Ruby Maxmind GeoIP2 Bindings

Build Status

Description

Searches city by IP address in local database from maxmind.com.

It can be used in any standalone Ruby app or in Lotus or in Rails app.

Installation

libmaxminddb must be installed.

gem install maxmind_geoip2

Note: For MacOS users, you may need to pass the paths to libmaxminddb when installing the gem. E.g.

gem install maxmind_geoip2 -- --with-opt-include=/usr/local/include --with-opt-lib=/usr/local/lib

Usage

Configuration

MaxmindGeoIP2.file '<local_db_file.mmdb>' # default: GeoLite2-City.mmdb
MaxmindGeoIP2.locale 'ru' # default: 'ru'

You could place above codes into a initializer file in Rails, for example config/initializers/geoip2.rb.

Further usage:

Returns nil if nothing found and raises exception if file not opened or not found

city = MaxmindGeoIP2.locate(<ip address>, <optional lang>)

city = MaxmindGeoIP2.locate '77.93.127.33'
=> {"city"=>"Тамбов",
"city_geoname_id"=>484646,
"country"=>"Россия",
"country_geoname_id"=>2017370,
"country_code"=>"RU",
"continent"=>"Европа",
"continent_code"=>"EU",
"continent_geoname_id"=>6255148,
"subdivision"=>"Тамбовская область",
"subdivision_code"=>"TAM",
"postal_code"=>nil,
"latitude"=>52.731700000000004,
"longitude"=>41.4433,
"time_zone"=>"Europe/Moscow"}

city = MaxmindGeoIP2.locate '77.93.127.33', 'en'
=> {"city"=>"Tambov",
"city_geoname_id"=>484646,
"country"=>"Russia",
"country_geoname_id"=>2017370,
"country_code"=>"RU",
"continent"=>"Europe",
"continent_code"=>"EU",
"continent_geoname_id"=>6255148,
"subdivision"=>"Tambovskaya Oblast'",
"subdivision_code"=>"TAM",
"postal_code"=>nil,
"latitude"=>52.731700000000004,
"longitude"=>41.4433,
"time_zone"=>"Europe/Moscow"}

Testing

bundle exec rake

License

WTFPL

geoip2's People

Contributors

da4nik avatar dizzzyroma avatar filiptepper avatar jqr avatar kennethkalmer avatar leonardteo avatar stevehodgkiss avatar tilo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

geoip2's Issues

Locales as symbols

It would be very nice if the locale arguments (for GeoIP2::locate and GeoIP2::locale) could be either String or Symbol instances, since Rails' I18n uses symbols for locales, some examples:

GeoIP2::locate '88.162.113.13', :fr
GeoIP2::locale I18n.default_locale

And as a side note: It would be equally nice if the returned hash contained Symbol keys rather than String keys, like so:

GeoIP2::locate '88.162.113.13', 'en'
=> {:city=>"Les Angles",
 :city_geoname_id=>3002160,
 (...)

Locale fallback

The paid GeoIP city database from Maxmind often doesn't hold translations for cities. The IP address 88.162.113.13 is located in Les Angles, France. Here are the lookup results:

GeoIP2::locate '88.162.113.13', 'fr'
=> {"city"=>nil,
 "city_geoname_id"=>3002160,
 "country"=>"France",
 "country_geoname_id"=>3017382,
 "country_code"=>"FR",
 "continent"=>"Europe",
 "continent_code"=>"EU",
 "continent_geoname_id"=>6255148,
 "subdivision"=>"Région Languedoc-Roussillon",
 "subdivision_code"=>"K",
 "subdivision_geoname_id"=>3007670,
 "postal_code"=>"30133",
 "latitude"=>42.5788,
 "longitude"=>2.0739,
 "time_zone"=>"Europe/Paris"}

GeoIP2::locate '88.162.113.13', 'en'
=> {"city"=>"Les Angles",
 "city_geoname_id"=>3002160,
 "country"=>"France",
 "country_geoname_id"=>3017382,
 "country_code"=>"FR",
 "continent"=>"Europe",
 "continent_code"=>"EU",
 "continent_geoname_id"=>6255148,
 "subdivision"=>"Languedoc-Roussillon",
 "subdivision_code"=>"K",
 "subdivision_geoname_id"=>3007670,
 "postal_code"=>"30133",
 "latitude"=>42.5788,
 "longitude"=>2.0739,
 "time_zone"=>"Europe/Paris"}

As you see, no city name is returned when a French city is queried with locale "fr", however, it is returned with locale "en". The consumer of your gem can only deal with this situation by performing a second lookup. Not so good!

Would it be possible to add a fallback setting such as:

GeoIP2::fallback_locale 'en'

If set, a lookup which finds a city (city_geoname_id is present) but no city name in the queried locale returns the city name in the fallback locale.

Doesn't compile in Mac OS X

Can't compile the library. libmaxminddb 0.5.2 installed using brew.

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /Users/sashazykov/.rvm/rubies/ruby-1.9.3-p484/bin/ruby extconf.rb 
checking for maxminddb.h... yes
checking for main() in -lmaxminddb... yes
creating Makefile

make  clean

make
compiling geoip2.c
In file included from geoip2.c:1:
/usr/local/include/maxminddb.h:57: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mmdb_uint128_t’
/usr/local/include/maxminddb.h:87: error: expected specifier-qualifier-list before ‘mmdb_uint128_t’
geoip2.c: In function ‘lookup_path_parse’:
geoip2.c:21: warning: ISO C90 forbids mixed declarations and code
geoip2.c:25: warning: ISO C90 forbids mixed declarations and code
geoip2.c: In function ‘mGeoIP2_locate’:
geoip2.c:69: warning: ISO C90 forbids mixed declarations and code
geoip2.c:79: warning: ISO C90 forbids mixed declarations and code
geoip2.c:81: warning: ISO C90 forbids mixed declarations and code
geoip2.c:89: warning: ISO C90 forbids mixed declarations and code
geoip2.c:100: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:101: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:102: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:103: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:104: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:105: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:106: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:107: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
make: *** [geoip2.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/sashazykov/.rvm/gems/ruby-1.9.3-p484/bundler/gems/geoip2-66a00071db22 for inspection.
Results logged to /Users/sashazykov/.rvm/gems/ruby-1.9.3-p484/bundler/gems/extensions/x86_64-darwin-13/1.9.1/geoip2-0.0.8/gem_make.out
An error occurred while installing geoip2 (0.0.8), and Bundler cannot continue.

Build fails on OSX with Ruby 2.1 against libmaxminddb 0.5.3

/usr/local/opt/rbenv/versions/2.1.0/bin/ruby extconf.rb
checking for maxminddb.h... yes
checking for main() in -lmaxminddb... yes
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling geoip2.c
In file included from geoip2.c:1:
/usr/local/include/maxminddb.h:57: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mmdb_uint128_t’
/usr/local/include/maxminddb.h:87: error: expected specifier-qualifier-list before ‘mmdb_uint128_t’
geoip2.c: In function ‘locate_by_path’:
geoip2.c:43: warning: passing argument 3 of ‘MMDB_aget_value’ from incompatible pointer type
make: *** [geoip2.o] Error 1

make failed, exit code 2

I have zero C skills, but I'm trying to figure this out locally. If you have a quick solution I'd be very grateful.

unable to open file /kpdata/maxmind/GeoLite2-City.mmdb (IOError)

@Selenium
Scenario: Visit free landing page with default twitter message # features/premium_twitter_message.feature:32
Given redis is empty # features/step_definitions/weighted_tracks_step.rb:27
Given the following organization records # features/step_definitions/administration_step.rb:7
| name | parent | id |
| klickpush | | 10 |
| rewardops | klickpush | 20 |
Given the following theme records # features/step_definitions/free_step.rb:9
| name | main_color_hex | hover_color_hex | id |
| default | #ccddff | #ddeeff | 1 |
Given the following premium landing pages # features/step_definitions/premium_step.rb:1
| organization_id | url_key | theme_id | email | logo | name |
| 20 | exampleoi | 1 | 1 | 0 | fake |
Given the following track records # features/step_definitions/administration_step.rb:43
| name | artist | isrc | genre | organization_id | id | status |
| hashpipe | weezer | abcd1234 | rock | 20 | 10 | ACTIVE |
| rape me | nirvana | cbcd1234 | indie | 20 | 20 | ACTIVE |
Given the following track_organization_campaign records # features/step_definitions/administration_step.rb:28
| organization_id | track_id | start_date | end_date | id |
| 20 | 10 | yesterday | tomorrow | 19 |
| 20 | 20 | yesterday | tomorrow | 29 |
Given the following tags # features/step_definitions/taggable_step.rb:1
| organization_id | track_id | tag_class | tag_name |
| 20 | 10 | genres | rock |
| 20 | 20 | genres | rap |
And I visit the "exampleoi" premium landing page with the following parameters "tid=123&app=KLICKPUSH_PREMIUM_OLD" # features/step_definitions/premium_step.rb:11
And I wait 1 seconds # features/step_definitions/landings_page_step.rb:13
Then I should see a twitter message of "@klickpush Thanks for this awesome song by @klickpush #music" # features/step_definitions/free_facebook_share_step.rb:14
unable to open file /kpdata/maxmind/GeoLite2-City.mmdb (IOError)
./app/models/concerns/geo_ipable.rb:5:in locate' ./app/models/concerns/geo_ipable.rb:5:inset_user_geo_ip'
./app/controllers/premia_controller.rb:197:in record_ip' features/premium_twitter_message.feature:58:inThen I should see a twitter message of "@klickpush Thanks for this awesome song by @klickpush #music"'

Failed to compile under OSX 10.11

OS: 10.11
Libs: libmaxminddb (1.0.4) (from Homebrew)

Steps to reproduce:

  • Run gem install maxmind_geoip2 -v '0.0.8' -- --with-opt-include=/usr/local/include
  • Got errors, following is the output of mkmf.log:
/Users/tle/.rbenv/versions/2.2.2/bin/ruby -r ./siteconf20150626-96182-1rhbaha.rb extconf.rb --with-opt-include=/usr/local/include/
checking for maxminddb.h... yes
checking for main() in -lmaxminddb... no
you must have mmdb library
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/tle/.rbenv/versions/2.2.2/bin/$(RUBY_BASE_NAME)
    --with-maxminddblib
    --without-maxminddblib

extconf failed, exit code 1

Happy to pair with anyone who are willing to resolve this issue.

The libmaxminddb API has changed

We just merged a branch into master that makes major changes to the API. On the plus side, we now have docs, so it shouldn't be too hard to update your code to the new API.

On the downside, the API still isn't 100% stable. We'll probably be tweaking little things for at least a week or two. I don't anticipate major API changes but we may be doing things like changing structure internals, so you'll need to recompile even if you don't have to change your code.

Updated for refactored libmaxminddb

Hello,

I work at MaxMind and I just wanted to let you know that we will soon be releasing a refactored version of libmaxminddb. The changes are currently in the dave/major-refactor branch of the libmaxminddb repo. Many of the function and structure names were changed. This should be the last significant refactoring before we do a production release later this fall.

Thanks for making this extension!

Greg

Geoip2 0.0.8 / Ruby 2.0.0 Compile Issue

I didn't try to debug this yet, but since there was no "It works using this" config, I thought I'd share what immediately popped up for me during a bundle install.

Using geoip2 (0.0.8) from git://github.com/da4nik/geoip2.git (at master)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/usr/local/bin/ruby extconf.rb 

checking for maxminddb.h... yes
checking for main() in -lmaxminddb... yes
creating Makefile

make
compiling geoip2.c
In file included from /usr/local/include/ruby-2.0.0/ruby/ruby.h:1567,
from /usr/local/include/ruby-2.0.0/ruby.h:33,
from geoip2.c:3:
/usr/local/include/ruby-2.0.0/ruby/intern.h:323: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘rb_fdset_t’
/usr/local/include/ruby-2.0.0/ruby/intern.h:412: error: expected declaration specifiers or ‘...’ before ‘fd_set’
/usr/local/include/ruby-2.0.0/ruby/intern.h:412: error: expected declaration specifiers or ‘...’ before ‘fd_set’
/usr/local/include/ruby-2.0.0/ruby/intern.h:412: error: expected declaration specifiers or ‘...’ before ‘fd_set’
/usr/local/include/ruby-2.0.0/ruby/intern.h:412: warning: ‘struct timeval’ declared inside parameter list
/usr/local/include/ruby-2.0.0/ruby/intern.h:413: error: expected declaration specifiers or ‘...’ before ‘rb_fdset_t’
/usr/local/include/ruby-2.0.0/ruby/intern.h:413: error: expected declaration specifiers or ‘...’ before ‘rb_fdset_t’
/usr/local/include/ruby-2.0.0/ruby/intern.h:413: error: expected declaration specifiers or ‘...’ before ‘rb_fdset_t’
/usr/local/include/ruby-2.0.0/ruby/intern.h:413: warning: ‘struct timeval’ declared inside parameter list
/usr/local/include/ruby-2.0.0/ruby/intern.h:414: warning: ‘struct timeval’ declared inside parameter list
geoip2.c: In function ‘lookup_path_parse’:
geoip2.c:21: warning: ISO C90 forbids mixed declarations and code
geoip2.c:22: warning: implicit declaration of function ‘strdup’
geoip2.c:22: warning: incompatible implicit declaration of built-in function ‘strdup’
geoip2.c:25: warning: ISO C90 forbids mixed declarations and code
geoip2.c: In function ‘locate_by_path’:
geoip2.c:50: warning: implicit declaration of function ‘strndup’
geoip2.c:50: warning: incompatible implicit declaration of built-in function ‘strndup’
geoip2.c: In function ‘mGeoIP2_locate’:
geoip2.c:69: warning: ISO C90 forbids mixed declarations and code
geoip2.c:79: warning: ISO C90 forbids mixed declarations and code
geoip2.c:81: warning: ISO C90 forbids mixed declarations and code
geoip2.c:89: warning: ISO C90 forbids mixed declarations and code
geoip2.c:100: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:37: note: expected ‘char ’ but argument is of type ‘const char *’
geoip2.c:101: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:37: note: expected ‘char *’ but argument is of type ‘const char *’
geoip2.c:102: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:37: note: expected ‘char *’ but argument is of type ‘const char *’
geoip2.c:103: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:37: note: expected ‘char *’ but argument is of type ‘const char *’
geoip2.c:104: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:37: note: expected ‘char *’ but argument is of type ‘const char *’
geoip2.c:105: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:37: note: expected ‘char *’ but argument is of type ‘const char *’
geoip2.c:106: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:37: note: expected ‘char *’ but argument is of type ‘const char *’
geoip2.c:107: warning: passing argument 2 of ‘locate_by_path’ discards qualifiers from pointer target type
geoip2.c:37: note: expected ‘char *’ but argument is of type ‘const char *’
make: *
* [geoip2.o] Error 1

Gem files will remain installed in /home/user/.bundler/ruby/2.0.0/geoip2-66a00071db22 for inspection.
Results logged to /home/user/.bundler/ruby/2.0.0/geoip2-66a00071db22/./gem_make.out

An error occurred while installing geoip2 (0.0.8), and Bundler cannot continue.
Make sure that gem install geoip2 -v '0.0.8' succeeds before bundling.

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.