fips_lookup 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +14 -0
  4. data/CODE_OF_CONDUCT.md +84 -0
  5. data/Gemfile +14 -0
  6. data/Gemfile.lock +69 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +198 -0
  9. data/Rakefile +12 -0
  10. data/lib/data/county/AK.csv +29 -0
  11. data/lib/data/county/AL.csv +67 -0
  12. data/lib/data/county/AR.csv +75 -0
  13. data/lib/data/county/AS.csv +5 -0
  14. data/lib/data/county/AZ.csv +15 -0
  15. data/lib/data/county/CA.csv +58 -0
  16. data/lib/data/county/CO.csv +64 -0
  17. data/lib/data/county/CT.csv +8 -0
  18. data/lib/data/county/DC.csv +1 -0
  19. data/lib/data/county/DE.csv +3 -0
  20. data/lib/data/county/FL.csv +67 -0
  21. data/lib/data/county/GA.csv +159 -0
  22. data/lib/data/county/GU.csv +1 -0
  23. data/lib/data/county/HI.csv +5 -0
  24. data/lib/data/county/IA.csv +99 -0
  25. data/lib/data/county/ID.csv +44 -0
  26. data/lib/data/county/IL.csv +102 -0
  27. data/lib/data/county/IN.csv +92 -0
  28. data/lib/data/county/KS.csv +105 -0
  29. data/lib/data/county/KY.csv +120 -0
  30. data/lib/data/county/LA.csv +64 -0
  31. data/lib/data/county/MA.csv +14 -0
  32. data/lib/data/county/MD.csv +24 -0
  33. data/lib/data/county/ME.csv +16 -0
  34. data/lib/data/county/MI.csv +83 -0
  35. data/lib/data/county/MN.csv +87 -0
  36. data/lib/data/county/MO.csv +115 -0
  37. data/lib/data/county/MP.csv +4 -0
  38. data/lib/data/county/MS.csv +82 -0
  39. data/lib/data/county/MT.csv +56 -0
  40. data/lib/data/county/NC.csv +100 -0
  41. data/lib/data/county/ND.csv +53 -0
  42. data/lib/data/county/NE.csv +93 -0
  43. data/lib/data/county/NH.csv +10 -0
  44. data/lib/data/county/NJ.csv +21 -0
  45. data/lib/data/county/NM.csv +33 -0
  46. data/lib/data/county/NV.csv +17 -0
  47. data/lib/data/county/NY.csv +62 -0
  48. data/lib/data/county/OH.csv +88 -0
  49. data/lib/data/county/OK.csv +77 -0
  50. data/lib/data/county/OR.csv +36 -0
  51. data/lib/data/county/PA.csv +67 -0
  52. data/lib/data/county/PR.csv +78 -0
  53. data/lib/data/county/RI.csv +5 -0
  54. data/lib/data/county/SC.csv +46 -0
  55. data/lib/data/county/SD.csv +66 -0
  56. data/lib/data/county/TN.csv +95 -0
  57. data/lib/data/county/TX.csv +254 -0
  58. data/lib/data/county/UM.csv +1 -0
  59. data/lib/data/county/UT.csv +29 -0
  60. data/lib/data/county/VA.csv +134 -0
  61. data/lib/data/county/VI.csv +3 -0
  62. data/lib/data/county/VT.csv +14 -0
  63. data/lib/data/county/WA.csv +39 -0
  64. data/lib/data/county/WI.csv +72 -0
  65. data/lib/data/county/WV.csv +55 -0
  66. data/lib/data/county/WY.csv +23 -0
  67. data/lib/data/state.csv +57 -0
  68. data/lib/fips_lookup/version.rb +5 -0
  69. data/lib/fips_lookup.rb +91 -0
  70. data/sig/fips_lookup.rbs +4 -0
  71. metadata +168 -0
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "fips_lookup/version"
4
+ require "csv"
5
+
6
+ # worker for county, state, fips lookups
7
+ module FipsLookup
8
+ STATE_CODES = { "AL" => "01", "AK" => "02", "AZ" => "04", "AR" => "05", "CA" => "06", "CO" => "08",
9
+ "CT" => "09", "DE" => "10", "DC" => "11", "FL" => "12", "GA" => "13", "HI" => "15",
10
+ "ID" => "16", "IL" => "17", "IN" => "18", "IA" => "19", "KS" => "20", "KY" => "21",
11
+ "LA" => "22", "ME" => "23", "MD" => "24", "MA" => "25", "MI" => "26", "MN" => "27",
12
+ "MS" => "28", "MO" => "29", "MT" => "30", "NE" => "31", "NV" => "32", "NH" => "33",
13
+ "NJ" => "34", "NM" => "35", "NY" => "36", "NC" => "37", "ND" => "38", "OH" => "39",
14
+ "OK" => "40", "OR" => "41", "PA" => "42", "RI" => "44", "SC" => "45", "SD" => "46",
15
+ "TN" => "47", "TX" => "48", "UT" => "49", "VT" => "50", "VA" => "51", "WA" => "53",
16
+ "WV" => "54", "WI" => "55", "WY" => "56", "AS" => "60", "GU" => "66", "MP" => "69",
17
+ "PR" => "72", "UM" => "74", "VI" => "78"
18
+ }.freeze
19
+
20
+ class << self
21
+ attr_accessor :county_fips, :state_fips
22
+
23
+ def county(state_param:, county_name:, return_nil: false)
24
+ state_code = find_state_code(state_param: state_param, return_nil: return_nil)
25
+ return {} if state_code.nil?
26
+
27
+ lookup = [state_code, county_name.upcase]
28
+ @county_fips ||= {}
29
+ @county_fips[lookup] ||= county_lookup(state_code, county_name, return_nil)
30
+ end
31
+
32
+ def state(state_param:, return_nil: false)
33
+ @state_fips ||= {}
34
+ @state_fips[state_param] ||= state_lookup(state_param, return_nil)
35
+ end
36
+
37
+ def fips_county(fips:, return_nil: false)
38
+ unless fips.is_a?(String) && fips.length == 5
39
+ return_nil ? (return nil) : (raise StandardError, "FIPS input must be 5 digit string")
40
+ end
41
+
42
+ state_code = find_state_code(state_param: fips[0..1], return_nil: return_nil)
43
+ return nil if state_code.nil?
44
+
45
+ CSV.foreach(county_file(state_code: state_code)) do |county_row|
46
+ return [county_row[3], state_code] if county_row[2] == fips[2..4]
47
+ end
48
+
49
+ raise StandardError, "Could not find county with fips: #{fips[2..4]}, in: #{state_code}" unless return_nil
50
+ end
51
+
52
+ def find_state_code(state_param:, return_nil: false)
53
+ return state_param.upcase if STATE_CODES.key?(state_param.upcase)
54
+ return STATE_CODES.key(state_param) if STATE_CODES.value?(state_param)
55
+
56
+ state(state_param: state_param, return_nil: return_nil)[:code]
57
+ end
58
+
59
+ def county_file(state_code:)
60
+ file_path = "#{File.expand_path(__dir__)}/data/county/#{state_code}.csv"
61
+ file_path if File.exist?(file_path)
62
+ end
63
+
64
+ def state_file
65
+ "#{File.expand_path(__dir__)}/data/state.csv"
66
+ end
67
+
68
+ private
69
+
70
+ def county_lookup(state_code, county_name_param, return_nil)
71
+ CSV.foreach(county_file(state_code: state_code)) do |county_row|
72
+ # county_row => state_code (AL), state fips (01), county fips (001), name (Augtauga County), class code (H1)
73
+ if county_row[3].upcase == county_name_param.upcase
74
+ return {state_code: county_row[0], fips: (county_row[1] + county_row[2]), name: county_row[3], class_code: county_row[4] }
75
+ end
76
+ end
77
+ return_nil ? (return {}) : (raise StandardError, "No county found matching: #{county_name_param}" unless return_nil)
78
+ end
79
+
80
+ def state_lookup(state_param, return_nil = false)
81
+ CSV.foreach(state_file) do |state_row|
82
+ # state_row => state fips (01), state code (AL), state name (Alabama), ansi (01779775)
83
+ if state_row[0] == state_param || state_row[1] == state_param.upcase ||
84
+ state_row[2].upcase == state_param.upcase || state_row[3] == state_param
85
+ return { fips: state_row[0], code: state_row[1], name: state_row[2], ansi: state_row[3] }
86
+ end
87
+ end
88
+ return_nil ? (return {}) : (raise StandardError, "No state found matching: #{state_param}")
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,4 @@
1
+ module FipsLookup
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fips_lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Erik Barroso
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: csv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
69
+ description:
70
+ email:
71
+ - erikbarroso22@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - ".rubocop.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/data/county/AK.csv
85
+ - lib/data/county/AL.csv
86
+ - lib/data/county/AR.csv
87
+ - lib/data/county/AS.csv
88
+ - lib/data/county/AZ.csv
89
+ - lib/data/county/CA.csv
90
+ - lib/data/county/CO.csv
91
+ - lib/data/county/CT.csv
92
+ - lib/data/county/DC.csv
93
+ - lib/data/county/DE.csv
94
+ - lib/data/county/FL.csv
95
+ - lib/data/county/GA.csv
96
+ - lib/data/county/GU.csv
97
+ - lib/data/county/HI.csv
98
+ - lib/data/county/IA.csv
99
+ - lib/data/county/ID.csv
100
+ - lib/data/county/IL.csv
101
+ - lib/data/county/IN.csv
102
+ - lib/data/county/KS.csv
103
+ - lib/data/county/KY.csv
104
+ - lib/data/county/LA.csv
105
+ - lib/data/county/MA.csv
106
+ - lib/data/county/MD.csv
107
+ - lib/data/county/ME.csv
108
+ - lib/data/county/MI.csv
109
+ - lib/data/county/MN.csv
110
+ - lib/data/county/MO.csv
111
+ - lib/data/county/MP.csv
112
+ - lib/data/county/MS.csv
113
+ - lib/data/county/MT.csv
114
+ - lib/data/county/NC.csv
115
+ - lib/data/county/ND.csv
116
+ - lib/data/county/NE.csv
117
+ - lib/data/county/NH.csv
118
+ - lib/data/county/NJ.csv
119
+ - lib/data/county/NM.csv
120
+ - lib/data/county/NV.csv
121
+ - lib/data/county/NY.csv
122
+ - lib/data/county/OH.csv
123
+ - lib/data/county/OK.csv
124
+ - lib/data/county/OR.csv
125
+ - lib/data/county/PA.csv
126
+ - lib/data/county/PR.csv
127
+ - lib/data/county/RI.csv
128
+ - lib/data/county/SC.csv
129
+ - lib/data/county/SD.csv
130
+ - lib/data/county/TN.csv
131
+ - lib/data/county/TX.csv
132
+ - lib/data/county/UM.csv
133
+ - lib/data/county/UT.csv
134
+ - lib/data/county/VA.csv
135
+ - lib/data/county/VI.csv
136
+ - lib/data/county/VT.csv
137
+ - lib/data/county/WA.csv
138
+ - lib/data/county/WI.csv
139
+ - lib/data/county/WV.csv
140
+ - lib/data/county/WY.csv
141
+ - lib/data/state.csv
142
+ - lib/fips_lookup.rb
143
+ - lib/fips_lookup/version.rb
144
+ - sig/fips_lookup.rbs
145
+ homepage: https://github.com/3barroso/fips_lookup
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: 2.6.0
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubygems_version: 3.5.6
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Stores FIPS codes for US States and Counties
168
+ test_files: []