census_api 1.0.1 → 1.0.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.
data/.gitignore CHANGED
@@ -20,3 +20,4 @@ spec/api_key.rb
20
20
  test/tmp
21
21
  test/version_tmp
22
22
  tmp
23
+ misc/*
data/README.md CHANGED
@@ -6,7 +6,9 @@ This gem provides a Ruby wrapper around the Census Bureau API.
6
6
  ## Obtaining an API key
7
7
 
8
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.
9
+ [http://www.census.gov/developers/tos/key_request.html][key] and follow the instructions.
10
+
11
+ [key]: (http://www.census.gov/developers/tos/key_request.html)
10
12
 
11
13
 
12
14
  ## Installing the gem
@@ -17,21 +19,46 @@ To use this gem, install it with <tt>gem install census_api</tt> or add it to yo
17
19
 
18
20
  And install it with <tt>bundle install</tt>
19
21
 
20
- ## Usage
22
+ ## Usage / Retrieving Census Data
23
+
24
+ ### (Optional) Set the API key as an environment variable
25
+
26
+ Once you have the API key, you may want to store it as an environment variable.
27
+
28
+ ```sh
29
+ $ export $CENSUS_API_KEY='your-api-key'
30
+ ```
21
31
 
22
- ### Retrieving Census Data
32
+ ### Register a New Client
23
33
 
24
- First register a new Client:
34
+ ```ruby
35
+ @client = CensusApi::Client.new(API_KEY)
36
+ @client = CensusApi::Client.new(ENV['CENSUS_API_KEY']) # from the environment variable
37
+ @client = CensusApi::Client.new(API_KEY, dataset: 'SF1') # with a dataset
38
+ ```
25
39
 
26
- `@client = CensusApi::Client.new(API_KEY)`
40
+ ### Query a Dataset
27
41
 
28
- To query the 2010 Census SF1 dataset:
42
+ To query the 2010 Census SF1 dataset, first set the dataset to 'SF1':
29
43
 
30
- `@client.sf1(fields, for, in*)`
44
+ ```ruby
45
+ @client = CensusApi::Client.new(API_KEY, dataset: 'SF1') # with a dataset # during intialization
46
+ @client.dataset = 'SF1' # or after initialization, setting the instance variable
47
+ ```
31
48
 
32
- To query the 2006-2010 ACS5 dataset:
49
+ To query the 2006-2010 ACS5 dataset, set the dataset to 'ACS5':
33
50
 
34
- `@client.acs5(fields, for, in*)`
51
+ ```ruby
52
+ @client = CensusApi::Client.new(API_KEY, dataset: 'ACS5') # with a dataset # during intialization
53
+ @client.dataset = 'ACS5' # or after initialization, setting the instance variable
54
+ ```
55
+
56
+ Then, use `Client#find` with the below parameters to query for Census data. For example:
57
+
58
+ ```ruby
59
+ @client.find('P0010001', 'STATE:02,06')
60
+
61
+ ```
35
62
 
36
63
  For a list of the fields available for each dataset, review the reference PDFs linked at the bottom of this document.
37
64
 
@@ -55,39 +82,39 @@ The 'in' parameter is optional, or required, depending upon the geography type.
55
82
 
56
83
  Retrieve fields for all States:
57
84
 
58
- `@client.sf1('P0010001', 'STATE')`
85
+ `@client.find('P0010001', 'STATE')`
59
86
 
60
87
  Retrieve fields for California (geoid: 06):
61
88
 
62
- `@client.sf1('P0010001', 'STATE:06')`
89
+ `@client.find('P0010001', 'STATE:06')`
63
90
 
64
91
  Retrieve fields for California and Alaska:
65
92
 
66
- `@client.sf1('P0010001', 'STATE:06,02')`
93
+ `@client.find('P0010001', 'STATE:06,02')`
67
94
 
68
95
  #### COUNTY - *(050) state-county*
69
96
 
70
97
  Retrieve fields for all Counties:
71
98
 
72
- `@client.sf1('P0010001', 'COUNTY')`
99
+ `@client.find('P0010001', 'COUNTY')`
73
100
 
74
101
  Retrieve fields for Counties in California:
75
102
 
76
- `@client.sf1('P0010001', 'COUNTY', 'STATE:06')`
103
+ `@client.find('P0010001', 'COUNTY', 'STATE:06')`
77
104
 
78
105
  Retrieve fields for a specific County in California:
79
106
 
80
- `@client.sf1('P0010001', 'COUNTY:001', 'STATE:06')`
107
+ `@client.find('P0010001', 'COUNTY:001', 'STATE:06')`
81
108
 
82
109
  #### COUSUB - *(060) state-county-county subdivision*
83
110
 
84
111
  Retrieve fields for all County Subdivisions within a specific County:
85
112
 
86
- `@client.sf1('P0010001', 'COUSUB', 'STATE:02+COUNTY:290')`
113
+ `@client.find('P0010001', 'COUSUB', 'STATE:02+COUNTY:290')`
87
114
 
88
115
  Retrieve fields for a specific County Subdivision within a specific County:
89
116
 
90
- `@client.sf1('P0010001', 'COUSUB:86690', 'STATE:02+COUNTY:290')`
117
+ `@client.find('P0010001', 'COUSUB:86690', 'STATE:02+COUNTY:290')`
91
118
 
92
119
  Note: You must also specify the State the County belongs to.
93
120
 
@@ -95,231 +122,231 @@ Note: You must also specify the State the County belongs to.
95
122
 
96
123
  Retrieve fields for all Subminor Civil Subdivisions within a specific County Subdivision:
97
124
 
98
- `@client.sf1('P0010001', 'SUBMCD', 'STATE:72+COUNTY:127+COUSUB:79693')`
125
+ `@client.find('P0010001', 'SUBMCD', 'STATE:72+COUNTY:127+COUSUB:79693')`
99
126
 
100
127
  Retrieve fields for a specific Subminor Civil Subdivisions within a specific County Subdivision:
101
128
 
102
- `@client.sf1('P0010001', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
129
+ `@client.find('P0010001', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
103
130
 
104
131
  #### TABBLOCK - *(101) state-county-tract-block*
105
132
 
106
133
  Retrieve fields for all Blocks within a specific Tract
107
134
 
108
- `@client.sf1('P0010001', 'TABBLOCK', 'STATE:02+COUNTY:290+TRACT:00100')`
135
+ `@client.find('P0010001', 'TABBLOCK', 'STATE:02+COUNTY:290+TRACT:00100')`
109
136
 
110
137
  Retrieve fields for a specific Subminor Civil Subdivisions within a specific County Subdivision:
111
138
 
112
- `@client.sf1('P0010001', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
139
+ `@client.find('P0010001', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
113
140
 
114
141
  #### TRACT - *(140) state-county-tract*
115
142
 
116
143
  Retrieve fields for all Tracts within a specific County:
117
144
 
118
- `@client.sf1('P0010001', 'TRACT', 'STATE:02+COUNTY:170')`
145
+ `@client.find('P0010001', 'TRACT', 'STATE:02+COUNTY:170')`
119
146
 
120
147
  Retrieve fields for a specific Tract within a specific County:
121
148
 
122
- `@client.sf1('P0010001', 'TRACT:000101', 'STATE:02+COUNTY:170')`
149
+ `@client.find('P0010001', 'TRACT:000101', 'STATE:02+COUNTY:170')`
123
150
 
124
151
  #### BG - *(150) state-county- tract-block group*
125
152
 
126
153
  Retrieve fields for all Block Groups within a specific Tract:
127
154
 
128
- `@client.sf1('P0010001', 'BG', 'STATE:02+COUNTY:170+TRACT:000101')`
155
+ `@client.find('P0010001', 'BG', 'STATE:02+COUNTY:170+TRACT:000101')`
129
156
 
130
157
  Retrieve fields for a specific Block Group within a specific Tract:
131
158
 
132
- `@client.sf1('P0010001', 'BG:1', 'STATE:02+COUNTY:170+TRACT:000101')`
159
+ `@client.find('P0010001', 'BG:1', 'STATE:02+COUNTY:170+TRACT:000101')`
133
160
 
134
161
  #### PLACE -*(160) state-place*
135
162
 
136
163
  Retrieve fields for all Places:
137
164
 
138
- `@client.sf1('P0010001', 'PLACE')`
165
+ `@client.find('P0010001', 'PLACE')`
139
166
 
140
167
  Retrieve fields for all Places within a specific State:
141
168
 
142
- `@client.sf1('P0010001', 'PLACE', 'STATE:06')`
169
+ `@client.find('P0010001', 'PLACE', 'STATE:06')`
143
170
 
144
171
  Retrieve fields for a specific place within a specific State:
145
172
 
146
- `@client.sf1('P0010001', 'PLACE:00135', 'STATE:06')`
173
+ `@client.find('P0010001', 'PLACE:00135', 'STATE:06')`
147
174
 
148
175
  #### ANRC - *(260) state-alaska native regional corporation*
149
176
 
150
177
  Retrieve fields for all Alaska Native Regional Corporations:
151
178
 
152
- `@client.sf1('P0010001', 'ANRC')`
179
+ `@client.find('P0010001', 'ANRC')`
153
180
 
154
181
  Retrieve fields for all Alaska Native Regional Corporations within a specific State:
155
182
 
156
- `@client.sf1('P0010001', 'ANRC', 'STATE:02')`
183
+ `@client.find('P0010001', 'ANRC', 'STATE:02')`
157
184
 
158
185
  Retrieve fields for all Alaska Native Regional Corporations within a specific State:
159
186
 
160
- `@client.sf1('P0010001', 'ANRC:00590', 'STATE:02')`
187
+ `@client.find('P0010001', 'ANRC:00590', 'STATE:02')`
161
188
 
162
189
  #### AIANNH - *(280) state-american indian area/alaska native area/hawaiian home land*
163
190
 
164
191
  Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land:
165
192
 
166
- `@client.sf1('P0010001', 'AIANNH')`
193
+ `@client.find('P0010001', 'AIANNH')`
167
194
 
168
195
  Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
169
196
 
170
- `@client.sf1('P0010001', 'AIANNH', 'STATE:02')`
197
+ `@client.find('P0010001', 'AIANNH', 'STATE:02')`
171
198
 
172
199
  Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
173
200
 
174
- `@client.sf1('P0010001', 'AIANNH:03800', 'STATE:02')`
201
+ `@client.find('P0010001', 'AIANNH:03800', 'STATE:02')`
175
202
 
176
203
  #### AITS - *(281) state-american indian area-tribal subdivision*
177
204
 
178
205
  Retrieve fields for all American Indian Area-Tribal Subdivisions:
179
206
 
180
- `@client.sf1('P0010001', 'AITS')`
207
+ __DOES NOT WORK__: `@client.find('P0010001', 'AITS')`
181
208
 
182
209
  Retrieve fields for all American Indian Area-Tribal Subdivisions in a specific American Indian Area:
183
210
 
184
- `@client.sf1('P0010001', 'AITS', 'STATE:40+AIANNH:13735')`
211
+ `@client.find('P0010001', 'AITS', 'STATE:40+AIANNH:13735')`
185
212
 
186
213
  Retrieve fields for a specific American Indian Area-Tribal Subdivision in a specific American Indian Area:
187
214
 
188
- `@client.sf1('P0010001', 'AITS:83127', 'STATE:40+AIANNH:13735')`
215
+ `@client.find('P0010001', 'AITS:83127', 'STATE:40+AIANNH:13735')`
189
216
 
190
217
  #### CBSA - *(320) state-metropolitan statistical area/micropolitan statistical area*
191
218
 
192
219
  Retrieve fields from all Metropolitan Statistical Areas / Micropolitan Statistical Areas in a specific State:
193
220
 
194
- `@client.sf1('P0010001', 'CBSA', 'STATE:02')`
221
+ `@client.find('P0010001', 'CBSA', 'STATE:02')`
195
222
 
196
223
  Retrieve fields from a specific Metropolitan Statistical Areas / Micropolitan Statistical Areas in a specific State:
197
224
 
198
- `@client.sf1('P0010001', 'CBSA:11260', 'STATE:02')`
225
+ `@client.find('P0010001', 'CBSA:11260', 'STATE:02')`
199
226
 
200
227
  #### METDIV - *(323) state-metropolitan statistical area/micropolitan statistical area- metropolitan division*
201
228
 
202
229
  Retrieve fields from all Metropolitan Divisions in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
203
230
 
204
- `@client.sf1('P0010001', 'METDIV', 'STATE:06+CBSA:31100')`
231
+ `@client.find('P0010001', 'METDIV', 'STATE:06+CBSA:31100')`
205
232
 
206
233
  Retrieve fields from all Metropolitan Division in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
207
234
 
208
- `@client.sf1('P0010001', 'METDIV', 'STATE:06+CBSA:31100')`
235
+ `@client.find('P0010001', 'METDIV', 'STATE:06+CBSA:31100')`
209
236
 
210
237
  Retrieve fields from a specific Metropolitan Division in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
211
238
 
212
- `@client.sf1('P0010001', 'METDIV:31084', 'STATE:06+CBSA:31100')`
239
+ `@client.find('P0010001', 'METDIV:31084', 'STATE:06+CBSA:31100')`
213
240
 
214
241
  #### CSA - *(340) - state-combined statistical area*
215
242
 
216
243
  Retrieve fields from all Combined Statistical Areas in a specific State:
217
244
 
218
- `@client.sf1('P0010001', 'CSA', 'STATE:24')`
245
+ `@client.find('P0010001', 'CSA', 'STATE:24')`
219
246
 
220
247
  Retrieve fields from a specific Combined Statistical Area in a specific State:
221
248
 
222
- `@client.sf1('P0010001', 'CSA:428', 'STATE:24')`
249
+ `@client.find('P0010001', 'CSA:428', 'STATE:24')`
223
250
 
224
251
  #### CD - *(500) state-congressional district*
225
252
 
226
253
  Retrieve fields from all Congressional Districts in a specific State:
227
254
 
228
- `@client.sf1('P0010001', 'CD', 'STATE:24')`
255
+ `@client.find('P0010001', 'CD', 'STATE:24')`
229
256
 
230
257
  Retrieve fields from a specific Congressional District in a specific State:
231
258
 
232
- `@client.sf1('P0010001', 'CD:01', 'STATE:24')`
259
+ `@client.find('P0010001', 'CD:01', 'STATE:24')`
233
260
 
234
261
  #### COUNTY (Remainder) - *(510) state-congressional district-county*
235
262
 
236
263
  Retrieve fields for all Counties within a specific Congressional District:
237
264
 
238
- `@client.sf1('P0010001', 'COUNTY', 'STATE:24+CD:01')`
265
+ `@client.find('P0010001', 'COUNTY', 'STATE:24+CD:01')`
239
266
 
240
267
  Retrieve fields for a specific County within a specific Congressional District:
241
268
 
242
- `@client.sf1('P0010001', 'COUNTY:01', 'STATE:24+CD:01')`
269
+ `@client.find('P0010001', 'COUNTY:01', 'STATE:24+CD:01')`
243
270
 
244
271
  #### TRACT (Remainder) - *(511) state-congressional district-county-tract*
245
272
 
246
273
  Retrieve fields for all Tracts within a specific Congressional District, County Remainder:
247
274
 
248
- `@client.sf1('P0010001', 'TRACT', 'STATE:24+CD:01+COUNTY:003')`
275
+ `@client.find('P0010001', 'TRACT', 'STATE:24+CD:01+COUNTY:003')`
249
276
 
250
277
  Retrieve fields for a specific County within a specific Congressional District, County Remainder:
251
278
 
252
- `@client.sf1('P0010001', 'TRACT:702100', 'STATE:24+CD:01+COUNTY:003')`
279
+ `@client.find('P0010001', 'TRACT:702100', 'STATE:24+CD:01+COUNTY:003')`
253
280
 
254
281
  ### COUSUB (Remainder) - *(521) state-congressional district-county-county subdivision*
255
282
 
256
283
  Retrieve fields for all County Subdivisions within a specific Congressional District, County Remainder:
257
284
 
258
- `@client.sf1('P0010001', 'COUSUB', 'STATE:24+CD:01+COUNTY:003')`
285
+ `@client.find('P0010001', 'COUSUB', 'STATE:24+CD:01+COUNTY:003')`
259
286
 
260
287
  Retrieve fields for a specific County Subdivision within a specific Congressional District, County Remainder:
261
288
 
262
- `@client.sf1('P0010001', 'COUSUB:90100', 'STATE:24+CD:01+COUNTY:003')`
289
+ `@client.find('P0010001', 'COUSUB:90100', 'STATE:24+CD:01+COUNTY:003')`
263
290
 
264
291
  #### PLACE (Remainder) - *(531) state congressional district-place*
265
292
 
266
293
  Retrieve fields for all Places within a specific Congressional District:
267
294
 
268
- `@client.sf1('P0010001', 'PLACE', 'STATE:24+CD:01')`
295
+ `@client.find('P0010001', 'PLACE', 'STATE:24+CD:01')`
269
296
 
270
297
  Retrieve fields for a specific Place within a specific Congressional District, County Remainder:
271
298
 
272
- `@client.sf1('P0010001', 'PLACE:00125', 'STATE:24+CD:01')`
299
+ `@client.find('P0010001', 'PLACE:00125', 'STATE:24+CD:01')`
273
300
 
274
301
  #### AIANNH (Remainder) - *550) state-congressional district-american indian area/alaska native area/hawaiian home land*
275
302
 
276
303
  Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Lands within a specific Congressional District:
277
304
 
278
- `@client.sf1('P0010001', 'AIANNH', 'STATE:02+CD:00')`
305
+ `@client.find('P0010001', 'AIANNH', 'STATE:02+CD:00')`
279
306
 
280
307
  Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific Congressional District:
281
308
 
282
- `@client.sf1('P0010001', 'AIANNH:03800', 'STATE:02+CD:00')`
309
+ `@client.find('P0010001', 'AIANNH:03800', 'STATE:02+CD:00')`
283
310
 
284
311
  #### ANRC (Remainder) - *(560) state-congressional district-alaska native regional corporation*
285
312
 
286
313
  Retrieve fields for all Alaska Native Regional Corporations within a specific Congressional District:
287
314
 
288
- `@client.sf1('P0010001', 'AIANNH', 'STATE:02+CD:00')`
315
+ `@client.find('P0010001', 'AIANNH', 'STATE:02+CD:00')`
289
316
 
290
317
  Retrieve fields for a specific Alaska Native Regional Corporation within a specific Congressional District:
291
318
 
292
- `@client.sf1('P0010001', 'AIANNH:00590', 'STATE:02+CD:00')`
319
+ `@client.find('P0010001', 'AIANNH:00590', 'STATE:02+CD:00')`
293
320
 
294
321
  #### SLDU - *(610) state-state legislative district (upper chamber)*
295
322
 
296
323
  Retrieve fields for all State Legislative Districts (Upper Chamber) within a State:
297
324
 
298
- `@client.sf1('P0010001', 'SLDU', 'STATE:02')`
325
+ `@client.find('P0010001', 'SLDU', 'STATE:02')`
299
326
 
300
327
  Retrieve fields for a specific State Legislative District (Upper Chamber) within a State:
301
328
 
302
- `@client.sf1('P0010001', 'SLDU:00A', 'STATE:02')`
329
+ `@client.find('P0010001', 'SLDU:00A', 'STATE:02')`
303
330
 
304
- #### SLDU - *(620) state-state legislative district (upper chamber)*
331
+ #### SLDU - *(620) state-state legislative district (lower chamber)*
305
332
 
306
333
  Retrieve fields for all State Legislative Districts (Lower Chamber) within a State:
307
334
 
308
- `@client.sf1('P0010001', 'SLDL', 'STATE:02')`
335
+ `@client.find('P0010001', 'SLDL', 'STATE:02')`
309
336
 
310
- Retrieve fields for a specific State Legislative District (Upper Chamber) within a State:
337
+ Retrieve fields for a specific State Legislative District (Lower Chamber) within a State:
311
338
 
312
- `@client.sf1('P0010001', 'SLDL:001', 'STATE:02')`
339
+ `@client.find('P0010001', 'SLDL:001', 'STATE:02')`
313
340
  
314
341
  #### ZCTA5 - *(871) state-zip code tabulation area*
315
342
 
316
343
  Retrieve fields for all Zip Code Tabulation Areas within a specific State:
317
344
 
318
- `@client.sf1('P0010001', 'ZCTA', 'STATE:02')`
345
+ `@client.find('P0010001', 'ZCTA5', 'STATE:02')`
319
346
 
320
347
  Retrieve fields for a specific Zip Code Tabulation Area within a specific State:
321
348
 
322
- `@client.sf1('P0010001', 'ZCTA:99501', 'STATE:02')`
349
+ `@client.find('P0010001', 'ZCTA5:99501', 'STATE:02')`
323
350
 
324
351
  ## ACS5 2010 Examples and Supported Geography
325
352
 
@@ -329,39 +356,39 @@ Querying the Census Bureau for ACS5 data is done in the same format as querying
329
356
 
330
357
  Retrieve fields for all States:
331
358
 
332
- `@client.acs5('B00001_001E', 'STATE')`
359
+ `@client.find('B00001_001E', 'STATE')`
333
360
 
334
361
  Retrieve fields for California (geoid: 06):
335
362
 
336
- `@client.acs5('B00001_001E', 'STATE:06')`
363
+ `@client.find('B00001_001E', 'STATE:06')`
337
364
 
338
365
  Retrieve fields for California and Alaska:
339
366
 
340
- `@client.acs5('B00001_001E', 'STATE:06,02')`
367
+ `@client.find('B00001_001E', 'STATE:06,02')`
341
368
 
342
369
  #### COUNTY - *(050) state-county*
343
370
 
344
371
  Retrieve fields for all Counties:
345
372
 
346
- `@client.acs5('B00001_001E', 'COUNTY')`
373
+ `@client.find('B00001_001E', 'COUNTY')`
347
374
 
348
375
  Retrieve fields for Counties in California:
349
376
 
350
- `@client.acs5('B00001_001E', 'COUNTY', 'STATE:06')`
377
+ `@client.find('B00001_001E', 'COUNTY', 'STATE:06')`
351
378
 
352
379
  Retrieve fields for a specific County in California:
353
380
 
354
- `@client.acs5('B00001_001E', 'COUNTY:001', 'STATE:06')`
381
+ `@client.find('B00001_001E', 'COUNTY:001', 'STATE:06')`
355
382
 
356
383
  #### COUSUB - *(060) state-county-county subdivision*
357
384
 
358
385
  Retrieve fields for all County Subdivisions within a specific County:
359
386
 
360
- `@client.acs5('B00001_001E', 'COUSUB', 'STATE:02+COUNTY:290')`
387
+ `@client.find('B00001_001E', 'COUSUB', 'STATE:02+COUNTY:290')`
361
388
 
362
389
  Retrieve fields for a specific County Subdivision within a specific County:
363
390
 
364
- `@client.acs5('B00001_001E', 'COUSUB:86690', 'STATE:02+COUNTY:290')`
391
+ `@client.find('B00001_001E', 'COUSUB:86690', 'STATE:02+COUNTY:290')`
365
392
 
366
393
  Note: You must also specify the State the County belongs to.
367
394
 
@@ -369,89 +396,89 @@ Note: You must also specify the State the County belongs to.
369
396
 
370
397
  Retrieve fields for all Subminor Civil Subdivisions within a specific County Subdivision:
371
398
 
372
- `@client.acs5('B00001_001E', 'SUBMCD', 'STATE:72+COUNTY:127+COUSUB:79693')`
399
+ `@client.find('B00001_001E', 'SUBMCD', 'STATE:72+COUNTY:127+COUSUB:79693')`
373
400
 
374
401
  Retrieve fields for a specific Subminor Civil Subdivision within a specific County Subdivision:
375
402
 
376
- `@client.acs5('B00001_001E', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
403
+ `@client.find('B00001_001E', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693')`
377
404
 
378
405
  #### PLACE - *(070) state-county-county subdivision-place*
379
406
 
380
407
  Retrieve fields for all Places within a specific County Subdivision:
381
408
 
382
- `@client.acs5('B00001_001E', 'PLACE', 'STATE:02+COUNTY:290+COUSUB:86690')`
409
+ `@client.find('B00001_001E', 'PLACE', 'STATE:02+COUNTY:290+COUSUB:86690')`
383
410
 
384
411
  Retrieve fields for a specific Place within a specific County Subdivision:
385
412
 
386
- `@client.acs5('B00001_001E', 'PLACE:05750', 'STATE:02+COUNTY:290+COUSUB:86690')`
413
+ `@client.find('B00001_001E', 'PLACE:05750', 'STATE:02+COUNTY:290+COUSUB:86690')`
387
414
 
388
415
  #### TRACT - *(080) state-county-county subdivision-place-tract*
389
416
 
390
417
  Retrieve fields for all Tracts within a specific Place:
391
418
 
392
- `@client.acs5('B00001_001E', 'TRACT', 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750')`
419
+ `@client.find('B00001_001E', 'TRACT', 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750')`
393
420
 
394
421
  Retrieve fields for a specific Tract within a specific Place:
395
422
 
396
- `@client.acs5('B00001_001E', 'TRACT:000100', 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750')`
423
+ `@client.find('B00001_001E', 'TRACT:000100', 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750')`
397
424
 
398
425
  #### TRACT - *(140) state-county-tract*
399
426
 
400
427
  Retrieve fields for all Tracts within a specific County:
401
428
 
402
- `@client.acs5('B00001_001E', 'TRACT', 'STATE:02+COUNTY:170')`
429
+ `@client.find('B00001_001E', 'TRACT', 'STATE:02+COUNTY:170')`
403
430
 
404
431
  Retrieve fields for a specific Tract within a specific County:
405
432
 
406
- `@client.acs5('B00001_001E', 'TRACT:000101', 'STATE:02+COUNTY:170')`
433
+ `@client.find('B00001_001E', 'TRACT:000101', 'STATE:02+COUNTY:170')`
407
434
 
408
435
  #### BG - *(150) state-county- tract-block group*
409
436
 
410
437
  Retrieve fields for all Block Groups within a specific Tract:
411
438
 
412
- `@client.acs5('B00001_001E', 'BG', 'STATE:02+COUNTY:170+TRACT:000101')`
439
+ `@client.find('B00001_001E', 'BG', 'STATE:02+COUNTY:170+TRACT:000101')`
413
440
 
414
441
  Retrieve fields for a specific Block Group within a specific Tract:
415
442
 
416
- `@client.acs5('B00001_001E', 'BG:1', 'STATE:02+COUNTY:170+TRACT:000101')`
443
+ `@client.find('B00001_001E', 'BG:1', 'STATE:02+COUNTY:170+TRACT:000101')`
417
444
 
418
445
  #### PLACE - *(160) state-place*
419
446
 
420
447
  Retrieve fields for all Places:
421
448
 
422
- `@client.acs5('B00001_001E', 'PLACE')`
449
+ `@client.find('B00001_001E', 'PLACE')`
423
450
 
424
451
  Retrieve fields for all Places within a specific State:
425
452
 
426
- `@client.acs5('B00001_001E', 'PLACE', 'STATE:06')`
453
+ `@client.find('B00001_001E', 'PLACE', 'STATE:06')`
427
454
 
428
455
  Retrieve fields for a specific place within a specific State:
429
456
 
430
- `@client.acs5('B00001_001E', 'PLACE:00135', 'STATE:06')`
457
+ `@client.find('B00001_001E', 'PLACE:00135', 'STATE:06')`
431
458
 
432
459
  #### AIANNH - *(280) state-american indian area/alaska native area/hawaiian home land*
433
460
 
434
461
  Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land:
435
462
 
436
- `@client.acs5('B00001_001E', 'AIANNH')`
463
+ `@client.find('B00001_001E', 'AIANNH')`
437
464
 
438
465
  Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
439
466
 
440
- `@client.acs5('B00001_001E', 'AIANNH', 'STATE:02')`
467
+ `@client.find('B00001_001E', 'AIANNH', 'STATE:02')`
441
468
 
442
469
  Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
443
470
 
444
- `@client.acs5('B00001_001E', 'AIANNH:03800', 'STATE:02')`
471
+ `@client.find('B00001_001E', 'AIANNH:03800', 'STATE:02')`
445
472
 
446
473
  #### CD - *(500) state-congressional district*
447
474
 
448
475
  Retrieve fields from all Congressional Districts in a specific State:
449
476
 
450
- `@client.acs5('B00001_001E', 'CD', 'STATE:24')`
477
+ `@client.find('B00001_001E', 'CD', 'STATE:24')`
451
478
 
452
479
  Retrieve fields from a specific Congressional District in a specific State:
453
480
 
454
- `@client.acs5('B00001_001E', 'CD:01', 'STATE:24')`
481
+ `@client.find('B00001_001E', 'CD:01', 'STATE:24')`
455
482
 
456
483
  ## Additional Resources
457
484
 
@@ -1,19 +1,30 @@
1
1
  module CensusApi
2
2
  class Client
3
- attr_reader :api_key
4
- attr_reader :options
3
+ attr_reader :api_key, :options
4
+ attr_accessor :dataset
5
+
6
+ DATASETS = %w( sf1 acs5 ) # can add more datasets as support becomes available
5
7
 
6
8
  def initialize(api_key, options = {})
9
+ raise ArgumentError, "You must set an api_key." unless api_key
10
+
11
+ # Use RestClient directly to determine the validity of the API Key
12
+ path = "http://api.census.gov/data/2010/sf1?key=#{api_key}&get=P0010001&for=state:01"
13
+ response = RestClient.get(path)
14
+
15
+ if response.body.include? "Invalid Key"
16
+ raise "'#{api_key}' is not a valid API key. Check your key for errors, or request a new one at census.gov."
17
+ end
18
+
7
19
  @api_key = api_key
8
- @options = options
20
+ if options[:dataset]
21
+ @dataset = options[:dataset].downcase if DATASETS.include? options[:dataset].downcase
22
+ end
9
23
  end
10
24
 
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})
25
+ def find(fields, level, *within)
26
+ raise "Client has not been assigned a dataset to query. Try @client.dataset = 'SF1' or anything from #{DATASETS}" if self.dataset.nil?
27
+ Request.find(dataset, {key: @api_key, fields: fields, level: level, within: within})
17
28
  end
18
29
  end
19
- end
30
+ end
@@ -12,22 +12,25 @@ module CensusApi
12
12
 
13
13
  CENSUS_URL = "http://api.census.gov/data/2010"
14
14
 
15
+ def initialize(url, source, options)
16
+ path = "#{url}/#{source}?#{options.to_params}"
17
+ @response = RestClient.get(path) do |response, request, result, &block|
18
+ response
19
+ end
20
+ return @response
21
+ end
22
+
23
+
15
24
  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) }
25
+ fields = options[:fields]
26
+ fields = fields.split(",").push("NAME").join(",") if fields.kind_of? String
27
+ fields = fields.push("NAME").join(",") if fields.kind_of? Array
28
+ params = { :key => options[:key], :get => fields, :for => format(options[:level],false) }
18
29
  params.merge!({ :in => format(options[:within][0],true) }) if !options[:within].empty?
19
30
  request = new(CENSUS_URL, source, params)
20
31
  request.parse_response
21
32
  end
22
33
 
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
34
 
32
35
  def parse_response
33
36
  case @response.code
@@ -36,39 +39,39 @@ module CensusApi
36
39
  header = response.delete_at(0)
37
40
  return response.map{|r| Hash[header.map{|h| h.gsub("NAME","name")}.zip(r)]}
38
41
  else
39
- return {:code => @response.code, :message=> "Invalid API key or request", :location=> @response.headers[:location]}
42
+ return {:code => @response.code, :message=> "Invalid API key or request", :location=> @response.headers[:location], :body => @response.body}
40
43
  end
41
44
  end
42
45
 
43
46
  protected
44
47
 
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(File.dirname(__FILE__).to_s + '/../yml/census_shapes.yml').each{|k,v| @@census_shapes[k] = v}
64
- return @@census_shapes
48
+ def self.format(str,truncate)
49
+ result = str.split("+").map{|s|
50
+ if s.match(":")
51
+ s = s.split(":")
52
+ else
53
+ s = [s,"*"]
54
+ end
55
+ shp = shapes[s[0].upcase]
56
+ s.shift && s.unshift(shp['name'].downcase.gsub(" ", "+")) if !shp.nil?
57
+ s.unshift(s.shift.split("/")[0]) if !s[0].scan("home+land").empty? && truncate
58
+ s.join(":")
59
+ }
60
+ return result.join("+")
61
+ end
62
+
63
+ def self.shapes
64
+ return @@census_shapes if defined?( @@census_shapes)
65
+ @@census_shapes = {}
66
+ YAML.load_file(File.dirname(__FILE__).to_s + '/../yml/census_shapes.yml').each{|k,v| @@census_shapes[k] = v}
67
+ return @@census_shapes
68
+ end
69
+
65
70
  end
66
-
67
71
  end
68
- end
69
72
 
70
73
  class Hash
71
- def to_p
74
+ def to_params
72
75
  self.map { |k,v| "#{k}=#{v}" }.join("&")
73
76
  end
74
- end
77
+ end
@@ -1,3 +1,3 @@
1
1
  module CensusApi
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
data/lib/census_api.rb CHANGED
@@ -1,5 +1,3 @@
1
1
  require 'rubygems'
2
2
 
3
- %w(version client request).each do |file|
4
- require File.join(File.dirname(__FILE__), 'census_api', file)
5
- end
3
+ Dir["./lib/census_api/*"].each { |file| require file }
@@ -2,27 +2,43 @@ require 'spec_helper'
2
2
 
3
3
  describe CensusApi::Client do
4
4
 
5
- it 'should not initialize without an api_key' do
6
- lambda { CensusApi::Client.new }.should raise_error
7
- end
5
+ describe "client initialization" do
8
6
 
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
7
+ use_vcr_cassette "initialize_client"
13
8
 
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])
9
+ it 'should not initialize without an api_key' do
10
+ lambda { CensusApi::Client.new }.should raise_error
11
+ end
12
+
13
+ it 'should initialize with an api_key' do
14
+ @client = CensusApi::Client.new(api_key)
15
+ @client.api_key.should == api_key
16
+ end
19
17
  end
20
18
 
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])
19
+ describe "client and dataset initialization" do
20
+
21
+ use_vcr_cassette "initialize_client_and_dataset"
22
+
23
+ it 'should initialize with an api_key and dataset' do
24
+ dataset = 'SF1'
25
+ @client = CensusApi::Client.new(api_key, dataset: dataset)
26
+ @client.api_key.should == api_key
27
+ @client.dataset.should == dataset.downcase
28
+ end
29
+
30
+ it 'should request sf1' do
31
+ source, options = 'sf1', {:key=> api_key, :fields => 'P0010001', :level => 'STATE:06', :within=>[]}
32
+ @client = CensusApi::Client.new(api_key, dataset: source)
33
+ CensusApi::Request.should_receive(:find).with(@client.dataset, options)
34
+ @client.find(options[:fields], options[:level])
35
+ end
36
+
37
+ it 'should request acs5' do
38
+ source, options = 'acs5', {:key=> api_key, :fields => 'B00001_001E', :level => 'STATE:06', :within=>[]}
39
+ @client = CensusApi::Client.new(api_key, dataset: source)
40
+ CensusApi::Request.should_receive(:find).with(@client.dataset, options)
41
+ @client.find(options[:fields], options[:level])
42
+ end
26
43
  end
27
-
28
44
  end
@@ -9,7 +9,7 @@ describe CensusApi::Request do
9
9
  ]},
10
10
  {:source => 'acs5', :field => 'B00001_001E', :results =>[
11
11
  {"B00001_001E"=>"2330290", "name"=>"California", "state"=>"06"},
12
- {"B00001_001E"=>"92854", "name"=>"Alameda County", "state"=>"06", "county"=>"001"}
12
+ {"B00001_001E"=>"92854", "name"=>"Alameda County, California", "state"=>"06", "county"=>"001"}
13
13
  ]}
14
14
  ].each do |test|
15
15
 
data/spec/vcr_setup.rb CHANGED
@@ -2,7 +2,7 @@ require 'vcr'
2
2
 
3
3
  VCR.config do |c|
4
4
  c.cassette_library_dir = 'spec/vcr_cassettes'
5
- c.stub_with :fakeweb
5
+ c.hook_into :fakeweb
6
6
  c.ignore_localhost = true
7
7
  c.default_cassette_options = { :record => :new_episodes }
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: census_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-12 00:00:00.000000000 Z
12
+ date: 2013-07-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client