Simple ruby client for the GeoNames API to get free and easy geographic info.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib bump version Oct 17, 2014
spec Support for marshalled objects: if consumers use a marshalled objects… Aug 10, 2014
Gemfile Go fly Nov 1, 2012

GeoNames API

Build Status Code Climate

This is a lightweight client for the GeoNames API. Huge thanks to them for such a great service!

There are many GeoNames API clients. BUT, most are rewritten versions of a Java API whose interface is a little funny =|

This is a simplified ruby implementation that does not implement the entire API. But, its lightweight and has a nice interface and will be easy to extend :)

Getting Started

$ gem install geonames_api



GeoNamesAPI uses conventions similar (but not exactly the same) to Rails' ActiveRecord. If you add any features, please stick to find and all as the core query methods.

The data is converted into the type implied by the content by default. If that isn't sufficient, let's create something more explicit for the exceptions.

The GeoNamesAPI classes include getters and setters that are the GeoNames API field names converted dynamically to more rubyish names using Rails' ActiveSupport underscore method. If the API ever changes, the attributes of the classes will change.

There are some extra methods in most of the key classes. Documentation is included below.


The gem is setup to use the demo user account by default. You'll need to provide your own credentials for anything but a simple set of test searches.

> GeoNamesAPI.username = ""
> GeoNamesAPI.lang = :en

For a complete list of the config options, take a look at the geonames_api.rb file.


The geonames api uses the ISO code of the country as its natural key.

> GeoNamesAPI::Country.find("US")

=> #<GeoNamesAPI::Country:0x007fd43503dfc0
      @country_name="United States",
      @continent_name="North America",

> GeoNamesAPI::Country.all

=> ... array of GeoNamesAPI::Country instances

The postal code exports are also available for each country either in string of ruby CSV form.

# String
> GeoNamesAPI::Country.find("US").postal_code_export
# CSV::Table
> GeoNamesAPI::Country.find("US").postal_code_csv


The GeoNamesAPI uses the latitude and longitude of the place as the parameters for its weather service.

> weather = GeoNamesAPI::Weather.find(41.88,-87.68)

=> #<GeoNamesAPI::Weather:0x007fab1c80dc10
      @clouds="few clouds",
      @observation="KMDW 012051Z 28009KT 10SM FEW049 BKN070 11/M02 A2983 RMK AO2 SLP105 T01111022 55010",
      @station_name="Chicago, Chicago Midway Airport",
      @datetime="2012-11-01 20:51:00",

The following instance methods are also available:

> weather.geo_names_api_country
> weather.geo_names_api_time_zone
> weather.time_zone
> weather.at_local
> weather.at_utc
> weather.elevation_feet
> weather.elevation_meters
# fahrenheit and celcius versions of temps
> weather.temperature_f
> weather.temperature_c
> weather.dew_point_f
> weather.dew_point_c


The GeoNamesAPI uses the latitude and longitude of the place as the parameters for its time zone service.

> time_zone = GeoNamesAPI::TimeZone.find(41.88,-87.68)

=> #<GeoNamesAPI::TimeZone:0x007fd4b24d7e00
      @time="2012-11-02 12:04",
      @country_name="United States",
      @sunset="2012-11-02 17:42",
      @sunrise="2012-11-02 07:25",

# ActiveSupport::TimeZone instance
> time_zone.time_zone
# Local and UTC version of each time
> time_zone.sunset_local
> time_zone.sunset_utc
> time_zone.sunrise_local
> time_zone.sunrise_utc
> time_zone.time_local
> time_zone.time_utc


The GeoNamesAPI uses the latitude and longitude of the place as the parameters for its Wikipedia artical service.

Please note the use of the all method with GeoNamesAPI::Wikipedia. It returns all of the articles that match. The find method will return the first matching article.

> articles = GeoNamesAPI::Wikipedia.all(41.88,-87.68)
=> [#<GeoNamesAPI::Wikipedia:0x007fd4b24e6040
        @summary="Richard T. Crane Technical Preparatory High School, commonly known as Crane Tech Prep or Crane Tech High School, is a public high school in Chicago, Illinois. It is located at 2245 West Jackson Boulevard in Chicago's Near West Side community area.  (...)",
        @title="Crane High School (Chicago, Illinois)",


Feel free to contribute! Let's keep the style consistent.


The gem was originally written by @barelyknown.

A number of nice features were added by @mrm including:

  • automatic retries on timeout,
  • properly encoded url parameters,
  • support for paid users (with https and API tokens)
  • more consistent find and where methods across endpoints
  • Timezone, AlternateName, and GeoName entries properly encoded in results
  • better test coverage, Travis CI, and CodeClimate integration

To contribute:

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
  6. Thanks <3