# GeoNames API [![Build Status](https://secure.travis-ci.org/buytruckload/geonames_api.png?branch=master)](http://travis-ci.org/buytruckload/geonames_api) [![Code Climate](https://codeclimate.com/repos/52a4b4c513d63712180028d4/badges/33cf2af8b28c4cb66098/gpa.png)](https://codeclimate.com/repos/52a4b4c513d63712180028d4/feed) This is a lightweight client for the [GeoNames](http://www.geonames.org) 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 ## Usage ### FYI 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. ### Configuration 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 = "yourusername@yourdomain.com" > GeoNamesAPI.lang = :en For a complete list of the config options, take a look at the `geonames_api.rb` file. ### Country The geonames api uses the ISO code of the country as its natural key. > GeoNamesAPI::Country.find("US") => # > GeoNamesAPI::Country.all => ... array of GeoNamesAPI::Country instances The [postal code exports](http://download.geonames.org/export/zip/) 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 ### Weather 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) => # 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 ### TimeZone 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) => # # 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 ### Wikipedia 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) => [#, ...] ### Others Feel free to contribute! Let's keep the style consistent. ## Contributing The gem was originally written by [@barelyknown](http://twitter.com/barelyknown). A number of nice features were added by [@mrm](http://twitter.com/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