census_api 1.0.7 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/census_api.gemspec +0 -1
- data/lib/census_api/client.rb +5 -4
- data/lib/census_api/request.rb +8 -4
- data/lib/census_api/version.rb +1 -1
- data/lib/census_api.rb +0 -1
- data/spec/census_api/example_spec.rb +58 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/census_examples.rb +84 -0
- metadata +5 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a62e626a1fbdc56c582ddb174cc461bf115dc7e6
|
4
|
+
data.tar.gz: 3e58b79d93058fc2f4395d27336ad2632d152c9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f6f49c3e3b567fac345a4fba23014fa2e52356436142bd4f2618fea8ec7568d6c383b7a82fc0d344907a7a28434736ad4dfc1ad3455167337b223a74c8ac3d7
|
7
|
+
data.tar.gz: 06141acd1d396546659618f5333cd051c34ab021e46fea50357606acb8e6d226c716cf1c09d3e89dbab06a6cb6a357be0b804868f1b6b8b21e51dd66d764a549
|
data/README.md
CHANGED
@@ -57,14 +57,14 @@ To query the 2006-2010 ACS5 dataset, set the dataset to 'ACS5':
|
|
57
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.where({ fields: 'P0010001', level: 'COUNTY:001', within:
|
60
|
+
@client.where({ fields: 'P0010001', level: 'COUNTY:001', within: 'STATE:06' })
|
61
61
|
|
62
62
|
```
|
63
63
|
|
64
64
|
The `Client.find` method which takes positional arguments is still available, but deprecated.
|
65
65
|
|
66
66
|
```ruby
|
67
|
-
@client.find('P0010001', 'COUNTY:001',
|
67
|
+
@client.find('P0010001', 'COUNTY:001', 'STATE:06')
|
68
68
|
|
69
69
|
```
|
70
70
|
|
@@ -274,7 +274,7 @@ Retrieve fields for all Counties within a specific Congressional District:
|
|
274
274
|
|
275
275
|
Retrieve fields for a specific County within a specific Congressional District:
|
276
276
|
|
277
|
-
`@client.where({ fields: 'P0010001', level: 'COUNTY:
|
277
|
+
`@client.where({ fields: 'P0010001', level: 'COUNTY:003', within: 'STATE:24+CD:01' })`
|
278
278
|
|
279
279
|
#### TRACT (Remainder) - *(511) state-congressional district-county-tract*
|
280
280
|
|
@@ -314,7 +314,7 @@ Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home La
|
|
314
314
|
|
315
315
|
Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific Congressional District:
|
316
316
|
|
317
|
-
`@client.where({ fields: 'P0010001', level: 'AIANNH:
|
317
|
+
__DOES NOT WORK__: `@client.where({ fields: 'P0010001', level: 'AIANNH:0010', within: 'STATE:02+CD:00' })`
|
318
318
|
|
319
319
|
#### ANRC (Remainder) - *(560) state-congressional district-alaska native regional corporation*
|
320
320
|
|
@@ -476,7 +476,7 @@ Retrieve fields for all American Indian Area/Alaska Native Area/Hawaiian Home La
|
|
476
476
|
|
477
477
|
Retrieve fields for a specific American Indian Area/Alaska Native Area/Hawaiian Home Land within a specific State:
|
478
478
|
|
479
|
-
`@client.where({ fields: 'B00001_001E', level: 'AIANNH:03800', within: 'STATE:02' })`
|
479
|
+
__DOES NOT WORK__: `@client.where({ fields: 'B00001_001E', level: 'AIANNH:03800', within: 'STATE:02' })`
|
480
480
|
|
481
481
|
#### CD - *(500) state-congressional district*
|
482
482
|
|
data/census_api.gemspec
CHANGED
@@ -22,7 +22,6 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.require_paths = ['lib']
|
23
23
|
|
24
24
|
gem.add_runtime_dependency 'rest-client'
|
25
|
-
gem.add_runtime_dependency 'addressable', '~> 2.3'
|
26
25
|
gem.add_runtime_dependency 'hpricot'
|
27
26
|
gem.add_development_dependency 'rspec'
|
28
27
|
gem.add_development_dependency 'fakeweb'
|
data/lib/census_api/client.rb
CHANGED
@@ -36,20 +36,21 @@ module CensusApi
|
|
36
36
|
Request.find(dataset, options)
|
37
37
|
end
|
38
38
|
|
39
|
-
def where(options
|
39
|
+
def where(options={})
|
40
|
+
options.merge!(key: @api_key, vintage: @api_vintage)
|
40
41
|
fail "Client requires a dataset (#{DATASETS})." if @dataset.nil?
|
41
42
|
[:fields, :level].each do |f|
|
42
43
|
fail ArgumentError, "#{f} is a requied parameter" if options[f].nil?
|
43
44
|
end
|
45
|
+
options[:within] = [options[:within]]
|
44
46
|
Request.find(dataset, options)
|
45
47
|
end
|
46
48
|
|
47
49
|
protected
|
48
50
|
|
49
51
|
def validate_api_key(api_key)
|
50
|
-
|
51
|
-
|
52
|
-
response = RestClient.get uri.to_s
|
52
|
+
path = "http://api.census.gov/data/2010/sf1?key=#{api_key}&get=P0010001&for=state:01"
|
53
|
+
response = RestClient.get path
|
53
54
|
if response.body.include? 'Invalid Key'
|
54
55
|
fail "'#{api_key}' is not a valid API key. Check your key for errors,
|
55
56
|
or request a new one at census.gov."
|
data/lib/census_api/request.rb
CHANGED
@@ -14,8 +14,7 @@ module CensusApi
|
|
14
14
|
CENSUS_URL = 'http://api.census.gov/data'
|
15
15
|
|
16
16
|
def initialize(url, vintage, source, options)
|
17
|
-
uri =
|
18
|
-
uri.query_values = options
|
17
|
+
uri = "#{url}/#{vintage}/#{source}?#{to_params(options)}"
|
19
18
|
@response = RestClient.get(uri.to_s) do |response, _req, _res, _blk|
|
20
19
|
response
|
21
20
|
end
|
@@ -27,7 +26,9 @@ module CensusApi
|
|
27
26
|
fields = fields.push('NAME').join(',') if fields.is_a? Array
|
28
27
|
level = format(options[:level], false)
|
29
28
|
params = { key: options[:key], get: fields, for: level }
|
30
|
-
|
29
|
+
unless (options[:within].nil? || (options[:within].is_a?(Array) && options[:within].compact.empty?))
|
30
|
+
params.merge!(in: format(options[:within][0], true))
|
31
|
+
end
|
31
32
|
options.merge!(vintage: 2010) unless options[:vintage]
|
32
33
|
request = new(CENSUS_URL, options[:vintage], source, params)
|
33
34
|
request.parse_response
|
@@ -42,6 +43,10 @@ module CensusApi
|
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
46
|
+
def to_params(options)
|
47
|
+
options.map { |k,v| "#{k}=#{v}" }.join("&")
|
48
|
+
end
|
49
|
+
|
45
50
|
protected
|
46
51
|
|
47
52
|
def response_success(response)
|
@@ -55,7 +60,6 @@ module CensusApi
|
|
55
60
|
def response_error(response)
|
56
61
|
{
|
57
62
|
code: response.code,
|
58
|
-
message: 'Invalid API key or request',
|
59
63
|
location: response.headers[:location],
|
60
64
|
body: response.body
|
61
65
|
}
|
data/lib/census_api/version.rb
CHANGED
data/lib/census_api.rb
CHANGED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'CensusApi::Examples' do
|
4
|
+
use_vcr_cassette 'census_api examples'
|
5
|
+
let(:client) { CensusApi::Client.new(api_key) }
|
6
|
+
|
7
|
+
describe 'sf1' do
|
8
|
+
before(:each) do
|
9
|
+
client.dataset = 'sf1'
|
10
|
+
end
|
11
|
+
describe '#find' do
|
12
|
+
CensusExamples::SF1.each do |query|
|
13
|
+
it "should retrieve #{query.join(",")}" do
|
14
|
+
response = client.send(:find, query[0], query[1], query[2])
|
15
|
+
expect{ response }.not_to raise_error
|
16
|
+
response.should be_a(Array)
|
17
|
+
response.first.should include('name')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
describe '#where' do
|
22
|
+
CensusExamples::SF1.each do |query|
|
23
|
+
it "should retrieve #{query.join(",")}" do
|
24
|
+
response = client.send(:where, {fields: query[0], level: query[1], within: query[2]})
|
25
|
+
expect{ response }.not_to raise_error
|
26
|
+
response.should be_a(Array)
|
27
|
+
response.first.should include('name')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'acs5' do
|
34
|
+
before(:each) do
|
35
|
+
client.dataset = 'acs5'
|
36
|
+
end
|
37
|
+
describe '#find' do
|
38
|
+
CensusExamples::ACS5.each do |query|
|
39
|
+
it "should retrieve #{query.join(",")}" do
|
40
|
+
response = client.send(:find, query[0], query[1], query[2])
|
41
|
+
expect{ response }.not_to raise_error
|
42
|
+
response.should be_a(Array)
|
43
|
+
response.first.should include('name')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
describe '#where' do
|
48
|
+
CensusExamples::ACS5.each do |query|
|
49
|
+
it "should retrieve #{query.join(",")}" do
|
50
|
+
response = client.send(:where, {fields: query[0], level: query[1], within: query[2]})
|
51
|
+
expect{ response }.not_to raise_error
|
52
|
+
response.should be_a(Array)
|
53
|
+
response.first.should include('name')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,84 @@
|
|
1
|
+
module CensusExamples
|
2
|
+
SF1 = [
|
3
|
+
['P0010001', 'STATE'],
|
4
|
+
['P0010001', 'STATE:06'],
|
5
|
+
['P0010001', 'STATE:02,06'],
|
6
|
+
['P0010001', 'COUNTY'],
|
7
|
+
['P0010001', 'COUNTY', 'STATE:06'],
|
8
|
+
['P0010001', 'COUNTY:001', 'STATE:06'],
|
9
|
+
['P0010001', 'COUSUB', 'STATE:02+COUNTY:290'],
|
10
|
+
['P0010001', 'COUSUB:86690', 'STATE:02+COUNTY:290'],
|
11
|
+
['P0010001', 'SUBMCD', 'STATE:72+COUNTY:127+COUSUB:79693'],
|
12
|
+
['P0010001', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693'],
|
13
|
+
['P0010001', 'TABBLOCK', 'STATE:02+COUNTY:290+TRACT:00100'],
|
14
|
+
['P0010001', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693'],
|
15
|
+
['P0010001', 'TRACT', 'STATE:02+COUNTY:170'],
|
16
|
+
['P0010001', 'TRACT:000101', 'STATE:02+COUNTY:170'],
|
17
|
+
['P0010001', 'BG', 'STATE:02+COUNTY:170+TRACT:000101'],
|
18
|
+
['P0010001', 'BG:1', 'STATE:02+COUNTY:170+TRACT:000101'],
|
19
|
+
['P0010001', 'PLACE'],
|
20
|
+
['P0010001', 'PLACE', 'STATE:06'],
|
21
|
+
['P0010001', 'PLACE:00135', 'STATE:06'],
|
22
|
+
['P0010001', 'ANRC'],
|
23
|
+
['P0010001', 'ANRC', 'STATE:02'],
|
24
|
+
['P0010001', 'ANRC:00590', 'STATE:02'],
|
25
|
+
['P0010001', 'AIANNH'],
|
26
|
+
['P0010001', 'AIANNH', 'STATE:02'],
|
27
|
+
['P0010001', 'AIANNH:03800', 'STATE:02'],
|
28
|
+
['P0010001', 'AITS', 'STATE:40+AIANNH:13735'],
|
29
|
+
['P0010001', 'AITS:83127', 'STATE:40+AIANNH:13735'],
|
30
|
+
['P0010001', 'CBSA', 'STATE:02'],
|
31
|
+
['P0010001', 'CBSA:11260', 'STATE:02'],
|
32
|
+
['P0010001', 'METDIV', 'STATE:06+CBSA:31100'],
|
33
|
+
['P0010001', 'METDIV', 'STATE:06+CBSA:31100'],
|
34
|
+
['P0010001', 'METDIV:31084', 'STATE:06+CBSA:31100'],
|
35
|
+
['P0010001', 'CSA', 'STATE:24'],
|
36
|
+
['P0010001', 'CSA:428', 'STATE:24'],
|
37
|
+
['P0010001', 'CD', 'STATE:24'],
|
38
|
+
['P0010001', 'CD:01', 'STATE:24'],
|
39
|
+
['P0010001', 'COUNTY', 'STATE:24+CD:01'],
|
40
|
+
['P0010001', 'COUNTY:003', 'STATE:24+CD:01'],
|
41
|
+
['P0010001', 'TRACT', 'STATE:24+CD:01+COUNTY:003'],
|
42
|
+
['P0010001', 'TRACT:702100', 'STATE:24+CD:01+COUNTY:003'],
|
43
|
+
['P0010001', 'COUSUB', 'STATE:24+CD:01+COUNTY:003'],
|
44
|
+
['P0010001', 'COUSUB:90100', 'STATE:24+CD:01+COUNTY:003'],
|
45
|
+
['P0010001', 'PLACE', 'STATE:24+CD:01'],
|
46
|
+
['P0010001', 'PLACE:00125', 'STATE:24+CD:01'],
|
47
|
+
['P0010001', 'AIANNH', 'STATE:02+CD:00'],
|
48
|
+
['P0010001', 'AIANNH:03800', 'STATE:02+CD:00'],
|
49
|
+
['P0010001', 'AIANNH', 'STATE:02+CD:00'],
|
50
|
+
['P0010001', 'SLDU', 'STATE:02'],
|
51
|
+
['P0010001', 'SLDU:00A', 'STATE:02'],
|
52
|
+
['P0010001', 'SLDL', 'STATE:02'],
|
53
|
+
['P0010001', 'SLDL:001', 'STATE:02'],
|
54
|
+
['P0010001', 'ZCTA5', 'STATE:02'],
|
55
|
+
['P0010001', 'ZCTA5:99501', 'STATE:02']
|
56
|
+
]
|
57
|
+
ACS5 = [
|
58
|
+
['B00001_001E', 'STATE'],
|
59
|
+
['B00001_001E', 'STATE:06'],
|
60
|
+
['B00001_001E', 'STATE:06,02'],
|
61
|
+
['B00001_001E', 'COUNTY'],
|
62
|
+
['B00001_001E', 'COUNTY', 'STATE:06'],
|
63
|
+
['B00001_001E', 'COUNTY:001', 'STATE:06'],
|
64
|
+
['B00001_001E', 'COUSUB', 'STATE:02+COUNTY:290'],
|
65
|
+
['B00001_001E', 'COUSUB:86690', 'STATE:02+COUNTY:290'],
|
66
|
+
['B00001_001E', 'SUBMCD', 'STATE:72+COUNTY:127+COUSUB:79693'],
|
67
|
+
['B00001_001E', 'SUBMCD:02350', 'STATE:72+COUNTY:127+COUSUB:79693'],
|
68
|
+
['B00001_001E', 'PLACE', 'STATE:02+COUNTY:290+COUSUB:86690'],
|
69
|
+
['B00001_001E', 'PLACE:05750', 'STATE:02+COUNTY:290+COUSUB:86690'],
|
70
|
+
['B00001_001E', 'TRACT', 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750'],
|
71
|
+
['B00001_001E', 'TRACT:000100', 'STATE:02+COUNTY:290+COUSUB:86690+PLACE:05750'],
|
72
|
+
['B00001_001E', 'TRACT', 'STATE:02+COUNTY:170'],
|
73
|
+
['B00001_001E', 'TRACT:000101', 'STATE:02+COUNTY:170'],
|
74
|
+
['B00001_001E', 'BG', 'STATE:02+COUNTY:170+TRACT:000101'],
|
75
|
+
['B00001_001E', 'BG:1', 'STATE:02+COUNTY:170+TRACT:000101'],
|
76
|
+
['B00001_001E', 'PLACE'],
|
77
|
+
['B00001_001E', 'PLACE', 'STATE:06'],
|
78
|
+
['B00001_001E', 'PLACE:00135', 'STATE:06'],
|
79
|
+
['B00001_001E', 'AIANNH'],
|
80
|
+
['B00001_001E', 'AIANNH', 'STATE:02'],
|
81
|
+
['B00001_001E', 'CD', 'STATE:24'],
|
82
|
+
['B00001_001E', 'CD:01', 'STATE:24']
|
83
|
+
]
|
84
|
+
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.
|
4
|
+
version: 1.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ty Rauber
|
@@ -24,20 +24,6 @@ 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'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: hpricot
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -128,8 +114,10 @@ files:
|
|
128
114
|
- lib/yml/census_shapes.yml
|
129
115
|
- spec/api_key.sample.rb
|
130
116
|
- spec/census_api/client_spec.rb
|
117
|
+
- spec/census_api/example_spec.rb
|
131
118
|
- spec/census_api/request_spec.rb
|
132
119
|
- spec/spec_helper.rb
|
120
|
+
- spec/support/census_examples.rb
|
133
121
|
- spec/vcr_setup.rb
|
134
122
|
homepage: https://github.com/tyrauber/census_api.git
|
135
123
|
licenses:
|
@@ -159,6 +147,8 @@ summary: A Ruby Wrapper for the US Census Bureau API, providing the ability to q
|
|
159
147
|
test_files:
|
160
148
|
- spec/api_key.sample.rb
|
161
149
|
- spec/census_api/client_spec.rb
|
150
|
+
- spec/census_api/example_spec.rb
|
162
151
|
- spec/census_api/request_spec.rb
|
163
152
|
- spec/spec_helper.rb
|
153
|
+
- spec/support/census_examples.rb
|
164
154
|
- spec/vcr_setup.rb
|