Class: RandomUsCity

Inherits:
Object
  • Object
show all
Defined in:
lib/random_us_city.rb,
lib/random_us_city/version.rb

Overview

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">

Defined Under Namespace

Classes: City

Constant Summary collapse

DATA_FIELDS =
[:zip, :latitude, :longitude, :city, :state, :county]
VERSION =
"0.1.3"

Instance Method Summary collapse

Constructor Details

#initializeRandomUsCity

Returns a new instance of RandomUsCity.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/random_us_city.rb', line 22

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

Instance Method Details

#random_cityObject

Returns a struct containing data for a random US city.



42
43
44
# File 'lib/random_us_city.rb', line 42

def random_city
  return @cities.sample
end