census_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ pkg/*
15
+ rdoc
16
+ spec/reports
17
+ spec/vcr_cassettes
18
+ spec/vcr_cassettes/*
19
+ spec/api_key.rb
20
+ test/tmp
21
+ test/version_tmp
22
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in census_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,468 @@
1
+ # Census API
2
+
3
+ This gem provides a Ruby wrapper around the Census Bureau API.
4
+
5
+
6
+ ## Obtaining an API key
7
+
8
+ To be able to use this gem, you'll need a Census Bureau API key. To request an API key, visit
9
+ http://www.census.gov/developers/tos/key_request.html and follow the instructions.
10
+
11
+
12
+ ## Installing the gem
13
+
14
+ To use this gem, install it with <tt>gem install census_api</tt> or add it to your Gemfile:
15
+
16
+ `gem 'cemsus_api'`
17
+
18
+ And install it with <tt>bundle install</tt>
19
+
20
+ ## Usage
21
+
22
+ ### Retrieving Census Data
23
+
24
+ First register a new Client:
25
+
26
+ `@client = CensusApi::Client.new(API_KEY)`
27
+
28
+ To query the 2010 Census SF1 dataset:
29
+
30
+ `@client.sf1(fields, for, in*)`
31
+
32
+ To query the 2006-2010 ACS5 dataset:
33
+
34
+ `@client.acs5(fields, for, in*)`
35
+
36
+ For a list of the fields available for each dataset, review the reference PDFs linked at the bottom of this document.
37
+
38
+ #### Parameters
39
+
40
+ The 'fields' parameter is a comma separated string of SF1 field names.
41
+
42
+ The 'for' parameter is a single geography type, and optionally, the geography ids (geoid) for specific geographies of that type. A colon separates the geography type from the id and a comma separates ids.
43
+
44
+ For example, 'STATE:06,02' would reference the state of California and Alaska.
45
+
46
+ The 'in' parameter is a set of geography types or geography. Similar to the 'for' parameter, a colon separates the geography type from the id and a comma separates ids. Sets of geography are separated by a plus symbol.
47
+
48
+ For example, 'STATE:02+COUNTY:290' would reference the 'Yukon-Koyukuk Census Area' County in the state of Alaska.
49
+
50
+ The 'in' parameter is optional, or required, depending upon the geography type. The smaller the geography type being required, the more the request must be restricted by the 'in' parameter.
51
+
52
+ ## Census 2010 SF1 Examples and Supported Geography
53
+
54
+ #### STATE - *(040) state*
55
+
56
+ Retrieve fields for all States:
57
+
58
+ `@client.sf1('P0010001', 'STATE')`
59
+
60
+ Retrieve fields for California (geoid: 06):
61
+
62
+ `@client.sf1('P0010001', 'STATE:06')`
63
+
64
+ Retrieve fields for California and Alaska:
65
+
66
+ `@client.sf1('P0010001', 'STATE:06,02')`
67
+
68
+ #### COUNTY - *(050) state-county*
69
+
70
+ Retrieve fields for all Counties:
71
+
72
+ `@client.sf1('P0010001', 'COUNTY')`
73
+
74
+ Retrieve fields for Counties in California:
75
+
76
+ `@client.sf1('P0010001', 'COUNTY', 'STATE:06')`
77
+
78
+ Retrieve fields for a specific County in California:
79
+
80
+ `@client.sf1('P0010001', 'COUNTY:001', 'STATE:06')`
81
+
82
+ #### COUSUB - *(060) state-county-county subdivision*
83
+
84
+ Retrieve fields for all County Subdivisions within a specific County:
85
+
86
+ `@client.sf1('P0010001', 'COUSUB', 'STATE:02+COUNTY:290')`
87
+
88
+ Retrieve fields for a specific County Subdivision within a specific County:
89
+
90
+ `@client.sf1('P0010001', 'COUSUB:86690', 'STATE:02+COUNTY:290')`
91
+
92
+ Note: You must also specify the State the County belongs to.
93
+
94
+ #### SUBMCD - *(067) state-county-county subdivision-subminor civil subdivision*
95
+
96
+ Retrieve fields for all Subminor Civil Subdivisions within a specific County Subdivision:
97
+
98
+ `@client.sf1('P0010001', 'SUBMCD', 'STATE:72+COUNTY:127+COUSUB:79693')`
99
+
100
+ Retrieve fields for a specific Subminor Civil Subdivisions within a specific County Subdivision:
101
+
102
+ `@client.sf1('P0010001', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
103
+
104
+ #### TABBLOCK - *(101) state-county-tract-block*
105
+
106
+ Retrieve fields for all Blocks within a specific Tract
107
+
108
+ `@client.sf1('P0010001', 'TABBLOCK', 'STATE:02+COUNTY:290+TRACT:00100')`
109
+
110
+ Retrieve fields for a specific Subminor Civil Subdivisions within a specific County Subdivision:
111
+
112
+ `@client.sf1('P0010001', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
113
+
114
+ #### TRACT - *(140) state-county-tract*
115
+
116
+ Retrieve fields for all Tracts within a specific County:
117
+
118
+ `@client.sf1('P0010001', 'TRACT', 'STATE:02+COUNTY:170')`
119
+
120
+ Retrieve fields for a specific Tract within a specific County:
121
+
122
+ `@client.sf1('P0010001', 'TRACT:000101', 'STATE:02+COUNTY:170')`
123
+
124
+ #### BG - *(150) state-county- tract-block group*
125
+
126
+ Retrieve fields for all Block Groups within a specific Tract:
127
+
128
+ `@client.sf1('P0010001', 'BG', 'STATE:02+COUNTY:170+TRACT:000101')`
129
+
130
+ Retrieve fields for a specific Block Group within a specific Tract:
131
+
132
+ `@client.sf1('P0010001', 'BG:1', 'STATE:02+COUNTY:170+TRACT:000101')`
133
+
134
+ #### PLACE -*(160) state-place*
135
+
136
+ Retrieve fields for all Places:
137
+
138
+ `@client.sf1('P0010001', 'PLACE')`
139
+
140
+ Retrieve fields for all Places within a specific State:
141
+
142
+ `@client.sf1('P0010001', 'PLACE', 'STATE:06')`
143
+
144
+ Retrieve fields for a specific place within a specific State:
145
+
146
+ `@client.sf1('P0010001', 'PLACE:00135', 'STATE:06')`
147
+
148
+ #### ANRC - *(260) state-alaska native regional corporation*
149
+
150
+ Retrieve fields for all Alaska Native Regional Corporations:
151
+
152
+ `@client.sf1('P0010001', 'ANRC')`
153
+
154
+ Retrieve fields for all Alaska Native Regional Corporations within a specific State:
155
+
156
+ `@client.sf1('P0010001', 'ANRC', 'STATE:02')`
157
+
158
+ Retrieve fields for all Alaska Native Regional Corporations within a specific State:
159
+
160
+ `@client.sf1('P0010001', 'ANRC:00590', 'STATE:02')`
161
+
162
+ #### AIANNH - *(280) state-american indian area/alaska native area/hawaiian home land*
163
+
164
+ Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land:
165
+
166
+ `@client.sf1('P0010001', 'AIANNH')`
167
+
168
+ Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
169
+
170
+ `@client.sf1('P0010001', 'AIANNH', 'STATE:02')`
171
+
172
+ Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
173
+
174
+ `@client.sf1('P0010001', 'AIANNH:03800', 'STATE:02')`
175
+
176
+ #### AITS - *(281) state-american indian area-tribal subdivision*
177
+
178
+ Retrieve fields for all American Indian Area-Tribal Subdivisions:
179
+
180
+ `@client.sf1('P0010001', 'AITS')`
181
+
182
+ Retrieve fields for all American Indian Area-Tribal Subdivisions in a specific American Indian Area:
183
+
184
+ `@client.sf1('P0010001', 'AITS', 'STATE:40+AIANNH:13735')`
185
+
186
+ Retrieve fields for a specific American Indian Area-Tribal Subdivision in a specific American Indian Area:
187
+
188
+ `@client.sf1('P0010001', 'AITS:83127', 'STATE:40+AIANNH:13735')`
189
+
190
+ #### CBSA - *(320) state-metropolitan statistical area/micropolitan statistical area*
191
+
192
+ Retrieve fields from all Metropolitan Statistical Areas / Micropolitan Statistical Areas in a specific State:
193
+
194
+ `@client.sf1('P0010001', 'CBSA', 'STATE:02')`
195
+
196
+ Retrieve fields from a specific Metropolitan Statistical Areas / Micropolitan Statistical Areas in a specific State:
197
+
198
+ `@client.sf1('P0010001', 'CBSA:11260', 'STATE:02')`
199
+
200
+ #### METDIV - *(323) state-metropolitan statistical area/micropolitan statistical area- metropolitan division*
201
+
202
+ Retrieve fields from all Metropolitan Divisions in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
203
+
204
+ `@client.sf1('P0010001', 'METDIV', 'STATE:06+CBSA:31100')`
205
+
206
+ Retrieve fields from all Metropolitan Division in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
207
+
208
+ `@client.sf1('P0010001', 'METDIV', 'STATE:06+CBSA:31100')`
209
+
210
+ Retrieve fields from a specific Metropolitan Division in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
211
+
212
+ `@client.sf1('P0010001', 'METDIV:31084', 'STATE:06+CBSA:31100')`
213
+
214
+ #### CSA - *(340) - state-combined statistical area*
215
+
216
+ Retrieve fields from all Combined Statistical Areas in a specific State:
217
+
218
+ `@client.sf1('P0010001', 'CSA', 'STATE:24')`
219
+
220
+ Retrieve fields from a specific Combined Statistical Area in a specific State:
221
+
222
+ `@client.sf1('P0010001', 'CSA:428', 'STATE:24')`
223
+
224
+ #### CD - *(500) state-congressional district*
225
+
226
+ Retrieve fields from all Congressional Districts in a specific State:
227
+
228
+ `@client.sf1('P0010001', 'CD', 'STATE:24')`
229
+
230
+ Retrieve fields from a specific Congressional District in a specific State:
231
+
232
+ `@client.sf1('P0010001', 'CD:01', 'STATE:24')`
233
+
234
+ #### COUNTY (Remainder) - *(510) state-congressional district-county*
235
+
236
+ Retrieve fields for all Counties within a specific Congressional District:
237
+
238
+ `@client.sf1('P0010001', 'COUNTY', 'STATE:24+CD:01')`
239
+
240
+ Retrieve fields for a specific County within a specific Congressional District:
241
+
242
+ `@client.sf1('P0010001', 'COUNTY:01', 'STATE:24+CD:01')`
243
+
244
+ #### TRACT (Remainder) - *(511) state-congressional district-county-tract*
245
+
246
+ Retrieve fields for all Tracts within a specific Congressional District, County Remainder:
247
+
248
+ `@client.sf1('P0010001', 'TRACT', 'STATE:24+CD:01+COUNTY:003')`
249
+
250
+ Retrieve fields for a specific County within a specific Congressional District, County Remainder:
251
+
252
+ `@client.sf1('P0010001', 'TRACT:702100', 'STATE:24+CD:01+COUNTY:003')`
253
+
254
+ ### COUSUB (Remainder) - *(521) state-congressional district-county-county subdivision*
255
+
256
+ Retrieve fields for all County Subdivisions within a specific Congressional District, County Remainder:
257
+
258
+ `@client.sf1('P0010001', 'COUSUB', 'STATE:24+CD:01+COUNTY:003')`
259
+
260
+ Retrieve fields for a specific County Subdivision within a specific Congressional District, County Remainder:
261
+
262
+ `@client.sf1('P0010001', 'COUSUB:90100', 'STATE:24+CD:01+COUNTY:003')`
263
+
264
+ #### PLACE (Remainder) - *(531) state congressional district-place*
265
+
266
+ Retrieve fields for all Places within a specific Congressional District:
267
+
268
+ `@client.sf1('P0010001', 'PLACE', 'STATE:24+CD:01')`
269
+
270
+ Retrieve fields for a specific Place within a specific Congressional District, County Remainder:
271
+
272
+ `@client.sf1('P0010001', 'PLACE:00125', 'STATE:24+CD:01')`
273
+
274
+ #### AIANNH (Remainder) - *550) state-congressional district-american indian area/alaska native area/hawaiian home land*
275
+
276
+ Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Lands within a specific Congressional District:
277
+
278
+ `@client.sf1('P0010001', 'AIANNH', 'STATE:02+CD:00')`
279
+
280
+ Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific Congressional District:
281
+
282
+ `@client.sf1('P0010001', 'AIANNH:03800', 'STATE:02+CD:00')`
283
+
284
+ #### ANRC (Remainder) - *(560) state-congressional district-alaska native regional corporation*
285
+
286
+ Retrieve fields for all Alaska Native Regional Corporations within a specific Congressional District:
287
+
288
+ `@client.sf1('P0010001', 'AIANNH', 'STATE:02+CD:00')`
289
+
290
+ Retrieve fields for a specific Alaska Native Regional Corporation within a specific Congressional District:
291
+
292
+ `@client.sf1('P0010001', 'AIANNH:00590', 'STATE:02+CD:00')`
293
+
294
+ #### SLDU - *(610) state-state legislative district (upper chamber)*
295
+
296
+ Retrieve fields for all State Legislative Districts (Upper Chamber) within a State:
297
+
298
+ `@client.sf1('P0010001', 'SLDU', 'STATE:02')`
299
+
300
+ Retrieve fields for a specific State Legislative District (Upper Chamber) within a State:
301
+
302
+ `@client.sf1('P0010001', 'SLDU:00A', 'STATE:02')`
303
+
304
+ #### SLDU - *(620) state-state legislative district (upper chamber)*
305
+
306
+ Retrieve fields for all State Legislative Districts (Lower Chamber) within a State:
307
+
308
+ `@client.sf1('P0010001', 'SLDL', 'STATE:02')`
309
+
310
+ Retrieve fields for a specific State Legislative District (Upper Chamber) within a State:
311
+
312
+ `@client.sf1('P0010001', 'SLDL:001', 'STATE:02')`
313
+ 
314
+ #### ZCTA5 - *(871) state-zip code tabulation area*
315
+
316
+ Retrieve fields for all Zip Code Tabulation Areas within a specific State:
317
+
318
+ `@client.sf1('P0010001', 'ZCTA', 'STATE:02')`
319
+
320
+ Retrieve fields for a specific Zip Code Tabulation Area within a specific State:
321
+
322
+ `@client.sf1('P0010001', 'ZCTA:99501', 'STATE:02')`
323
+
324
+ ## ACS5 2010 Examples and Supported Geography
325
+
326
+ Querying the Census Bureau for ACS5 data is done in the same format as querying for census data. The only difference is the geographies and fields available.
327
+
328
+ #### STATE - *(040) state *
329
+
330
+ Retrieve fields for all States:
331
+
332
+ `@client.acs5('B00001_001E', 'STATE')`
333
+
334
+ Retrieve fields for California (geoid: 06):
335
+
336
+ `@client.acs5('B00001_001E', 'STATE:06')`
337
+
338
+ Retrieve fields for California and Alaska:
339
+
340
+ `@client.acs5('B00001_001E', 'STATE:06,02')`
341
+
342
+ #### COUNTY - *(050) state-county*
343
+
344
+ Retrieve fields for all Counties:
345
+
346
+ `@client.acs5('B00001_001E', 'COUNTY')`
347
+
348
+ Retrieve fields for Counties in California:
349
+
350
+ `@client.acs5('B00001_001E', 'COUNTY', 'STATE:06')`
351
+
352
+ Retrieve fields for a specific County in California:
353
+
354
+ `@client.acs5('B00001_001E', 'COUNTY:001', 'STATE:06')`
355
+
356
+ #### COUSUB - *(060) state-county-county subdivision*
357
+
358
+ Retrieve fields for all County Subdivisions within a specific County:
359
+
360
+ `@client.acs5('B00001_001E', 'COUSUB', 'STATE:02+COUNTY:290')`
361
+
362
+ Retrieve fields for a specific County Subdivision within a specific County:
363
+
364
+ `@client.acs5('B00001_001E', 'COUSUB:86690', 'STATE:02+COUNTY:290')`
365
+
366
+ Note: You must also specify the State the County belongs to.
367
+
368
+ #### SUBMCD - *(067) state-county-county subdivision-subminor civil subdivision*
369
+
370
+ Retrieve fields for all Subminor Civil Subdivisions within a specific County Subdivision:
371
+
372
+ `@client.acs5('B00001_001E', 'SUBMCD', 'STATE:72+COUNTY:127+COUSUB:79693')`
373
+
374
+ Retrieve fields for a specific Subminor Civil Subdivision within a specific County Subdivision:
375
+
376
+ `@client.acs5('B00001_001E', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
377
+
378
+ #### PLACE - *(070) state-county-county subdivision-place*
379
+
380
+ Retrieve fields for all Places within a specific County Subdivision:
381
+
382
+ `@client.acs5('B00001_001E', 'PLACE', 'STATE:02+COUNTY:290+COUSUB:86690')`
383
+
384
+ Retrieve fields for a specific Place within a specific County Subdivision:
385
+
386
+ `@client.acs5('B00001_001E', 'PLACE:05750', 'STATE:02+COUNTY:290+COUSUB:86690')`
387
+
388
+ #### TRACT - *(080) state-county-county subdivision-place-tract*
389
+
390
+ Retrieve fields for all Tracts within a specific Place:
391
+
392
+ `@client.acs5('B00001_001E', 'TRACT', 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750')`
393
+
394
+ Retrieve fields for a specific Tract within a specific Place:
395
+
396
+ `@client.acs5('B00001_001E', 'TRACT:000100', 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750')`
397
+
398
+ #### TRACT - *(140) state-county-tract*
399
+
400
+ Retrieve fields for all Tracts within a specific County:
401
+
402
+ `@client.acs5('B00001_001E', 'TRACT', 'STATE:02+COUNTY:170')`
403
+
404
+ Retrieve fields for a specific Tract within a specific County:
405
+
406
+ `@client.acs5('B00001_001E', 'TRACT:000101', 'STATE:02+COUNTY:170')`
407
+
408
+ #### BG - *(150) state-county- tract-block group*
409
+
410
+ Retrieve fields for all Block Groups within a specific Tract:
411
+
412
+ `@client.acs5('B00001_001E', 'BG', 'STATE:02+COUNTY:170+TRACT:000101')`
413
+
414
+ Retrieve fields for a specific Block Group within a specific Tract:
415
+
416
+ `@client.acs5('B00001_001E', 'BG:1', 'STATE:02+COUNTY:170+TRACT:000101')`
417
+
418
+ #### PLACE - *(160) state-place*
419
+
420
+ Retrieve fields for all Places:
421
+
422
+ `@client.acs5('B00001_001E', 'PLACE')`
423
+
424
+ Retrieve fields for all Places within a specific State:
425
+
426
+ `@client.acs5('B00001_001E', 'PLACE', 'STATE:06')`
427
+
428
+ Retrieve fields for a specific place within a specific State:
429
+
430
+ `@client.acs5('B00001_001E', 'PLACE:00135', 'STATE:06')`
431
+
432
+ #### AIANNH - *(280) state-american indian area/alaska native area/hawaiian home land*
433
+
434
+ Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land:
435
+
436
+ `@client.acs5('B00001_001E', 'AIANNH')`
437
+
438
+ Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
439
+
440
+ `@client.acs5('B00001_001E', 'AIANNH', 'STATE:02')`
441
+
442
+ Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
443
+
444
+ `@client.acs5('B00001_001E', 'AIANNH:03800', 'STATE:02')`
445
+
446
+ #### CD - *(500) state-congressional district*
447
+
448
+ Retrieve fields from all Congressional Districts in a specific State:
449
+
450
+ `@client.acs5('B00001_001E', 'CD', 'STATE:24')`
451
+
452
+ Retrieve fields from a specific Congressional District in a specific State:
453
+
454
+ `@client.acs5('B00001_001E', 'CD:01', 'STATE:24')`
455
+
456
+ ## Additional Resources
457
+
458
+ For a list of all the fields available for the 2010 Census, please consult the following PDF:
459
+
460
+ 2010 Census Summary File 1 - Census Bureau
461
+
462
+ http://www.census.gov/prod/cen2010/doc/sf1.pdf
463
+
464
+ For a list of all the fields available for the 2006-2010 ACS5, please consult the following PDF:
465
+
466
+ The 2006-2010 ACS 5-Year Summary File - Census Bureau
467
+
468
+ http://www2.census.gov/acs2010_5yr/summaryfile/ACS_2006-2010_SF_Tech_Doc.pdf
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'census_api/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "census_api"
8
+ gem.version = CensusApi::VERSION
9
+ gem.authors = ["Ty Rauber"]
10
+ gem.email = ["tyrauber@mac.com"]
11
+ gem.description = %q{A Ruby Gem for querying the US Census Bureau API}
12
+ gem.summary = %q{A Ruby Wrapper for the US Census Bureau API, providing the ability to query both the 2010 Census and 2006-2010 ACS5 datasets.}
13
+ gem.homepage = "https://github.com/tyrauber/census_api.git"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "rest-client"
21
+ gem.add_runtime_dependency "hpricot"
22
+ gem.add_development_dependency 'rspec'
23
+ gem.add_development_dependency 'fakeweb'
24
+ gem.add_development_dependency 'vcr'
25
+ end
@@ -0,0 +1,19 @@
1
+ module CensusApi
2
+ class Client
3
+ attr_reader :api_key
4
+ attr_reader :options
5
+
6
+ def initialize(api_key, options = {})
7
+ @api_key = api_key
8
+ @options = options
9
+ end
10
+
11
+ def sf1(field, level, *within)
12
+ Request.find("sf1", {:key => @api_key, :fields => field, :level => level, :within => within})
13
+ end
14
+
15
+ def acs5(field, level, *within)
16
+ Request.find("acs5", {:key => @api_key, :fields => field, :level => level, :within => within})
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,74 @@
1
+ module CensusApi
2
+ class Request
3
+
4
+ require 'restclient'
5
+ require 'hpricot'
6
+ require 'json'
7
+ require "yaml"
8
+
9
+ attr_accessor :response
10
+
11
+ @@census_shapes
12
+
13
+ CENSUS_URL = "http://api.census.gov/data/2010"
14
+
15
+ def self.find(source, options = {})
16
+ flds = options[:fields].split(",").push("NAME").join(",")
17
+ params = { :key => options[:key], :get => flds, :for => format(options[:level],false) }
18
+ params.merge!({ :in => format(options[:within][0],true) }) if !options[:within].empty?
19
+ request = new(CENSUS_URL, source, params)
20
+ request.parse_response
21
+ end
22
+
23
+ def initialize(url, source, options)
24
+ path = "#{url}/#{source}?#{options.to_p}"
25
+ @response = RestClient.get(path){
26
+ |response, request, result, &block|
27
+ response
28
+ }
29
+ return @response
30
+ end
31
+
32
+ def parse_response
33
+ case @response.code
34
+ when 200
35
+ response = JSON.parse(@response)
36
+ header = response.delete_at(0)
37
+ return response.map{|r| Hash[header.map{|h| h.gsub("NAME","name")}.zip(r)]}
38
+ else
39
+ return {:code => @response.code, :message=> "Invalid API key or request", :location=> @response.headers[:location]}
40
+ end
41
+ end
42
+
43
+ protected
44
+
45
+ def self.format(str,truncate)
46
+ result = str.split("+").map{|s|
47
+ if s.match(":")
48
+ s = s.split(":")
49
+ else
50
+ s = [s,"*"]
51
+ end
52
+ shp = shapes[s[0].upcase]
53
+ s.shift && s.unshift(shp['name'].downcase.gsub(" ", "+")) if !shp.nil?
54
+ s.unshift(s.shift.split("/")[0]) if !s[0].scan("home+land").empty? && truncate
55
+ s.join(":")
56
+ }
57
+ return result.join("+")
58
+ end
59
+
60
+ def self.shapes
61
+ return @@census_shapes if defined?( @@census_shapes)
62
+ @@census_shapes = {}
63
+ YAML.load_file("lib/yml/census_shapes.yml").each{|k,v| @@census_shapes[k] = v}
64
+ return @@census_shapes
65
+ end
66
+
67
+ end
68
+ end
69
+
70
+ class Hash
71
+ def to_p
72
+ self.map { |k,v| "#{k}=#{v}" }.join("&")
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module CensusApi
2
+ VERSION = "0.1.0"
3
+ end
data/lib/census_api.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+
3
+ %w(version client request).each do |file|
4
+ require File.join(File.dirname(__FILE__), 'census_api', file)
5
+ end
@@ -0,0 +1,133 @@
1
+ ---
2
+ STATE:
3
+ sumlevel: '040'
4
+ slug: STATE
5
+ name: State
6
+ description: state
7
+ path: STATE/2010
8
+ COUNTY:
9
+ sumlevel: '050'
10
+ slug: COUNTY
11
+ name: County
12
+ description: state-county
13
+ path: COUNTY/2010
14
+ COUSUB:
15
+ sumlevel: '060'
16
+ slug: COUSUB
17
+ name: County Subdivision
18
+ description: state-county-county subdivision
19
+ path: COUSUB/2010
20
+ SUBMCD:
21
+ sumlevel: '067'
22
+ slug: SUBMCD
23
+ name: Subminor Civil Subdivision
24
+ description: state-county-county subdivision-subminor civil subdivision
25
+ path: SUBMCD/2010
26
+ TABBLOCK:
27
+ sumlevel: '101'
28
+ slug: TABBLOCK
29
+ name: Block
30
+ description: state-county-tract-block
31
+ path: TABBLOCK/2010
32
+ TRACT:
33
+ sumlevel: '140'
34
+ slug: TRACT
35
+ name: Tract
36
+ description: state-county-tract
37
+ path: TRACT/2010
38
+ BG:
39
+ sumlevel: '150'
40
+ slug: BG
41
+ name: Block Group
42
+ description: state-county-tract-block group
43
+ path: BG/2010
44
+ PLACE:
45
+ sumlevel: '160'
46
+ slug: PLACE
47
+ name: Place
48
+ description: state-place
49
+ path: PLACE/2010
50
+ ANRC:
51
+ sumlevel: '230'
52
+ slug: ANRC
53
+ name: Alaska Native Regional Corporation
54
+ description: state-alaska native regional corporation
55
+ path: ANRC/2010
56
+ AIANNH:
57
+ sumlevel: '280'
58
+ slug: AIANNH
59
+ name: American Indian Area/Alaska Native Area/Hawaiian Home Land
60
+ description: state-american indian area/alaska native area/hawaiian home land
61
+ path: AIANNH/2010
62
+ AITS:
63
+ sumlevel: '281'
64
+ slug: AITS
65
+ name: Tribal Subdivision
66
+ description: state-american indian area-tribal subdivision
67
+ path: AITS/2010
68
+ CBSA:
69
+ sumlevel: '320'
70
+ slug: CBSA
71
+ name: Metropolitan Statistical Area/Micropolitan Statistical Area
72
+ description: state-metropolitan statistical area/micropolitan statistical area
73
+ path: CBSA/2010
74
+ METDIV:
75
+ sumlevel: '323'
76
+ slug: METDIV
77
+ name: Metropolitan Division
78
+ description: state-metropolitan statistical area/micropolitan statistical area-metropolitan division
79
+ path: METDIV/2010
80
+ CSA:
81
+ sumlevel: '340'
82
+ slug: CSA
83
+ name: Combined Statistical Area
84
+ description: state-combined statistical area
85
+ path: CSA/2010
86
+ CD:
87
+ sumlevel: '500'
88
+ slug: CD
89
+ name: Congressional District
90
+ description: state-congressional district (111th)
91
+ path: CD/111
92
+ SLDU:
93
+ sumlevel: '610'
94
+ slug: SLDU
95
+ name: State Legislative District (Upper Chamber)
96
+ description: state-state legislative district (upper chamber)
97
+ path: SLDU/2010
98
+ SLDL:
99
+ sumlevel: '620'
100
+ slug: SLDL
101
+ name: State Legislative District (Lower Chamber)
102
+ description: state-state legislative district (lower chamber)
103
+ path: SLDL/2010
104
+ VTD:
105
+ sumlevel: '700'
106
+ slug: VTD
107
+ name: Voting District
108
+ description: State-County-Voting District/Remainder
109
+ path: VTD/2010
110
+ ZCTA5:
111
+ sumlevel: '871'
112
+ slug: ZCTA5
113
+ name: ZIP Code Tabulation Area
114
+ description: state-zip code tabulation area
115
+ path: ZCTA5/2010
116
+ ELSD:
117
+ sumlevel: '950'
118
+ slug: ELSD
119
+ name: School District (Elementary)
120
+ description: State-School District (Elementary)/Remainder
121
+ path: ELSD/2010
122
+ SCSD:
123
+ sumlevel: '960'
124
+ slug: SCSD
125
+ name: School District (Secondary)
126
+ description: State-School District (Secondary)/Remainder
127
+ path: SCSD/2010
128
+ UNSD:
129
+ sumlevel: '970'
130
+ slug: UNSD
131
+ name: School District (Unified)
132
+ description: State-School District (Unified)/Remainder
133
+ path: UNSD/2010
@@ -0,0 +1 @@
1
+ RSPEC_API_KEY = 'abc123'
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe CensusApi::Client do
4
+
5
+ it 'should not initialize without an api_key' do
6
+ lambda { CensusApi::Client.new }.should raise_error
7
+ end
8
+
9
+ it 'should initialize with an api_key' do
10
+ @client = CensusApi::Client.new(api_key)
11
+ @client.api_key.should == api_key
12
+ end
13
+
14
+ it 'should request sf1' do
15
+ source, options = 'sf1', {:key=> api_key, :fields => 'P0010001', :level => 'STATE:06', :within=>[]}
16
+ @client = CensusApi::Client.new(api_key)
17
+ CensusApi::Request.should_receive(:find).with(source, options)
18
+ @client.sf1(options[:fields], options[:level])
19
+ end
20
+
21
+ it 'should request acs5' do
22
+ source, options = 'acs5', {:key=> api_key, :fields => 'B00001_001E', :level => 'STATE:06', :within=>[]}
23
+ @client = CensusApi::Client.new(api_key)
24
+ CensusApi::Request.should_receive(:find).with(source, options)
25
+ @client.acs5(options[:fields], options[:level])
26
+ end
27
+
28
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ describe CensusApi::Request do
4
+
5
+ context "#find" do
6
+ [{:source => 'sf1', :field => 'P0010001', :results=> [
7
+ {"P0010001"=>"37253956", "name"=>"California", "state"=>"06"},
8
+ {"P0010001"=>"1510271", "name"=>"Alameda County", "state"=>"06", "county"=>"001"}
9
+ ]},
10
+ {:source => 'acs5', :field => 'B00001_001E', :results =>[
11
+ {"B00001_001E"=>"2330290", "name"=>"California", "state"=>"06"},
12
+ {"B00001_001E"=>"92854", "name"=>"Alameda County", "state"=>"06", "county"=>"001"}
13
+ ]}
14
+ ].each do |test|
15
+
16
+ describe "#{test[:source]} for a geography type" do
17
+ use_vcr_cassette "#{test[:source]}_find_states"
18
+
19
+ before(:each) do
20
+ params = {:key=> api_key, :fields => test[:field], :level => 'STATE', :within=>[]}
21
+ @collection = CensusApi::Request.find(test[:source], params)
22
+ end
23
+
24
+ it 'should have 52 results' do
25
+ @collection.count.should == 52
26
+ end
27
+
28
+ it 'should include fields for each result' do
29
+ @collection.each do |result|
30
+ result.should include(test[:field])
31
+ result.should include('name')
32
+ result.should include('state')
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "#{test[:source]} for a geography type and id" do
38
+ use_vcr_cassette "#{test[:source]}_find_state_with_id"
39
+
40
+ before(:each) do
41
+ params = {:key=> api_key, :fields => test[:field], :level => 'STATE:06', :within=>[]}
42
+ @collection = CensusApi::Request.find(test[:source], params)
43
+ end
44
+
45
+ it 'should have one result' do
46
+ @collection.count.should == 1
47
+ end
48
+
49
+ it 'should include fields for each result' do
50
+ @collection.each do |result|
51
+ result.should == test[:results][0]
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "#{test[:source]} for a geography type in a geography type" do
57
+ use_vcr_cassette "#{test[:source]}_find_counties_in_state"
58
+
59
+ before(:each) do
60
+ params = {:key=> api_key, :fields => test[:field], :level => 'COUNTY', :within=>['STATE:06']}
61
+ @collection = CensusApi::Request.find(test[:source], params)
62
+ end
63
+
64
+ it 'should have one result' do
65
+ @collection.count.should == 58
66
+ end
67
+
68
+ it 'should include fields for each result' do
69
+ @collection.each do |result|
70
+ result.should include(test[:field])
71
+ result.should include('name')
72
+ result.should include('state')
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "#{test[:source]} for a geography type and id in a geography type" do
78
+ use_vcr_cassette "#{test[:source]}_find_county_in_state"
79
+
80
+ before(:each) do
81
+ params = {:key=> api_key, :fields => test[:field], :level => 'COUNTY:001', :within=>['STATE:06']}
82
+ @collection = CensusApi::Request.find(test[:source], params)
83
+ end
84
+
85
+ it 'should have one result' do
86
+ @collection.count.should == 1
87
+ end
88
+
89
+ it 'should include fields for each result' do
90
+ @collection.each do |result|
91
+ result.should == test[:results][1]
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ context "#format" do
99
+ it 'should add wildcard after reformatting geography type without id' do
100
+ CensusApi::Request.format('COUSUB', false).should == 'county+subdivision:*'
101
+ end
102
+
103
+ it 'should maintain geography id after reformatting geography type' do
104
+ CensusApi::Request.format('COUSUB:86690', false).should == 'county+subdivision:86690'
105
+ end
106
+
107
+ it 'should truncate geography type AIANNH' do
108
+ CensusApi::Request.format('AIANNH', true).should == 'american+indian+area:*'
109
+ end
110
+
111
+ it 'should not truncate geography type CBSA' do
112
+ CensusApi::Request.format('CBSA', true).should == 'metropolitan+statistical+area/micropolitan+statistical+area:*'
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'vcr_setup'
4
+ require 'api_key'
5
+ require 'census_api'
6
+
7
+ def api_key
8
+ RSPEC_API_KEY
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.extend VCR::RSpec::Macros
13
+ end
data/spec/vcr_setup.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'vcr'
2
+
3
+ VCR.config do |c|
4
+ c.cassette_library_dir = 'spec/vcr_cassettes'
5
+ c.stub_with :fakeweb
6
+ c.ignore_localhost = true
7
+ c.default_cassette_options = { :record => :new_episodes }
8
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: census_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ty Rauber
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: hpricot
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: fakeweb
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: vcr
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A Ruby Gem for querying the US Census Bureau API
95
+ email:
96
+ - tyrauber@mac.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - census_api.gemspec
107
+ - lib/census_api.rb
108
+ - lib/census_api/client.rb
109
+ - lib/census_api/request.rb
110
+ - lib/census_api/version.rb
111
+ - lib/yml/census_shapes.yml
112
+ - spec/api_key.sample.rb
113
+ - spec/census_api/client_spec.rb
114
+ - spec/census_api/request_spec.rb
115
+ - spec/spec_helper.rb
116
+ - spec/vcr_setup.rb
117
+ homepage: https://github.com/tyrauber/census_api.git
118
+ licenses: []
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 1.8.24
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: A Ruby Wrapper for the US Census Bureau API, providing the ability to query
141
+ both the 2010 Census and 2006-2010 ACS5 datasets.
142
+ test_files:
143
+ - spec/api_key.sample.rb
144
+ - spec/census_api/client_spec.rb
145
+ - spec/census_api/request_spec.rb
146
+ - spec/spec_helper.rb
147
+ - spec/vcr_setup.rb