class RandomUsCity
This class returns a struct with data for a random city in the US.
For example:
#<struct RandomUsCity::City zip="67878", latitude="37.986428", longitude="-101.751732", city="Syracuse", state="KS", county="Hamilton">
Constants
- City
- DATA_FIELDS
- VERSION
Public Class Methods
new()
click to toggle source
# File lib/random_us_city.rb, line 24 def initialize # read in our city data... @cities = [] data_file = File.join(path_to_resources, "cities.csv") CSV.foreach(data_file, :headers => true) do |row| @cities << City.new(*( row.map do |r| if r[0] == 'longitude' || r[0] == 'latitude' r[1].to_f else r[1] end end )) end end
Public Instance Methods
random_city()
click to toggle source
Returns a struct containing data for a random US city.
# File lib/random_us_city.rb, line 44 def random_city return @cities.sample end
Private Instance Methods
path_to_resources()
click to toggle source
# File lib/random_us_city.rb, line 49 def path_to_resources File.join(File.dirname(File.expand_path(__FILE__)), '../data') end