ip2country v0.1.3

IP(v4) address to country name converter for Crystal


IP(v4) address to country name converter for Crystal.


Add this to your application's shard.yml:

    github: arcage/ip2country


require "ip2country"

# Create a converter object.
ip2country = IP2Country.new

# Country name lookup by IP address string.
ip2country.lookup("", "en") # => "United States"
ip2country.lookup("", "fr") # => "États-Unis"
ip2country.lookup("", "es") # => "Estados Unidos"
ip2country.lookup("", "de") # => "Vereinigte Staaten"
ip2country.lookup("", "ja") # => "アメリカ合衆国"
ip2country.lookup("", "zh") # => "美国"
ip2country.lookup("", "ko") # => "미국"
ip2country.lookup("")       # => "United States"(English is default)

# You can specify the default language.
ip2country = IP2Country.new("ja")
ip2country.lookup("")       # => "アメリカ合衆国"

# Get a hash of country names in all supported languages
# => {"de" => "Vereinigte Staaten", "en" => "United States", "es" => "Estados Unidos", "fr" => "États-Unis", "ja" => "アメリカ合衆国", "ko" => "미국", "pt" => "Estados Unidos", "zh" => "美国"}

At the first time you use, this library will fetch some conversion tables, and cache them. This may take a while.

When you want to update cache of tables, you can call IP2Country.cache_update explicitly.

Notice: You should not call it every time you use this library.

Special thanks

This library uses conversion tables(ISO 3166-1 alpha-2 country codes to country names) provided by umpirsky/country-list © Saša Stamenković umpirsky@gmail.com(license is here).

It supports many kinds of file formats(CSV, JSON, YAML, etc...) and many kinds of languages.

I sincerely respect this great work.


  • arcage ʕ·ᴥ·ʔAKJ - creator, maintainer

Change Logs

ver. 0.1.3

  • support crystal 0.25.0

ver. 0.1.2

  • Replace as keyword to #as(TYPE) method for crystal 0.19.0
  • Add minimum specs
  • Fix integrating address range

ver. 0.1.1

  • Add type declaration of instance variables for crystal 0.16.0

ver. 0.1.0

  • First commit
Github statistic:
  • 7
  • 1
  • 2
  • 0
  • 2 months ago


MIT License