census_for 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/data/2014-census-data.csv +3221 -0
  3. data/lib/census_for.rb +126 -0
  4. metadata +118 -0
data/lib/census_for.rb ADDED
@@ -0,0 +1,126 @@
1
+ require 'smarter_csv'
2
+
3
+ class CensusFor
4
+
5
+ VERSION = "0.1.2"
6
+
7
+ STATES =
8
+ {
9
+ ak: "alaska", al: "alabama", ar: "arkansas", az: "arizona",
10
+ as: "american samoa",
11
+ ca: "california", co: "colorado", ct: "connecticut",
12
+ dc: "district of columbia",
13
+ de: "delaware",
14
+ fl: "florida",
15
+ ga: "georgia",
16
+ gu: "guam",
17
+ hi: "hawaii",
18
+ ia: "iowa", id: "idaho", il: "illinois", in: "indiana",
19
+ ks: "kansas", ky: "kentucky",
20
+ la: "louisiana",
21
+ ma: "massachusetts", md: "maryland", me: "maine", mi: "michigan", mn: "minnesota", mo: "missouri", ms: "mississippi", mt: "montana",
22
+ nc: "north carolina", nd: "north dakota", ne: "nebraska", nh: "new hampshire", nj: "new jersey", nm: "new mexico", nv: "nevada", ny: "new york",
23
+ oh: "ohio", ok: "oklahoma", or: "oregon",
24
+ pa: "pennsylvania",
25
+ pr: "puerto rico",
26
+ ri: "rhode island",
27
+ sc: "south carolina", sd: "south dakota", tn: "tennessee", tx: "texas",
28
+ ut: "utah",
29
+ va: "virginia", vt: "vermont",
30
+ vi: "virgin islands",
31
+ wa: "washingington", wi: "wisconsin", wv: "west virginia", wy: "wyoming"
32
+ }
33
+
34
+ class CensusData
35
+ def self.data
36
+ @@data ||= load_data
37
+ end
38
+
39
+ def self.load_data
40
+ SmarterCSV.process(File.dirname(__FILE__)+"/../data/2014-census-data.csv")
41
+ end
42
+ end
43
+
44
+ class County
45
+ def self.population(request)
46
+ parsed_request = parse_county_state(request)
47
+ return population_lookup(parsed_request)
48
+ end
49
+
50
+ def self.parse_county_state(county_state)
51
+ transit = county_state.downcase.split(/[\s,]+/) - ["county"] - ["parish"] - ["borough"] - ["municipio"] - ["municipality"]
52
+ if transit.size >= 3
53
+ result = []
54
+ 1.upto((transit.size - 1)) do |x|
55
+ y = transit.size
56
+ first = transit.take(x).join(' ')
57
+ second = transit.last(y-x).join(' ')
58
+ result << [first, second].flatten
59
+ end
60
+ return result
61
+ else
62
+ return [transit]
63
+ end
64
+ end
65
+
66
+ def self.population_lookup(county_state)
67
+ county_state.each do |cs|
68
+ county_name = cs.first
69
+ state_name = cs.last
70
+ state = Abbrev.converter(state_name)
71
+ result = CensusData.data.find { |x| x[:"geo.display_label"] ==
72
+ "#{county_name.split.map(&:capitalize).join(' ')} County, #{state}" || x[:"geo.display_label"] == "#{county_name.split.map(&:capitalize).join(' ')} Parish, Louisiana" || x[:"geo.display_label"] == "#{county_name.split.map(&:capitalize).join(' ')} Municipio, Puerto Rico" }
73
+ if result
74
+ return result[:respop72014]
75
+ end
76
+ end
77
+ return "not found" #preceding each loop matched nothing from query
78
+ end
79
+
80
+ def self.coeff(county_state)
81
+ coefficient = (population(county_state) * 1000 / highest_county_pop).to_f
82
+ coefficient < 1 ? 1 : coefficient
83
+ end
84
+
85
+ def self.highest_county_pop
86
+ most_populous_county = CensusData.data.max_by { |x| x[:respop72014] }
87
+ most_populous_county[:respop72014]
88
+ #for 2014, this is Los Angeles County, CA: pop 10,116,705
89
+ end
90
+ end
91
+
92
+ class State
93
+ def self.population(request)
94
+ state = Abbrev.converter(request)
95
+ return population_lookup(state)
96
+ end
97
+
98
+ def self.population_lookup(state)
99
+ if state
100
+ counties_in_state = []
101
+
102
+ CensusData.data.each do |x|
103
+ counties_in_state << x if x[:"geo.display_label"].split(/\s*,\s*/).last == "#{state}"
104
+ end
105
+
106
+ counties_pop_total = 0
107
+ counties_in_state.each do |c|
108
+ counties_pop_total += c[:respop72014]
109
+ end
110
+ return counties_pop_total
111
+ else 'not found'
112
+ end
113
+ end
114
+ end
115
+
116
+ class Abbrev
117
+ def self.converter(abbrev)
118
+ if STATES.has_value?(abbrev.downcase)
119
+ return abbrev.split.map(&:capitalize).join(' ')
120
+ elsif STATES.has_key?(abbrev.downcase.to_sym)
121
+ return STATES[abbrev.downcase.to_sym].split.map(&:capitalize).join(' ')
122
+ else return nil
123
+ end
124
+ end
125
+ end
126
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: census_for
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Evan Koch
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: smarter_csv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Gem returns population data for US States, Counties, and a linear ranking/weight
84
+ coefficient of US counties by population.
85
+ email:
86
+ - evankoch@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - data/2014-census-data.csv
92
+ - lib/census_for.rb
93
+ homepage: https://github.com/evo21/census_for
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ - data
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.5
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Quickly retrieve US census data for counties/states.
118
+ test_files: []