Giter Club home page Giter Club logo

cities's Introduction

Cities

This repo is archived โ€“ no longer maintained. Please fork if you need it!

All the cities of the world (according to the periodically updated MaxMind database).

Data

To use this gem, you'll need the JSON data.

Download it, extract it and connect it up as follows.

$ wget https://s3-us-west-2.amazonaws.com/cities-gem/cities.tar.gz
$ tar -xzf cities.tar.gz
Cities.data_path = '/path/to/cities'

Usage

Countries are identified by their ISO 3166-1 alpha-2 codes.

cities = Cities.cities_in_country('GB')
  #=> { "abberley"=> #<City:0x000001049b9ba0>, "abberton"=> #<City:0x000001049b9b50>, ... }

mcr = cities['manchester']
  #=> #<City:0x00000102fb4ea8>

mcr.name
  #=> "Manchester"

mcr.population
  #=> 395516

mcr.latlong
  #=> [53,5, -2.216667]

The database is exhaustive and certainly stretches the definition of the word "city".

Cities.cities_in_country('GB')['buchlyvie'].population
  #=> 448

Countries

This gem was designed as an extension to the Countries gem. Installing Cities adds two new instance methods to the Country class.

us = Country.search('US')
  #=> #<Country:0x000001020cf5f0>

us.cities?
  #=> true

us.cities
  #=> { "abanda" => #<Cities::City:0x00000114b34a38>, ...  }

Caching

Sometimes you may want to not constantly want to read and parse the same large JSON data files. So by default we cache in memory the parsed JSON. To turn this off simply set the cache_data flag to false

Country.cache_data = false

Configuration options available are data_path and cache_data

Country.configure do |config|
  config.data_path = '../data/cities'
  config.cache_data = false
end

Specs

The default path for data is the following path

GEM_ROOT/data/cities

Or you can set the environment variable DATA_PATH

To run the specs bundle and run specs

=> bundle
=> rake

Credits

Provided under an MIT license by Joe Corcoran. Thanks to hexorx for the countries gem that brought this idea about.

This product includes data created by MaxMind, available from http://www.maxmind.com. All data ยฉ 2008 MaxMind Inc. Read the MaxMind WorldCities open data license for more details.

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.