Class: PeoplePlacesThings::ANSICounties

Inherits:
Object
  • Object
show all
Defined in:
lib/people_places_things/ansi_counties.rb

Overview

Provides two-way mapping between U.S. state and county names and their associated ANSI codes (formerly known as FIPS codes).

Examples

To get the ANSI code for a state and county, you call ANSICounties.code_for, like so:

code = ANSICounties.code_for('GA', 'FULTON')
# => 13121

You can also pass a single Hash argument:

code = ANSICounties.code_for(:state => 'ga', :county => 'fulton')
# => 13121

Conversely, to get the state and county for an ANSI code, you call ANSICounties.data_for:

hash = ANSICounties.data_for(13121)
# => { :state => 'GA', :county => 'FULTON' }

Data source

The data that makes up lib/ansi-counties/data/data.yml was generated from lib/ansi-counties/data/raw.txt, which was downloaded from the US Census website.

Class Method Summary (collapse)

Class Method Details

+ (Object) code_for(data_or_state, county = nil)

Get the ANSI code for the given state and county. If data_or_state is a Hash, then it must contain state and county keys, otherwise, it is assumbed to be a String containing the state name.



31
32
33
34
35
36
37
38
39
# File 'lib/people_places_things/ansi_counties.rb', line 31

def self.code_for(data_or_state, county=nil)
  if data_or_state.is_a?(Hash)
    state, county = data_or_state[:state], data_or_state[:county]
  else
    state = data_or_state
  end

  forward_hash[key_for(state, county)] rescue nil
end

+ (Object) data_for(code)

Get the state and county names for a given ANSI code. Returns a Hash containing state and county keys



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

def self.data_for(code)
  reverse_hash[code]
end

+ (Object) key_for(state, county)

:nodoc:



50
51
52
# File 'lib/people_places_things/ansi_counties.rb', line 50

def self.key_for(state, county)  #:nodoc:
  "#{state.upcase}/#{normalize_county_name(county)}"
end

+ (Object) normalize_county_name(name)

:nodoc:



46
47
48
# File 'lib/people_places_things/ansi_counties.rb', line 46

def self.normalize_county_name(name)  #:nodoc:
  name.upcase.gsub("ST ", "ST. ").gsub("SAINT ", "ST. ")
end