census_api 1.0.6 → 1.0.7
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.
- checksums.yaml +4 -4
- data/README.md +93 -86
- data/Rakefile +1 -1
- data/census_api.gemspec +18 -13
- data/lib/census_api/client.rb +42 -16
- data/lib/census_api/request.rb +57 -52
- data/lib/census_api/version.rb +2 -1
- data/lib/census_api.rb +2 -1
- data/spec/api_key.sample.rb +1 -1
- data/spec/census_api/client_spec.rb +103 -12
- data/spec/census_api/request_spec.rb +94 -40
- data/spec/spec_helper.rb +1 -1
- data/spec/vcr_setup.rb +5 -5
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 642511253be7aea4c68837c8cc2c769a866a27fa
|
4
|
+
data.tar.gz: 072c04b497dafce02b299a697f9d1f0546a164a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f44224cf3a16e0ebe6892b08eb9b8253753850d936cfeb724098c7e7bc7930d465d750cd159275f8d05fd23409d390a09a0f1400fa67d07b19a71a170eecc537
|
7
|
+
data.tar.gz: 274f4ed8b1af0caa138d1d892f6ae697f43e360a86e5732712f08fb6f51c605bd725560c90a3a855f6556cf87fe0536ba28e65fc6a53df6edcc68e8217bec678
|
data/README.md
CHANGED
@@ -54,10 +54,17 @@ To query the 2006-2010 ACS5 dataset, set the dataset to 'ACS5':
|
|
54
54
|
@client.dataset = 'ACS5' # or after initialization, setting the instance variable
|
55
55
|
```
|
56
56
|
|
57
|
-
Then, use `Client#
|
57
|
+
Then, use `Client#where` with an options hash to query for Census data. The fields and level parameters are required. The within parameter is optional and scopes the query. For example:
|
58
58
|
|
59
59
|
```ruby
|
60
|
-
@client.
|
60
|
+
@client.where({ fields: 'P0010001', level: 'COUNTY:001', within: ['STATE:06'] })
|
61
|
+
|
62
|
+
```
|
63
|
+
|
64
|
+
The `Client.find` method which takes positional arguments is still available, but deprecated.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
@client.find('P0010001', 'COUNTY:001', ['STATE:06'])
|
61
68
|
|
62
69
|
```
|
63
70
|
|
@@ -67,15 +74,15 @@ For a list of the fields available for each dataset, review the reference PDFs l
|
|
67
74
|
|
68
75
|
The 'fields' parameter is a comma separated string of SF1 field names.
|
69
76
|
|
70
|
-
The '
|
77
|
+
The 'level' 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.
|
71
78
|
|
72
79
|
For example, 'STATE:06,02' would reference the state of California and Alaska.
|
73
80
|
|
74
|
-
The '
|
81
|
+
The 'within' parameter is a set of geography types or geography. Similar to the 'level' parameter, a colon separates the geography type from the id and a comma separates ids. Sets of geography are separated by a plus symbol.
|
75
82
|
|
76
83
|
For example, 'STATE:02+COUNTY:290' would reference the 'Yukon-Koyukuk Census Area' County in the state of Alaska.
|
77
84
|
|
78
|
-
The '
|
85
|
+
The 'within' 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 'within' parameter.
|
79
86
|
|
80
87
|
## Census 2010 SF1 Examples and Supported Geography
|
81
88
|
|
@@ -83,39 +90,39 @@ The 'in' parameter is optional, or required, depending upon the geography type.
|
|
83
90
|
|
84
91
|
Retrieve fields for all States:
|
85
92
|
|
86
|
-
`@client.
|
93
|
+
`@client.where({ fields: 'P0010001', level: 'STATE' })`
|
87
94
|
|
88
95
|
Retrieve fields for California (geoid: 06):
|
89
96
|
|
90
|
-
`@client.
|
97
|
+
`@client.where({ fields: 'P0010001', level: 'STATE:06' })`
|
91
98
|
|
92
99
|
Retrieve fields for California and Alaska:
|
93
100
|
|
94
|
-
`@client.
|
101
|
+
`@client.where({ fields: 'P0010001', level: 'STATE:06,02' })`
|
95
102
|
|
96
103
|
#### COUNTY - *(050) state-county*
|
97
104
|
|
98
105
|
Retrieve fields for all Counties:
|
99
106
|
|
100
|
-
`@client.
|
107
|
+
`@client.where({ fields: 'P0010001', level: 'COUNTY' })`
|
101
108
|
|
102
109
|
Retrieve fields for Counties in California:
|
103
110
|
|
104
|
-
`@client.
|
111
|
+
`@client.where({ fields: 'P0010001', level: 'COUNTY', within: 'STATE:06' })`
|
105
112
|
|
106
113
|
Retrieve fields for a specific County in California:
|
107
114
|
|
108
|
-
`@client.
|
115
|
+
`@client.where({ fields: 'P0010001', level: 'COUNTY:001', within: 'STATE:06' })`
|
109
116
|
|
110
117
|
#### COUSUB - *(060) state-county-county subdivision*
|
111
118
|
|
112
119
|
Retrieve fields for all County Subdivisions within a specific County:
|
113
120
|
|
114
|
-
`@client.
|
121
|
+
`@client.where({ fields: 'P0010001', level: 'COUSUB', within: 'STATE:02+COUNTY:290' })`
|
115
122
|
|
116
123
|
Retrieve fields for a specific County Subdivision within a specific County:
|
117
124
|
|
118
|
-
`@client.
|
125
|
+
`@client.where({ fields: 'P0010001', level: 'COUSUB:86690', within: 'STATE:02+COUNTY:290' })`
|
119
126
|
|
120
127
|
Note: You must also specify the State the County belongs to.
|
121
128
|
|
@@ -123,231 +130,231 @@ Note: You must also specify the State the County belongs to.
|
|
123
130
|
|
124
131
|
Retrieve fields for all Subminor Civil Subdivisions within a specific County Subdivision:
|
125
132
|
|
126
|
-
`@client.
|
133
|
+
`@client.where({ fields: 'P0010001', level: 'SUBMCD', within: 'STATE:72+COUNTY:127+COUSUB:79693' })`
|
127
134
|
|
128
135
|
Retrieve fields for a specific Subminor Civil Subdivisions within a specific County Subdivision:
|
129
136
|
|
130
|
-
`@client.
|
137
|
+
`@client.where({ fields: 'P0010001', level: 'SUBMCD:02350', within: 'STATE:72+COUNTY:127+COUSUB:79693' })`
|
131
138
|
|
132
139
|
#### TABBLOCK - *(101) state-county-tract-block*
|
133
140
|
|
134
141
|
Retrieve fields for all Blocks within a specific Tract
|
135
142
|
|
136
|
-
`@client.
|
143
|
+
`@client.where({ fields: 'P0010001', level: 'TABBLOCK', within: 'STATE:02+COUNTY:290+TRACT:00100' })`
|
137
144
|
|
138
145
|
Retrieve fields for a specific Subminor Civil Subdivisions within a specific County Subdivision:
|
139
146
|
|
140
|
-
`@client.
|
147
|
+
`@client.where({ fields: 'P0010001', level: 'SUBMCD:02350', within: 'STATE:72+COUNTY:127+COUSUB:79693' })`
|
141
148
|
|
142
149
|
#### TRACT - *(140) state-county-tract*
|
143
150
|
|
144
151
|
Retrieve fields for all Tracts within a specific County:
|
145
152
|
|
146
|
-
`@client.
|
153
|
+
`@client.where({ fields: 'P0010001', level: 'TRACT', within: 'STATE:02+COUNTY:170' })`
|
147
154
|
|
148
155
|
Retrieve fields for a specific Tract within a specific County:
|
149
156
|
|
150
|
-
`@client.
|
157
|
+
`@client.where({ fields: 'P0010001', level: 'TRACT:000101', within: 'STATE:02+COUNTY:170' })`
|
151
158
|
|
152
159
|
#### BG - *(150) state-county- tract-block group*
|
153
160
|
|
154
161
|
Retrieve fields for all Block Groups within a specific Tract:
|
155
162
|
|
156
|
-
`@client.
|
163
|
+
`@client.where({ fields: 'P0010001', level: 'BG', within: 'STATE:02+COUNTY:170+TRACT:000101' })`
|
157
164
|
|
158
165
|
Retrieve fields for a specific Block Group within a specific Tract:
|
159
166
|
|
160
|
-
`@client.
|
167
|
+
`@client.where({ fields: 'P0010001', level: 'BG:1', within: 'STATE:02+COUNTY:170+TRACT:000101' })`
|
161
168
|
|
162
169
|
#### PLACE -*(160) state-place*
|
163
170
|
|
164
171
|
Retrieve fields for all Places:
|
165
172
|
|
166
|
-
`@client.
|
173
|
+
`@client.where({ fields: 'P0010001', level: 'PLACE' })`
|
167
174
|
|
168
175
|
Retrieve fields for all Places within a specific State:
|
169
176
|
|
170
|
-
`@client.
|
177
|
+
`@client.where({ fields: 'P0010001', level: 'PLACE', within: 'STATE:06' })`
|
171
178
|
|
172
179
|
Retrieve fields for a specific place within a specific State:
|
173
180
|
|
174
|
-
`@client.
|
181
|
+
`@client.where({ fields: 'P0010001', level: 'PLACE:00135', within: 'STATE:06' })`
|
175
182
|
|
176
183
|
#### ANRC - *(260) state-alaska native regional corporation*
|
177
184
|
|
178
185
|
Retrieve fields for all Alaska Native Regional Corporations:
|
179
186
|
|
180
|
-
`@client.
|
187
|
+
`@client.where({ fields: 'P0010001', level: 'ANRC' })`
|
181
188
|
|
182
189
|
Retrieve fields for all Alaska Native Regional Corporations within a specific State:
|
183
190
|
|
184
|
-
`@client.
|
191
|
+
`@client.where({ fields: 'P0010001', level: 'ANRC', within: 'STATE:02' })`
|
185
192
|
|
186
193
|
Retrieve fields for all Alaska Native Regional Corporations within a specific State:
|
187
194
|
|
188
|
-
`@client.
|
195
|
+
`@client.where({ fields: 'P0010001', level: 'ANRC:00590', within: 'STATE:02' })`
|
189
196
|
|
190
197
|
#### AIANNH - *(280) state-american indian area/alaska native area/hawaiian home land*
|
191
198
|
|
192
199
|
Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land:
|
193
200
|
|
194
|
-
`@client.
|
201
|
+
`@client.where({ fields: 'P0010001', level: 'AIANNH' })`
|
195
202
|
|
196
203
|
Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
|
197
204
|
|
198
|
-
`@client.
|
205
|
+
`@client.where({ fields: 'P0010001', level: 'AIANNH', within: 'STATE:02 }')`
|
199
206
|
|
200
207
|
Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
|
201
208
|
|
202
|
-
`@client.
|
209
|
+
`@client.where({ fields: 'P0010001', level: 'AIANNH:03800', within: 'STATE:02' })`
|
203
210
|
|
204
211
|
#### AITS - *(281) state-american indian area-tribal subdivision*
|
205
212
|
|
206
213
|
Retrieve fields for all American Indian Area-Tribal Subdivisions:
|
207
214
|
|
208
|
-
__DOES NOT WORK__: `@client.
|
215
|
+
__DOES NOT WORK__: `@client.where({ fields: 'P0010001', level: 'AITS' })`
|
209
216
|
|
210
217
|
Retrieve fields for all American Indian Area-Tribal Subdivisions in a specific American Indian Area:
|
211
218
|
|
212
|
-
`@client.
|
219
|
+
`@client.where({ fields: 'P0010001', level: 'AITS', within: 'STATE:40+AIANNH:13735' })`
|
213
220
|
|
214
221
|
Retrieve fields for a specific American Indian Area-Tribal Subdivision in a specific American Indian Area:
|
215
222
|
|
216
|
-
`@client.
|
223
|
+
`@client.where({ fields: 'P0010001', level: 'AITS:83127', within: 'STATE:40+AIANNH:13735' })`
|
217
224
|
|
218
225
|
#### CBSA - *(320) state-metropolitan statistical area/micropolitan statistical area*
|
219
226
|
|
220
227
|
Retrieve fields from all Metropolitan Statistical Areas / Micropolitan Statistical Areas in a specific State:
|
221
228
|
|
222
|
-
`@client.
|
229
|
+
`@client.where({ fields: 'P0010001', level: 'CBSA', within: 'STATE:02' })`
|
223
230
|
|
224
231
|
Retrieve fields from a specific Metropolitan Statistical Areas / Micropolitan Statistical Areas in a specific State:
|
225
232
|
|
226
|
-
`@client.
|
233
|
+
`@client.where({ fields: 'P0010001', level: 'CBSA:11260', within: 'STATE:02' })`
|
227
234
|
|
228
235
|
#### METDIV - *(323) state-metropolitan statistical area/micropolitan statistical area- metropolitan division*
|
229
236
|
|
230
237
|
Retrieve fields from all Metropolitan Divisions in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
|
231
238
|
|
232
|
-
`@client.
|
239
|
+
`@client.where({ fields: 'P0010001', level: 'METDIV', within: 'STATE:06+CBSA:31100' })`
|
233
240
|
|
234
241
|
Retrieve fields from all Metropolitan Division in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
|
235
242
|
|
236
|
-
`@client.
|
243
|
+
`@client.where({ fields: 'P0010001', level: 'METDIV', within: 'STATE:06+CBSA:31100' })`
|
237
244
|
|
238
245
|
Retrieve fields from a specific Metropolitan Division in a specific Metropolitan Statistical Area / Micropolitan Statistical Area:
|
239
246
|
|
240
|
-
`@client.
|
247
|
+
`@client.where({ fields: 'P0010001', level: 'METDIV:31084', within: 'STATE:06+CBSA:31100' })`
|
241
248
|
|
242
249
|
#### CSA - *(340) - state-combined statistical area*
|
243
250
|
|
244
251
|
Retrieve fields from all Combined Statistical Areas in a specific State:
|
245
252
|
|
246
|
-
`@client.
|
253
|
+
`@client.where({ fields: 'P0010001', level: 'CSA', within: 'STATE:24' })`
|
247
254
|
|
248
255
|
Retrieve fields from a specific Combined Statistical Area in a specific State:
|
249
256
|
|
250
|
-
`@client.
|
257
|
+
`@client.where({ fields: 'P0010001', level: 'CSA:428', within: 'STATE:24' })`
|
251
258
|
|
252
259
|
#### CD - *(500) state-congressional district*
|
253
260
|
|
254
261
|
Retrieve fields from all Congressional Districts in a specific State:
|
255
262
|
|
256
|
-
`@client.
|
263
|
+
`@client.where({ fields: 'P0010001', level: 'CD', within: 'STATE:24' })`
|
257
264
|
|
258
265
|
Retrieve fields from a specific Congressional District in a specific State:
|
259
266
|
|
260
|
-
`@client.
|
267
|
+
`@client.where({ fields: 'P0010001', level: 'CD:01', within: 'STATE:24' })`
|
261
268
|
|
262
269
|
#### COUNTY (Remainder) - *(510) state-congressional district-county*
|
263
270
|
|
264
271
|
Retrieve fields for all Counties within a specific Congressional District:
|
265
272
|
|
266
|
-
`@client.
|
273
|
+
`@client.where({ fields: 'P0010001', level: 'COUNTY', within: 'STATE:24+CD:01' })`
|
267
274
|
|
268
275
|
Retrieve fields for a specific County within a specific Congressional District:
|
269
276
|
|
270
|
-
`@client.
|
277
|
+
`@client.where({ fields: 'P0010001', level: 'COUNTY:01', within: 'STATE:24+CD:01' })`
|
271
278
|
|
272
279
|
#### TRACT (Remainder) - *(511) state-congressional district-county-tract*
|
273
280
|
|
274
281
|
Retrieve fields for all Tracts within a specific Congressional District, County Remainder:
|
275
282
|
|
276
|
-
`@client.
|
283
|
+
`@client.where({ fields: 'P0010001', level: 'TRACT', within: 'STATE:24+CD:01+COUNTY:003' })`
|
277
284
|
|
278
285
|
Retrieve fields for a specific County within a specific Congressional District, County Remainder:
|
279
286
|
|
280
|
-
`@client.
|
287
|
+
`@client.where({ fields: 'P0010001', level: 'TRACT:702100', within: 'STATE:24+CD:01+COUNTY:003' })`
|
281
288
|
|
282
289
|
### COUSUB (Remainder) - *(521) state-congressional district-county-county subdivision*
|
283
290
|
|
284
291
|
Retrieve fields for all County Subdivisions within a specific Congressional District, County Remainder:
|
285
292
|
|
286
|
-
`@client.
|
293
|
+
`@client.where({ fields: 'P0010001', level: 'COUSUB', within: 'STATE:24+CD:01+COUNTY:003' })`
|
287
294
|
|
288
295
|
Retrieve fields for a specific County Subdivision within a specific Congressional District, County Remainder:
|
289
296
|
|
290
|
-
`@client.
|
297
|
+
`@client.where({ fields: 'P0010001', level: 'COUSUB:90100', within: 'STATE:24+CD:01+COUNTY:003' })`
|
291
298
|
|
292
299
|
#### PLACE (Remainder) - *(531) state congressional district-place*
|
293
300
|
|
294
301
|
Retrieve fields for all Places within a specific Congressional District:
|
295
302
|
|
296
|
-
`@client.
|
303
|
+
`@client.where({ fields: 'P0010001', level: 'PLACE', within: 'STATE:24+CD:01' })`
|
297
304
|
|
298
305
|
Retrieve fields for a specific Place within a specific Congressional District, County Remainder:
|
299
306
|
|
300
|
-
`@client.
|
307
|
+
`@client.where({ fields: 'P0010001', level: 'PLACE:00125', within: 'STATE:24+CD:01' })`
|
301
308
|
|
302
309
|
#### AIANNH (Remainder) - *550) state-congressional district-american indian area/alaska native area/hawaiian home land*
|
303
310
|
|
304
311
|
Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Lands within a specific Congressional District:
|
305
312
|
|
306
|
-
`@client.
|
313
|
+
`@client.where({ fields: 'P0010001', level: 'AIANNH', within: 'STATE:02+CD:00' })`
|
307
314
|
|
308
315
|
Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific Congressional District:
|
309
316
|
|
310
|
-
`@client.
|
317
|
+
`@client.where({ fields: 'P0010001', level: 'AIANNH:03800', within: 'STATE:02+CD:00' })`
|
311
318
|
|
312
319
|
#### ANRC (Remainder) - *(560) state-congressional district-alaska native regional corporation*
|
313
320
|
|
314
321
|
Retrieve fields for all Alaska Native Regional Corporations within a specific Congressional District:
|
315
322
|
|
316
|
-
`@client.
|
323
|
+
`@client.where({ fields: 'P0010001', level: 'AIANNH', within: 'STATE:02+CD:00' })`
|
317
324
|
|
318
325
|
Retrieve fields for a specific Alaska Native Regional Corporation within a specific Congressional District:
|
319
326
|
|
320
|
-
`@client.
|
327
|
+
`@client.where({ fields: 'P0010001', level: 'AIANNH:00590', within: 'STATE:02+CD:00' })`
|
321
328
|
|
322
329
|
#### SLDU - *(610) state-state legislative district (upper chamber)*
|
323
330
|
|
324
331
|
Retrieve fields for all State Legislative Districts (Upper Chamber) within a State:
|
325
332
|
|
326
|
-
`@client.
|
333
|
+
`@client.where({ fields: 'P0010001', level: 'SLDU', within: 'STATE:02' })`
|
327
334
|
|
328
335
|
Retrieve fields for a specific State Legislative District (Upper Chamber) within a State:
|
329
336
|
|
330
|
-
`@client.
|
337
|
+
`@client.where({ fields: 'P0010001', level: 'SLDU:00A', within: 'STATE:02' })`
|
331
338
|
|
332
339
|
#### SLDU - *(620) state-state legislative district (lower chamber)*
|
333
340
|
|
334
341
|
Retrieve fields for all State Legislative Districts (Lower Chamber) within a State:
|
335
342
|
|
336
|
-
`@client.
|
343
|
+
`@client.where({ fields: 'P0010001', level: 'SLDL', within: 'STATE:02' })`
|
337
344
|
|
338
345
|
Retrieve fields for a specific State Legislative District (Lower Chamber) within a State:
|
339
346
|
|
340
|
-
`@client.
|
347
|
+
`@client.where({ fields: 'P0010001', level: 'SLDL:001', within: 'STATE:02' })`
|
341
348
|

|
342
349
|
#### ZCTA5 - *(871) state-zip code tabulation area*
|
343
350
|
|
344
351
|
Retrieve fields for all Zip Code Tabulation Areas within a specific State:
|
345
352
|
|
346
|
-
`@client.
|
353
|
+
`@client.where({ fields: 'P0010001', level: 'ZCTA5', within: 'STATE:02' })`
|
347
354
|
|
348
355
|
Retrieve fields for a specific Zip Code Tabulation Area within a specific State:
|
349
356
|
|
350
|
-
`@client.
|
357
|
+
`@client.where({ fields: 'P0010001', level: 'ZCTA5:99501', within: 'STATE:02' })`
|
351
358
|
|
352
359
|
## ACS5 2010 Examples and Supported Geography
|
353
360
|
|
@@ -357,39 +364,39 @@ Querying the Census Bureau for ACS5 data is done in the same format as querying
|
|
357
364
|
|
358
365
|
Retrieve fields for all States:
|
359
366
|
|
360
|
-
`@client.
|
367
|
+
`@client.where({ fields: 'B00001_001E', level: 'STATE' })`
|
361
368
|
|
362
369
|
Retrieve fields for California (geoid: 06):
|
363
370
|
|
364
|
-
`@client.
|
371
|
+
`@client.where({ fields: 'B00001_001E', level: 'STATE:06' })`
|
365
372
|
|
366
373
|
Retrieve fields for California and Alaska:
|
367
374
|
|
368
|
-
`@client.
|
375
|
+
`@client.where({ fields: 'B00001_001E', level: 'STATE:06,02' })`
|
369
376
|
|
370
377
|
#### COUNTY - *(050) state-county*
|
371
378
|
|
372
379
|
Retrieve fields for all Counties:
|
373
380
|
|
374
|
-
`@client.
|
381
|
+
`@client.where({ fields: 'B00001_001E', level: 'COUNTY' })`
|
375
382
|
|
376
383
|
Retrieve fields for Counties in California:
|
377
384
|
|
378
|
-
`@client.
|
385
|
+
`@client.where({ fields: 'B00001_001E', level: 'COUNTY', within: 'STATE:06' })`
|
379
386
|
|
380
387
|
Retrieve fields for a specific County in California:
|
381
388
|
|
382
|
-
`@client.
|
389
|
+
`@client.where({ fields: 'B00001_001E', level: 'COUNTY:001', within: 'STATE:06' })`
|
383
390
|
|
384
391
|
#### COUSUB - *(060) state-county-county subdivision*
|
385
392
|
|
386
393
|
Retrieve fields for all County Subdivisions within a specific County:
|
387
394
|
|
388
|
-
`@client.
|
395
|
+
`@client.where({ fields: 'B00001_001E', level: 'COUSUB', within: 'STATE:02+COUNTY:290' })`
|
389
396
|
|
390
397
|
Retrieve fields for a specific County Subdivision within a specific County:
|
391
398
|
|
392
|
-
`@client.
|
399
|
+
`@client.where({ fields: 'B00001_001E', level: 'COUSUB:86690', within: 'STATE:02+COUNTY:290' })`
|
393
400
|
|
394
401
|
Note: You must also specify the State the County belongs to.
|
395
402
|
|
@@ -397,89 +404,89 @@ Note: You must also specify the State the County belongs to.
|
|
397
404
|
|
398
405
|
Retrieve fields for all Subminor Civil Subdivisions within a specific County Subdivision:
|
399
406
|
|
400
|
-
`@client.
|
407
|
+
`@client.where({ fields: 'B00001_001E', level: 'SUBMCD', within: 'STATE:72+COUNTY:127+COUSUB:79693' })`
|
401
408
|
|
402
409
|
Retrieve fields for a specific Subminor Civil Subdivision within a specific County Subdivision:
|
403
410
|
|
404
|
-
`@client.
|
411
|
+
`@client.where({ fields: 'B00001_001E', level: 'SUBMCD:02350', within: 'STATE:72+COUNTY:127+COUSUB:79693' })`
|
405
412
|
|
406
413
|
#### PLACE - *(070) state-county-county subdivision-place*
|
407
414
|
|
408
415
|
Retrieve fields for all Places within a specific County Subdivision:
|
409
416
|
|
410
|
-
`@client.
|
417
|
+
`@client.where({ fields: 'B00001_001E', level: 'PLACE', within: 'STATE:02+COUNTY:290+COUSUB:86690' })`
|
411
418
|
|
412
419
|
Retrieve fields for a specific Place within a specific County Subdivision:
|
413
420
|
|
414
|
-
`@client.
|
421
|
+
`@client.where({ fields: 'B00001_001E', level: 'PLACE:05750', within: 'STATE:02+COUNTY:290+COUSUB:86690' })`
|
415
422
|
|
416
423
|
#### TRACT - *(080) state-county-county subdivision-place-tract*
|
417
424
|
|
418
425
|
Retrieve fields for all Tracts within a specific Place:
|
419
426
|
|
420
|
-
`@client.
|
427
|
+
`@client.where({ fields: 'B00001_001E', level: 'TRACT', within: 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750' })`
|
421
428
|
|
422
429
|
Retrieve fields for a specific Tract within a specific Place:
|
423
430
|
|
424
|
-
`@client.
|
431
|
+
`@client.where({ fields: 'B00001_001E', level: 'TRACT:000100', within: 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750' })`
|
425
432
|
|
426
433
|
#### TRACT - *(140) state-county-tract*
|
427
434
|
|
428
435
|
Retrieve fields for all Tracts within a specific County:
|
429
436
|
|
430
|
-
`@client.
|
437
|
+
`@client.where({ fields: 'B00001_001E', level: 'TRACT', within: 'STATE:02+COUNTY:170' })`
|
431
438
|
|
432
439
|
Retrieve fields for a specific Tract within a specific County:
|
433
440
|
|
434
|
-
`@client.
|
441
|
+
`@client.where({ fields: 'B00001_001E', level: 'TRACT:000101', within: 'STATE:02+COUNTY:170' })`
|
435
442
|
|
436
443
|
#### BG - *(150) state-county- tract-block group*
|
437
444
|
|
438
445
|
Retrieve fields for all Block Groups within a specific Tract:
|
439
446
|
|
440
|
-
`@client.
|
447
|
+
`@client.where({ fields: 'B00001_001E', level: 'BG', within: 'STATE:02+COUNTY:170+TRACT:000101' })`
|
441
448
|
|
442
449
|
Retrieve fields for a specific Block Group within a specific Tract:
|
443
450
|
|
444
|
-
`@client.
|
451
|
+
`@client.where({ fields: 'B00001_001E', level: 'BG:1', within: 'STATE:02+COUNTY:170+TRACT:000101' })`
|
445
452
|
|
446
453
|
#### PLACE - *(160) state-place*
|
447
454
|
|
448
455
|
Retrieve fields for all Places:
|
449
456
|
|
450
|
-
`@client.
|
457
|
+
`@client.where({ fields: 'B00001_001E', level: 'PLACE' })`
|
451
458
|
|
452
459
|
Retrieve fields for all Places within a specific State:
|
453
460
|
|
454
|
-
`@client.
|
461
|
+
`@client.where({ fields: 'B00001_001E', level: 'PLACE', within: 'STATE:06' })`
|
455
462
|
|
456
463
|
Retrieve fields for a specific place within a specific State:
|
457
464
|
|
458
|
-
`@client.
|
465
|
+
`@client.where({ fields: 'B00001_001E', level: 'PLACE:00135', within: 'STATE:06' })`
|
459
466
|
|
460
467
|
#### AIANNH - *(280) state-american indian area/alaska native area/hawaiian home land*
|
461
468
|
|
462
469
|
Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land:
|
463
470
|
|
464
|
-
`@client.
|
471
|
+
`@client.where({ fields: 'B00001_001E', level: 'AIANNH' })`
|
465
472
|
|
466
473
|
Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
|
467
474
|
|
468
|
-
`@client.
|
475
|
+
`@client.where({ fields: 'B00001_001E', level: 'AIANNH', within: 'STATE:02' })`
|
469
476
|
|
470
477
|
Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
|
471
478
|
|
472
|
-
`@client.
|
479
|
+
`@client.where({ fields: 'B00001_001E', level: 'AIANNH:03800', within: 'STATE:02' })`
|
473
480
|
|
474
481
|
#### CD - *(500) state-congressional district*
|
475
482
|
|
476
483
|
Retrieve fields from all Congressional Districts in a specific State:
|
477
484
|
|
478
|
-
`@client.
|
485
|
+
`@client.where({ fields: 'B00001_001E', level: 'CD', within: 'STATE:24' })`
|
479
486
|
|
480
487
|
Retrieve fields from a specific Congressional District in a specific State:
|
481
488
|
|
482
|
-
`@client.
|
489
|
+
`@client.where({ fields: 'B00001_001E', level: 'CD:01', within: 'STATE:24' })`
|
483
490
|
|
484
491
|
## Additional Resources
|
485
492
|
|
data/Rakefile
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
data/census_api.gemspec
CHANGED
@@ -4,23 +4,28 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'census_api/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
|
-
gem.name =
|
7
|
+
gem.name = 'census_api'
|
8
8
|
gem.version = CensusApi::VERSION
|
9
|
-
gem.authors = [
|
9
|
+
gem.authors = ['Ty Rauber']
|
10
10
|
gem.license = 'MIT'
|
11
|
-
gem.email = [
|
12
|
-
gem.description =
|
13
|
-
gem.summary =
|
14
|
-
|
11
|
+
gem.email = ['tyrauber@mac.com']
|
12
|
+
gem.description = 'A Ruby Gem for querying the US Census Bureau API'
|
13
|
+
gem.summary = 'A Ruby Wrapper for the US Census Bureau API,
|
14
|
+
providing the ability to query both the 2010 Census
|
15
|
+
and 2006-2010 ACS5 datasets.'
|
16
|
+
gem.homepage = 'https://github.com/tyrauber/census_api.git'
|
15
17
|
|
16
|
-
gem.files = `git ls-files`.split(
|
17
|
-
gem.
|
18
|
-
gem.
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
gem.add_runtime_dependency
|
18
|
+
gem.files = `git ls-files`.split("\n")
|
19
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
gem.executables = `git ls-files -- bin/*`.split("\n")
|
21
|
+
.map { |f| File.basename(f) }
|
22
|
+
gem.require_paths = ['lib']
|
23
|
+
|
24
|
+
gem.add_runtime_dependency 'rest-client'
|
25
|
+
gem.add_runtime_dependency 'addressable', '~> 2.3'
|
26
|
+
gem.add_runtime_dependency 'hpricot'
|
23
27
|
gem.add_development_dependency 'rspec'
|
24
28
|
gem.add_development_dependency 'fakeweb'
|
25
29
|
gem.add_development_dependency 'vcr'
|
30
|
+
gem.add_development_dependency 'rubocop'
|
26
31
|
end
|
data/lib/census_api/client.rb
CHANGED
@@ -1,33 +1,59 @@
|
|
1
1
|
module CensusApi
|
2
|
+
# => CensusApi::Client
|
3
|
+
# client#initialize method takes an api_key and options hash,
|
4
|
+
# which includes dataset and vintage. client#where method accepts
|
5
|
+
# an options hash, including fields, level and within. Within is optional.
|
6
|
+
# client#find takes positional arguments and is now deprecated.
|
2
7
|
class Client
|
3
8
|
require 'rest-client'
|
4
9
|
|
5
|
-
attr_reader
|
10
|
+
attr_reader :api_key, :api_vintage, :options
|
6
11
|
attr_accessor :dataset
|
7
12
|
|
8
|
-
DATASETS = %w( sf1 acs1 acs3 acs5 )
|
13
|
+
DATASETS = %w( sf1 acs1 acs3 acs5 )
|
14
|
+
# can add more datasets as support becomes available
|
9
15
|
|
10
16
|
def initialize(api_key, options = {})
|
11
|
-
|
12
|
-
|
13
|
-
# Use RestClient directly to determine the validity of the API Key
|
14
|
-
path = "http://api.census.gov/data/2010/sf1?key=#{api_key}&get=P0010001&for=state:01"
|
15
|
-
response = RestClient.get(path)
|
16
|
-
|
17
|
-
if response.body.include? "Invalid Key"
|
18
|
-
raise "'#{api_key}' is not a valid API key. Check your key for errors, or request a new one at census.gov."
|
19
|
-
end
|
20
|
-
|
17
|
+
fail ArgumentError, 'You must set an api_key.' unless api_key
|
18
|
+
validate_api_key(api_key)
|
21
19
|
@api_key = api_key
|
22
20
|
@api_vintage = options[:vintage] || 2010
|
23
|
-
if options[:dataset]
|
24
|
-
@dataset = options[:dataset].downcase
|
21
|
+
if options[:dataset] && DATASETS.include?(options[:dataset].downcase)
|
22
|
+
@dataset = options[:dataset].downcase
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
28
26
|
def find(fields, level, *within)
|
29
|
-
|
30
|
-
|
27
|
+
warn '[DEPRECATION] `find` is deprecated. Please use `where` instead.'
|
28
|
+
fail "Client requires a dataset (#{DATASETS})." if @dataset.nil?
|
29
|
+
options = {
|
30
|
+
key: @api_key,
|
31
|
+
vintage: @api_vintage,
|
32
|
+
fields: fields,
|
33
|
+
level: level,
|
34
|
+
within: within
|
35
|
+
}
|
36
|
+
Request.find(dataset, options)
|
37
|
+
end
|
38
|
+
|
39
|
+
def where(options = { key: @api_key, vintage: @api_vintage })
|
40
|
+
fail "Client requires a dataset (#{DATASETS})." if @dataset.nil?
|
41
|
+
[:fields, :level].each do |f|
|
42
|
+
fail ArgumentError, "#{f} is a requied parameter" if options[f].nil?
|
43
|
+
end
|
44
|
+
Request.find(dataset, options)
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
|
49
|
+
def validate_api_key(api_key)
|
50
|
+
uri = Addressable::URI.parse('http://api.census.gov/data/2010/sf1')
|
51
|
+
uri.query_values = { key: api_key, get: 'P0010001', for: 'state:01' }
|
52
|
+
response = RestClient.get uri.to_s
|
53
|
+
if response.body.include? 'Invalid Key'
|
54
|
+
fail "'#{api_key}' is not a valid API key. Check your key for errors,
|
55
|
+
or request a new one at census.gov."
|
56
|
+
end
|
31
57
|
end
|
32
58
|
end
|
33
59
|
end
|
data/lib/census_api/request.rb
CHANGED
@@ -1,78 +1,83 @@
|
|
1
1
|
module CensusApi
|
2
|
+
# => CensusApi::Request
|
3
|
+
# client#initialize method takes an url, vintage, source, options hash.
|
4
|
+
# client#find method accepts source and options hash, which include
|
5
|
+
# :key, :fields, :level, :within and :vintage.
|
2
6
|
class Request
|
3
|
-
|
4
7
|
require 'restclient'
|
5
8
|
require 'hpricot'
|
6
9
|
require 'json'
|
7
|
-
require
|
8
|
-
|
10
|
+
require 'yaml'
|
11
|
+
|
9
12
|
attr_accessor :response
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
CENSUS_URL = "http://api.census.gov/data"
|
13
|
+
|
14
|
+
CENSUS_URL = 'http://api.census.gov/data'
|
14
15
|
|
15
16
|
def initialize(url, vintage, source, options)
|
16
|
-
|
17
|
-
|
17
|
+
uri = Addressable::URI.parse("#{url}/#{vintage}/#{source}")
|
18
|
+
uri.query_values = options
|
19
|
+
@response = RestClient.get(uri.to_s) do |response, _req, _res, _blk|
|
18
20
|
response
|
19
21
|
end
|
20
|
-
return @response
|
21
22
|
end
|
22
23
|
|
23
|
-
|
24
24
|
def self.find(source, options = {})
|
25
25
|
fields = options[:fields]
|
26
|
-
fields = fields.split(
|
27
|
-
fields = fields.push(
|
28
|
-
|
29
|
-
params
|
30
|
-
|
26
|
+
fields = fields.split(',').push('NAME').join(',') if fields.is_a? String
|
27
|
+
fields = fields.push('NAME').join(',') if fields.is_a? Array
|
28
|
+
level = format(options[:level], false)
|
29
|
+
params = { key: options[:key], get: fields, for: level }
|
30
|
+
params.merge!(in: format(options[:within][0], true)) unless options[:within].empty?
|
31
|
+
options.merge!(vintage: 2010) unless options[:vintage]
|
31
32
|
request = new(CENSUS_URL, options[:vintage], source, params)
|
32
33
|
request.parse_response
|
33
34
|
end
|
34
35
|
|
35
|
-
|
36
36
|
def parse_response
|
37
37
|
case @response.code
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
return {:code => @response.code, :message=> "Invalid API key or request", :location=> @response.headers[:location], :body => @response.body}
|
44
|
-
end
|
38
|
+
when 200
|
39
|
+
response_success(@response)
|
40
|
+
else
|
41
|
+
response_error(@response)
|
42
|
+
end
|
45
43
|
end
|
46
|
-
|
44
|
+
|
47
45
|
protected
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
s = [s,"*"]
|
55
|
-
end
|
56
|
-
shp = shapes[s[0].upcase]
|
57
|
-
s.shift && s.unshift(shp['name'].downcase.gsub(" ", "+")) if !shp.nil?
|
58
|
-
s.unshift(s.shift.split("/")[0]) if !s[0].scan("home+land").empty? && truncate
|
59
|
-
s.join(":")
|
60
|
-
}
|
61
|
-
return result.join("+")
|
62
|
-
end
|
63
|
-
|
64
|
-
def self.shapes
|
65
|
-
return @@census_shapes if defined?( @@census_shapes)
|
66
|
-
@@census_shapes = {}
|
67
|
-
YAML.load_file(File.dirname(__FILE__).to_s + '/../yml/census_shapes.yml').each{|k,v| @@census_shapes[k] = v}
|
68
|
-
return @@census_shapes
|
46
|
+
|
47
|
+
def response_success(response)
|
48
|
+
response = JSON.parse(response)
|
49
|
+
header = response.delete_at(0)
|
50
|
+
response.map do |r|
|
51
|
+
Hash[header.map { |h| h.gsub('NAME', 'name') }.zip(r)]
|
69
52
|
end
|
70
|
-
|
71
53
|
end
|
72
|
-
end
|
73
54
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
55
|
+
def response_error(response)
|
56
|
+
{
|
57
|
+
code: response.code,
|
58
|
+
message: 'Invalid API key or request',
|
59
|
+
location: response.headers[:location],
|
60
|
+
body: response.body
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.format(str, truncate)
|
65
|
+
result = str.split('+').map do |s|
|
66
|
+
s = s.match(':') ? s.split(':') : [s, '*']
|
67
|
+
shp = shapes[s[0].upcase]
|
68
|
+
s.shift && s.unshift(shp['name'].downcase.gsub(' ', '+')) unless shp.nil?
|
69
|
+
s.unshift(s.shift.split('/')[0]) if !s[0].scan('home+land').empty? && truncate
|
70
|
+
s.join(':')
|
71
|
+
end
|
72
|
+
result.join('+')
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.shapes
|
76
|
+
return @@census_shapes if defined?(@@census_shapes)
|
77
|
+
@@census_shapes = {}
|
78
|
+
path = "#{File.dirname(__FILE__)}/../yml/census_shapes.yml"
|
79
|
+
YAML.load_file(path).each { |k, v| @@census_shapes[k] = v }
|
80
|
+
@@census_shapes
|
81
|
+
end
|
82
|
+
end
|
78
83
|
end
|
data/lib/census_api/version.rb
CHANGED
data/lib/census_api.rb
CHANGED
data/spec/api_key.sample.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
RSPEC_API_KEY = 'abc123'
|
1
|
+
RSPEC_API_KEY = 'abc123'
|
@@ -2,9 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe CensusApi::Client do
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe 'client initialization' do
|
6
6
|
|
7
|
-
use_vcr_cassette
|
7
|
+
use_vcr_cassette 'initialize_client'
|
8
8
|
|
9
9
|
it 'should not initialize without an api_key' do
|
10
10
|
lambda { CensusApi::Client.new }.should raise_error
|
@@ -16,9 +16,9 @@ describe CensusApi::Client do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
describe
|
19
|
+
describe 'client and dataset initialization' do
|
20
20
|
|
21
|
-
use_vcr_cassette
|
21
|
+
use_vcr_cassette 'initialize_client_and_dataset'
|
22
22
|
|
23
23
|
it 'should initialize with an api_key and dataset' do
|
24
24
|
dataset = 'SF1'
|
@@ -26,19 +26,110 @@ describe CensusApi::Client do
|
|
26
26
|
@client.api_key.should == api_key
|
27
27
|
@client.dataset.should == dataset.downcase
|
28
28
|
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'datasets' do
|
32
|
+
|
33
|
+
use_vcr_cassette 'find_method'
|
34
|
+
describe 'sf1' do
|
35
|
+
let(:source) { 'sf1' }
|
36
|
+
let(:options) do
|
37
|
+
{ key: api_key,
|
38
|
+
vintage: 2010,
|
39
|
+
fields: 'P0010001',
|
40
|
+
level: 'STATE:06',
|
41
|
+
within: [] }
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should request sf1' do
|
45
|
+
@client = CensusApi::Client.new(api_key, dataset: source)
|
46
|
+
CensusApi::Request.should_receive(:find).with(@client.dataset, options)
|
47
|
+
@client.where(options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'acs5' do
|
52
|
+
let(:source) { 'acs5' }
|
53
|
+
let(:options) do
|
54
|
+
{ key: api_key,
|
55
|
+
vintage: 2010,
|
56
|
+
fields: 'B00001_001E',
|
57
|
+
level: 'STATE:06',
|
58
|
+
within: [] }
|
59
|
+
end
|
29
60
|
|
30
|
-
|
31
|
-
|
61
|
+
it 'should request acs5' do
|
62
|
+
@client = CensusApi::Client.new(api_key, dataset: source)
|
63
|
+
CensusApi::Request.should_receive(:find).with(@client.dataset, options)
|
64
|
+
@client.where(options)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#find' do
|
70
|
+
|
71
|
+
use_vcr_cassette 'find_method'
|
72
|
+
|
73
|
+
let(:source) { 'sf1' }
|
74
|
+
let(:options) do
|
75
|
+
{ key: api_key,
|
76
|
+
vintage: 2010,
|
77
|
+
fields: 'P0010001',
|
78
|
+
level: 'STATE:06',
|
79
|
+
within: [] }
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should be deprecated' do
|
32
83
|
@client = CensusApi::Client.new(api_key, dataset: source)
|
33
|
-
|
84
|
+
@client.should_receive(:warn)
|
85
|
+
.with('[DEPRECATION] `find` is deprecated. Please use `where` instead.')
|
34
86
|
@client.find(options[:fields], options[:level])
|
35
87
|
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#where' do
|
91
|
+
use_vcr_cassette 'where_method'
|
92
|
+
|
93
|
+
let(:source) { 'sf1' }
|
94
|
+
|
95
|
+
let(:options) do
|
96
|
+
{
|
97
|
+
key: api_key,
|
98
|
+
vintage: 2010,
|
99
|
+
fields: 'P0010001',
|
100
|
+
level: 'STATE:06',
|
101
|
+
within: []
|
102
|
+
}
|
103
|
+
end
|
104
|
+
|
105
|
+
let(:full_params) do
|
106
|
+
options.merge!(level: 'COUNTY:001', within: 'STATE:06')
|
107
|
+
end
|
36
108
|
|
37
|
-
it 'should
|
38
|
-
source, options = 'acs5', {:key=> api_key, :vintage => 2010, :fields => 'B00001_001E', :level => 'STATE:06', :within=>[]}
|
109
|
+
it 'should raise if missing fields params' do
|
39
110
|
@client = CensusApi::Client.new(api_key, dataset: source)
|
40
|
-
|
41
|
-
|
111
|
+
expect { @client.where(fields: options[:fields]) }
|
112
|
+
.to raise_error(ArgumentError)
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should raise if missing level params' do
|
116
|
+
@client = CensusApi::Client.new(api_key, dataset: source)
|
117
|
+
expect { @client.where(level: options[:level]) }
|
118
|
+
.to raise_error(ArgumentError)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should request sf1 with valid fields and level params' do
|
122
|
+
@client = CensusApi::Client.new(api_key, dataset: source)
|
123
|
+
CensusApi::Request.should_receive(:find)
|
124
|
+
.with(@client.dataset, options)
|
125
|
+
expect { @client.where(options) }.not_to raise_error
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'should request sf1 with valid fields, level and within params' do
|
129
|
+
@client = CensusApi::Client.new(api_key, dataset: source)
|
130
|
+
CensusApi::Request.should_receive(:find)
|
131
|
+
.with(@client.dataset, full_params)
|
132
|
+
expect { @client.where(full_params) }.not_to raise_error
|
42
133
|
end
|
43
134
|
end
|
44
|
-
end
|
135
|
+
end
|
@@ -2,29 +2,39 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe CensusApi::Request do
|
4
4
|
|
5
|
-
context
|
6
|
-
[{:
|
7
|
-
{
|
8
|
-
{
|
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
9
|
]},
|
10
|
-
{:
|
11
|
-
{
|
12
|
-
{
|
10
|
+
{source: 'acs5', field: 'B00001_001E', results: [
|
11
|
+
{'B00001_001E'=>'2330290', 'name'=>'California', 'state'=>'06'},
|
12
|
+
{'B00001_001E'=>'92854', 'name'=>'Alameda County, California', 'state'=>'06', 'county'=>'001'}
|
13
13
|
]}
|
14
14
|
].each do |test|
|
15
|
-
|
15
|
+
|
16
16
|
describe "#{test[:source]} for a geography type" do
|
17
17
|
use_vcr_cassette "#{test[:source]}_find_states"
|
18
18
|
|
19
|
+
let(:params) do
|
20
|
+
{
|
21
|
+
key: api_key,
|
22
|
+
source: test[:source],
|
23
|
+
vintage: 2010,
|
24
|
+
fields: test[:field],
|
25
|
+
level: 'STATE',
|
26
|
+
within: []
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
19
30
|
before(:each) do
|
20
|
-
|
21
|
-
@collection = CensusApi::Request.find(test[:source], params)
|
31
|
+
@collection = CensusApi::Request.find(test[:source], params)
|
22
32
|
end
|
23
33
|
|
24
34
|
it 'should have 52 results' do
|
25
35
|
@collection.count.should == 52
|
26
36
|
end
|
27
|
-
|
37
|
+
|
28
38
|
it 'should include fields for each result' do
|
29
39
|
@collection.each do |result|
|
30
40
|
result.should include(test[:field])
|
@@ -33,31 +43,50 @@ describe CensusApi::Request do
|
|
33
43
|
end
|
34
44
|
end
|
35
45
|
end
|
36
|
-
|
46
|
+
|
37
47
|
describe "#{test[:source]} for a geography type and id" do
|
38
48
|
use_vcr_cassette "#{test[:source]}_find_state_with_id"
|
49
|
+
let(:params) do
|
50
|
+
{
|
51
|
+
key: api_key,
|
52
|
+
source: test[:source],
|
53
|
+
vintage: 2010,
|
54
|
+
fields: test[:field],
|
55
|
+
level: 'STATE:06',
|
56
|
+
within: []
|
57
|
+
}
|
58
|
+
end
|
39
59
|
|
40
60
|
before(:each) do
|
41
|
-
|
42
|
-
@collection = CensusApi::Request.find(test[:source], params)
|
61
|
+
@collection = CensusApi::Request.find(test[:source], params)
|
43
62
|
end
|
44
63
|
|
45
64
|
it 'should have one result' do
|
46
65
|
@collection.count.should == 1
|
47
66
|
end
|
48
|
-
|
67
|
+
|
49
68
|
it 'should include fields for each result' do
|
50
69
|
@collection.each do |result|
|
51
70
|
result.should == test[:results][0]
|
52
71
|
end
|
53
72
|
end
|
54
73
|
end
|
55
|
-
|
56
|
-
describe "#{test[:source]} for a geography type
|
74
|
+
|
75
|
+
describe "#{test[:source]} for a geography type" do
|
57
76
|
use_vcr_cassette "#{test[:source]}_find_counties_in_state"
|
58
77
|
|
78
|
+
let(:params) do
|
79
|
+
{
|
80
|
+
key: api_key,
|
81
|
+
source: test[:source],
|
82
|
+
vintage: 2010,
|
83
|
+
fields: test[:field],
|
84
|
+
level: 'COUNTY',
|
85
|
+
within: ['STATE:06']
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
59
89
|
before(:each) do
|
60
|
-
params = {:key=> api_key, :fields => test[:field], :level => 'COUNTY', :within=>['STATE:06']}
|
61
90
|
@collection = CensusApi::Request.find(test[:source], params)
|
62
91
|
end
|
63
92
|
|
@@ -74,11 +103,21 @@ describe CensusApi::Request do
|
|
74
103
|
end
|
75
104
|
end
|
76
105
|
|
77
|
-
describe "#{test[:source]} for a geography
|
106
|
+
describe "#{test[:source]} for a geography" do
|
78
107
|
use_vcr_cassette "#{test[:source]}_find_county_in_state"
|
79
108
|
|
109
|
+
let(:params) do
|
110
|
+
{
|
111
|
+
key: api_key,
|
112
|
+
source: test[:source],
|
113
|
+
vintage: 2010,
|
114
|
+
fields: test[:field],
|
115
|
+
level: 'COUNTY:001',
|
116
|
+
within: ['STATE:06']
|
117
|
+
}
|
118
|
+
end
|
119
|
+
|
80
120
|
before(:each) do
|
81
|
-
params = {:key=> api_key, :fields => test[:field], :level => 'COUNTY:001', :within=>['STATE:06']}
|
82
121
|
@collection = CensusApi::Request.find(test[:source], params)
|
83
122
|
end
|
84
123
|
|
@@ -95,17 +134,18 @@ describe CensusApi::Request do
|
|
95
134
|
end
|
96
135
|
end
|
97
136
|
|
98
|
-
context
|
137
|
+
context 'DATASETS' do
|
99
138
|
CensusApi::Client::DATASETS.each do |source|
|
100
139
|
describe "#{source}" do
|
101
140
|
use_vcr_cassette "dataset_#{source}_find_states"
|
102
141
|
let(:params) do
|
103
|
-
{
|
104
|
-
:
|
105
|
-
:
|
106
|
-
:source
|
107
|
-
:
|
108
|
-
:
|
142
|
+
{
|
143
|
+
key: api_key,
|
144
|
+
source: source,
|
145
|
+
vintage: source.match('sf1') ? 2010 : 2012,
|
146
|
+
fields: source.match('sf1') ? 'P0010001' : 'B00001_001E',
|
147
|
+
level: 'STATE',
|
148
|
+
within: []
|
109
149
|
}
|
110
150
|
end
|
111
151
|
|
@@ -117,19 +157,29 @@ describe CensusApi::Request do
|
|
117
157
|
end
|
118
158
|
end
|
119
159
|
|
120
|
-
context
|
121
|
-
describe
|
122
|
-
use_vcr_cassette
|
160
|
+
context '.vintage' do
|
161
|
+
describe 'vintage' do
|
162
|
+
use_vcr_cassette 'sf1_find_states_vintage'
|
163
|
+
|
164
|
+
let(:params) do
|
165
|
+
{
|
166
|
+
key: api_key,
|
167
|
+
source: 'acs5',
|
168
|
+
vintage: 2010,
|
169
|
+
fields: 'B00001_001E',
|
170
|
+
level: 'STATE',
|
171
|
+
within: []
|
172
|
+
}
|
173
|
+
end
|
123
174
|
|
124
175
|
before(:each) do
|
125
|
-
params = {:key=> api_key, :vintage => 2010, :fields => 'B00001_001E', :source => 'acs5', :level => 'STATE', :within=>[]}
|
126
176
|
@collection_2010 = CensusApi::Request.find(params[:source], params)
|
127
|
-
|
177
|
+
params.merge!(vintage: 2012)
|
178
|
+
@collection_2012 = CensusApi::Request.find(params[:source], params)
|
128
179
|
end
|
129
180
|
|
130
181
|
it 'should be valid' do
|
131
|
-
@collection_2010.count.should
|
132
|
-
@collection_2012.count.should == 52
|
182
|
+
@collection_2010.count.should eq(52)
|
133
183
|
end
|
134
184
|
|
135
185
|
it 'should not be same result set' do
|
@@ -138,21 +188,25 @@ describe CensusApi::Request do
|
|
138
188
|
end
|
139
189
|
end
|
140
190
|
|
141
|
-
context
|
191
|
+
context '#format' do
|
142
192
|
it 'should add wildcard after reformatting geography type without id' do
|
143
|
-
CensusApi::Request.format('COUSUB', false).should
|
193
|
+
CensusApi::Request.format('COUSUB', false).should
|
194
|
+
be('county+subdivision:*')
|
144
195
|
end
|
145
196
|
|
146
197
|
it 'should maintain geography id after reformatting geography type' do
|
147
|
-
CensusApi::Request.format('COUSUB:86690', false).should
|
198
|
+
CensusApi::Request.format('COUSUB:86690', false).should
|
199
|
+
be('county+subdivision:86690')
|
148
200
|
end
|
149
201
|
|
150
202
|
it 'should truncate geography type AIANNH' do
|
151
|
-
CensusApi::Request.format('AIANNH', true).should
|
203
|
+
CensusApi::Request.format('AIANNH', true).should
|
204
|
+
be('american+indian+area:*')
|
152
205
|
end
|
153
206
|
|
154
207
|
it 'should not truncate geography type CBSA' do
|
155
|
-
CensusApi::Request.format('CBSA', true).should
|
208
|
+
CensusApi::Request.format('CBSA', true).should
|
209
|
+
be('metropolitan+statistical+area/micropolitan+statistical+area:*')
|
156
210
|
end
|
157
211
|
end
|
158
|
-
end
|
212
|
+
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/vcr_setup.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'vcr'
|
2
2
|
|
3
3
|
VCR.config do |c|
|
4
|
-
c.cassette_library_dir
|
5
|
-
c.hook_into
|
6
|
-
c.ignore_localhost
|
7
|
-
c.default_cassette_options = { :
|
8
|
-
end
|
4
|
+
c.cassette_library_dir = 'spec/vcr_cassettes'
|
5
|
+
c.hook_into :fakeweb
|
6
|
+
c.ignore_localhost = true
|
7
|
+
c.default_cassette_options = { record: :new_episodes }
|
8
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: census_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ty Rauber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05
|
11
|
+
date: 2014-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: addressable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.3'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.3'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: hpricot
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +94,20 @@ dependencies:
|
|
80
94
|
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description: A Ruby Gem for querying the US Census Bureau API
|
84
112
|
email:
|
85
113
|
- tyrauber@mac.com
|