Module: FipsLookup
- Defined in:
- lib/fips_lookup.rb,
lib/fips_lookup/version.rb
Overview
worker for county, state, fips lookups
Constant Summary collapse
- STATE_CODES =
{ "AL" => "01", "AK" => "02", "AZ" => "04", "AR" => "05", "CA" => "06", "CO" => "08", "CT" => "09", "DE" => "10", "DC" => "11", "FL" => "12", "GA" => "13", "HI" => "15", "ID" => "16", "IL" => "17", "IN" => "18", "IA" => "19", "KS" => "20", "KY" => "21", "LA" => "22", "ME" => "23", "MD" => "24", "MA" => "25", "MI" => "26", "MN" => "27", "MS" => "28", "MO" => "29", "MT" => "30", "NE" => "31", "NV" => "32", "NH" => "33", "NJ" => "34", "NM" => "35", "NY" => "36", "NC" => "37", "ND" => "38", "OH" => "39", "OK" => "40", "OR" => "41", "PA" => "42", "RI" => "44", "SC" => "45", "SD" => "46", "TN" => "47", "TX" => "48", "UT" => "49", "VT" => "50", "VA" => "51", "WA" => "53", "WV" => "54", "WI" => "55", "WY" => "56", "AS" => "60", "GU" => "66", "MP" => "69", "PR" => "72", "UM" => "74", "VI" => "78" }.freeze
- VERSION =
"0.2.0"
Class Attribute Summary collapse
-
.county_fips ⇒ Object
Returns the value of attribute county_fips.
-
.state_fips ⇒ Object
Returns the value of attribute state_fips.
Class Method Summary collapse
- .county(state_param:, county_name:, return_nil: false) ⇒ Object
- .county_file(state_code:) ⇒ Object
- .find_state_code(state_param:, return_nil: false) ⇒ Object
- .fips_county(fips:, return_nil: false) ⇒ Object
- .state(state_param:, return_nil: false) ⇒ Object
- .state_file ⇒ Object
Class Attribute Details
.county_fips ⇒ Object
Returns the value of attribute county_fips.
21 22 23 |
# File 'lib/fips_lookup.rb', line 21 def county_fips @county_fips end |
.state_fips ⇒ Object
Returns the value of attribute state_fips.
21 22 23 |
# File 'lib/fips_lookup.rb', line 21 def state_fips @state_fips end |
Class Method Details
.county(state_param:, county_name:, return_nil: false) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/fips_lookup.rb', line 23 def county(state_param:, county_name:, return_nil: false) state_code = find_state_code(state_param: state_param, return_nil: return_nil) return {} if state_code.nil? lookup = [state_code, county_name.upcase] @county_fips ||= {} @county_fips[lookup] ||= county_lookup(state_code, county_name, return_nil) end |
.county_file(state_code:) ⇒ Object
59 60 61 62 |
# File 'lib/fips_lookup.rb', line 59 def county_file(state_code:) file_path = "#{File.expand_path(__dir__)}/data/county/#{state_code}.csv" file_path if File.exist?(file_path) end |
.find_state_code(state_param:, return_nil: false) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/fips_lookup.rb', line 52 def find_state_code(state_param:, return_nil: false) return state_param.upcase if STATE_CODES.key?(state_param.upcase) return STATE_CODES.key(state_param) if STATE_CODES.value?(state_param) state(state_param: state_param, return_nil: return_nil)[:code] end |
.fips_county(fips:, return_nil: false) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fips_lookup.rb', line 37 def fips_county(fips:, return_nil: false) unless fips.is_a?(String) && fips.length == 5 return_nil ? (return nil) : (raise StandardError, "FIPS input must be 5 digit string") end state_code = find_state_code(state_param: fips[0..1], return_nil: return_nil) return nil if state_code.nil? CSV.foreach(county_file(state_code: state_code)) do |county_row| return [county_row[3], state_code] if county_row[2] == fips[2..4] end raise StandardError, "Could not find county with fips: #{fips[2..4]}, in: #{state_code}" unless return_nil end |
.state(state_param:, return_nil: false) ⇒ Object
32 33 34 35 |
# File 'lib/fips_lookup.rb', line 32 def state(state_param:, return_nil: false) @state_fips ||= {} @state_fips[state_param] ||= state_lookup(state_param, return_nil) end |
.state_file ⇒ Object
64 65 66 |
# File 'lib/fips_lookup.rb', line 64 def state_file "#{File.expand_path(__dir__)}/data/state.csv" end |