census_api 1.0.8 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a62e626a1fbdc56c582ddb174cc461bf115dc7e6
4
- data.tar.gz: 3e58b79d93058fc2f4395d27336ad2632d152c9d
3
+ metadata.gz: 2a7ed25157bf9e34b4bd73269a608cdd1d312f0e
4
+ data.tar.gz: 9de9fe0553987838d86f9c6621e5638a3222b096
5
5
  SHA512:
6
- metadata.gz: 5f6f49c3e3b567fac345a4fba23014fa2e52356436142bd4f2618fea8ec7568d6c383b7a82fc0d344907a7a28434736ad4dfc1ad3455167337b223a74c8ac3d7
7
- data.tar.gz: 06141acd1d396546659618f5333cd051c34ab021e46fea50357606acb8e6d226c716cf1c09d3e89dbab06a6cb6a357be0b804868f1b6b8b21e51dd66d764a549
6
+ metadata.gz: e08efa0b1fd2bef67d6cbcab59f3e5e39e0f0f1310558e71d998a737ad647e3fde7b1f3363c639271a86eaa9aad9fe975f859fd28e1a549537c5eb7e19376f11
7
+ data.tar.gz: 00d11868c1ff9cd83b7439c64a0a44f9bb979e63b6fceb915a07fa000b342c97f603b2107b8a3c9b7dc14717fae5d276aeaaa7438b1e54abf9ece9a28d95bcc2
data/.gitignore CHANGED
@@ -20,4 +20,6 @@ spec/api_key.rb
20
20
  test/tmp
21
21
  test/version_tmp
22
22
  tmp
23
- misc/*
23
+ misc/*
24
+ /.ruby-version
25
+ /.ruby-gemset
data/README.md CHANGED
@@ -61,13 +61,6 @@ Then, use `Client#where` with an options hash to query for Census data. The fiel
61
61
 
62
62
  ```
63
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')
68
-
69
- ```
70
-
71
64
  For a list of the fields available for each dataset, review the reference PDFs linked at the bottom of this document.
72
65
 
73
66
  #### Parameters
data/census_api.gemspec CHANGED
@@ -11,8 +11,8 @@ Gem::Specification.new do |gem|
11
11
  gem.email = ['tyrauber@mac.com']
12
12
  gem.description = 'A Ruby Gem for querying the US Census Bureau API'
13
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.'
14
+ providing the ability to query both the SF1 and ACS5
15
+ datasets.'
16
16
  gem.homepage = 'https://github.com/tyrauber/census_api.git'
17
17
 
18
18
  gem.files = `git ls-files`.split("\n")
@@ -21,10 +21,9 @@ Gem::Specification.new do |gem|
21
21
  .map { |f| File.basename(f) }
22
22
  gem.require_paths = ['lib']
23
23
 
24
- gem.add_runtime_dependency 'rest-client'
25
- gem.add_runtime_dependency 'hpricot'
24
+ gem.add_runtime_dependency 'http'
26
25
  gem.add_development_dependency 'rspec'
27
- gem.add_development_dependency 'fakeweb'
26
+ gem.add_development_dependency 'webmock', '< 2.0'
28
27
  gem.add_development_dependency 'vcr'
29
28
  gem.add_development_dependency 'rubocop'
30
29
  end
@@ -3,9 +3,9 @@ module CensusApi
3
3
  # client#initialize method takes an api_key and options hash,
4
4
  # which includes dataset and vintage. client#where method accepts
5
5
  # an options hash, including fields, level and within. Within is optional.
6
- # client#find takes positional arguments and is now deprecated.
7
6
  class Client
8
- require 'rest-client'
7
+ require 'http'
8
+ $census_connection = HTTP.persistent "http://api.census.gov"
9
9
 
10
10
  attr_reader :api_key, :api_vintage, :options
11
11
  attr_accessor :dataset
@@ -23,19 +23,6 @@ module CensusApi
23
23
  end
24
24
  end
25
25
 
26
- def find(fields, level, *within)
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
26
  def where(options={})
40
27
  options.merge!(key: @api_key, vintage: @api_vintage)
41
28
  fail "Client requires a dataset (#{DATASETS})." if @dataset.nil?
@@ -49,9 +36,9 @@ module CensusApi
49
36
  protected
50
37
 
51
38
  def validate_api_key(api_key)
52
- path = "http://api.census.gov/data/2010/sf1?key=#{api_key}&get=P0010001&for=state:01"
53
- response = RestClient.get path
54
- if response.body.include? 'Invalid Key'
39
+ path = "/data/2010/sf1?key=#{api_key}&get=P0010001&for=state:01"
40
+ response = $census_connection.get path
41
+ if response.to_s.include? 'Invalid Key'
55
42
  fail "'#{api_key}' is not a valid API key. Check your key for errors,
56
43
  or request a new one at census.gov."
57
44
  end
@@ -4,20 +4,16 @@ module CensusApi
4
4
  # client#find method accepts source and options hash, which include
5
5
  # :key, :fields, :level, :within and :vintage.
6
6
  class Request
7
- require 'restclient'
8
- require 'hpricot'
7
+ require 'http'
9
8
  require 'json'
10
9
  require 'yaml'
11
10
 
12
11
  attr_accessor :response
13
12
 
14
- CENSUS_URL = 'http://api.census.gov/data'
15
-
16
- def initialize(url, vintage, source, options)
17
- uri = "#{url}/#{vintage}/#{source}?#{to_params(options)}"
18
- @response = RestClient.get(uri.to_s) do |response, _req, _res, _blk|
19
- response
20
- end
13
+ def initialize(vintage, source, options)
14
+ uri = "/data/#{vintage}/#{source}?#{to_params(options)}"
15
+ @response = $census_connection.get(uri.to_s)
16
+ @response.flush
21
17
  end
22
18
 
23
19
  def self.find(source, options = {})
@@ -30,7 +26,7 @@ module CensusApi
30
26
  params.merge!(in: format(options[:within][0], true))
31
27
  end
32
28
  options.merge!(vintage: 2010) unless options[:vintage]
33
- request = new(CENSUS_URL, options[:vintage], source, params)
29
+ request = new(options[:vintage], source, params)
34
30
  request.parse_response
35
31
  end
36
32
 
@@ -1,4 +1,4 @@
1
1
  # => CensusApi::Version
2
2
  module CensusApi
3
- VERSION = '1.0.8'
3
+ VERSION = '1.1.0'
4
4
  end
@@ -1,36 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe CensusApi::Client do
3
+ describe CensusApi::Client, :vcr do
4
4
 
5
- describe 'client initialization' do
6
-
7
- use_vcr_cassette 'initialize_client'
5
+ describe 'initialize client' do
8
6
 
9
7
  it 'should not initialize without an api_key' do
10
- lambda { CensusApi::Client.new }.should raise_error
8
+ expect { CensusApi::Client.new }.to raise_error(ArgumentError)
11
9
  end
12
10
 
13
11
  it 'should initialize with an api_key' do
14
12
  @client = CensusApi::Client.new(api_key)
15
- @client.api_key.should == api_key
13
+ expect(@client.api_key).to eq(api_key)
16
14
  end
17
15
  end
18
16
 
19
- describe 'client and dataset initialization' do
20
-
21
- use_vcr_cassette 'initialize_client_and_dataset'
17
+ describe 'initialize client and dataset' do
22
18
 
23
19
  it 'should initialize with an api_key and dataset' do
24
20
  dataset = 'SF1'
25
21
  @client = CensusApi::Client.new(api_key, dataset: dataset)
26
- @client.api_key.should == api_key
27
- @client.dataset.should == dataset.downcase
22
+ expect(@client.api_key).to eq(api_key)
23
+ expect(@client.dataset).to eq(dataset.downcase)
28
24
  end
29
25
  end
30
26
 
31
27
  describe 'datasets' do
32
28
 
33
- use_vcr_cassette 'find_method'
34
29
  describe 'sf1' do
35
30
  let(:source) { 'sf1' }
36
31
  let(:options) do
@@ -43,7 +38,7 @@ describe CensusApi::Client do
43
38
 
44
39
  it 'should request sf1' do
45
40
  @client = CensusApi::Client.new(api_key, dataset: source)
46
- CensusApi::Request.should_receive(:find).with(@client.dataset, options)
41
+ expect(CensusApi::Request).to receive(:find).with(@client.dataset, options)
47
42
  @client.where(options)
48
43
  end
49
44
  end
@@ -60,15 +55,13 @@ describe CensusApi::Client do
60
55
 
61
56
  it 'should request acs5' do
62
57
  @client = CensusApi::Client.new(api_key, dataset: source)
63
- CensusApi::Request.should_receive(:find).with(@client.dataset, options)
58
+ expect(CensusApi::Request).to receive(:find).with(@client.dataset, options)
64
59
  @client.where(options)
65
60
  end
66
61
  end
67
62
  end
68
63
 
69
- describe '#find' do
70
-
71
- use_vcr_cassette 'find_method'
64
+ describe '#where' do
72
65
 
73
66
  let(:source) { 'sf1' }
74
67
  let(:options) do
@@ -79,29 +72,6 @@ describe CensusApi::Client do
79
72
  within: [] }
80
73
  end
81
74
 
82
- it 'should be deprecated' do
83
- @client = CensusApi::Client.new(api_key, dataset: source)
84
- @client.should_receive(:warn)
85
- .with('[DEPRECATION] `find` is deprecated. Please use `where` instead.')
86
- @client.find(options[:fields], options[:level])
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
75
  let(:full_params) do
106
76
  options.merge!(level: 'COUNTY:001', within: 'STATE:06')
107
77
  end
@@ -120,14 +90,14 @@ describe CensusApi::Client do
120
90
 
121
91
  it 'should request sf1 with valid fields and level params' do
122
92
  @client = CensusApi::Client.new(api_key, dataset: source)
123
- CensusApi::Request.should_receive(:find)
93
+ expect(CensusApi::Request).to receive(:find)
124
94
  .with(@client.dataset, options)
125
95
  expect { @client.where(options) }.not_to raise_error
126
96
  end
127
97
 
128
98
  it 'should request sf1 with valid fields, level and within params' do
129
99
  @client = CensusApi::Client.new(api_key, dataset: source)
130
- CensusApi::Request.should_receive(:find)
100
+ expect(CensusApi::Request).to receive(:find)
131
101
  .with(@client.dataset, full_params)
132
102
  expect { @client.where(full_params) }.not_to raise_error
133
103
  end
@@ -1,56 +1,35 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'CensusApi::Examples' do
4
- use_vcr_cassette 'census_api examples'
3
+ describe 'CensusApi::Examples', :vcr do
5
4
  let(:client) { CensusApi::Client.new(api_key) }
6
5
 
7
6
  describe 'sf1' do
8
7
  before(:each) do
9
8
  client.dataset = 'sf1'
10
9
  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
10
  describe '#where' do
22
11
  CensusExamples::SF1.each do |query|
23
12
  it "should retrieve #{query.join(",")}" do
24
13
  response = client.send(:where, {fields: query[0], level: query[1], within: query[2]})
25
14
  expect{ response }.not_to raise_error
26
- response.should be_a(Array)
27
- response.first.should include('name')
15
+ expect(response).to be_a(Array)
16
+ expect(response.first).to include('name')
28
17
  end
29
18
  end
30
19
  end
31
20
  end
32
-
21
+
33
22
  describe 'acs5' do
34
23
  before(:each) do
35
24
  client.dataset = 'acs5'
36
25
  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
26
  describe '#where' do
48
27
  CensusExamples::ACS5.each do |query|
49
28
  it "should retrieve #{query.join(",")}" do
50
29
  response = client.send(:where, {fields: query[0], level: query[1], within: query[2]})
51
30
  expect{ response }.not_to raise_error
52
- response.should be_a(Array)
53
- response.first.should include('name')
31
+ expect(response).to be_a(Array)
32
+ expect(response.first).to include('name')
54
33
  end
55
34
  end
56
35
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe CensusApi::Request do
3
+ describe CensusApi::Request, :vcr do
4
4
 
5
5
  context '#find' do
6
6
  [{source: 'sf1', field: 'P0010001', results: [
@@ -14,7 +14,6 @@ describe CensusApi::Request do
14
14
  ].each do |test|
15
15
 
16
16
  describe "#{test[:source]} for a geography type" do
17
- use_vcr_cassette "#{test[:source]}_find_states"
18
17
 
19
18
  let(:params) do
20
19
  {
@@ -32,20 +31,19 @@ describe CensusApi::Request do
32
31
  end
33
32
 
34
33
  it 'should have 52 results' do
35
- @collection.count.should == 52
34
+ expect(@collection.count).to eq(52)
36
35
  end
37
36
 
38
37
  it 'should include fields for each result' do
39
38
  @collection.each do |result|
40
- result.should include(test[:field])
41
- result.should include('name')
42
- result.should include('state')
39
+ expect(result).to include(test[:field])
40
+ expect(result).to include('name')
41
+ expect(result).to include('state')
43
42
  end
44
43
  end
45
44
  end
46
45
 
47
46
  describe "#{test[:source]} for a geography type and id" do
48
- use_vcr_cassette "#{test[:source]}_find_state_with_id"
49
47
  let(:params) do
50
48
  {
51
49
  key: api_key,
@@ -62,18 +60,17 @@ describe CensusApi::Request do
62
60
  end
63
61
 
64
62
  it 'should have one result' do
65
- @collection.count.should == 1
63
+ expect(@collection.count).to eq(1)
66
64
  end
67
65
 
68
66
  it 'should include fields for each result' do
69
67
  @collection.each do |result|
70
- result.should == test[:results][0]
68
+ expect(result).to eq(test[:results][0])
71
69
  end
72
70
  end
73
71
  end
74
72
 
75
73
  describe "#{test[:source]} for a geography type" do
76
- use_vcr_cassette "#{test[:source]}_find_counties_in_state"
77
74
 
78
75
  let(:params) do
79
76
  {
@@ -91,20 +88,19 @@ describe CensusApi::Request do
91
88
  end
92
89
 
93
90
  it 'should have one result' do
94
- @collection.count.should == 58
91
+ expect(@collection.count).to eq(58)
95
92
  end
96
93
 
97
94
  it 'should include fields for each result' do
98
95
  @collection.each do |result|
99
- result.should include(test[:field])
100
- result.should include('name')
101
- result.should include('state')
96
+ expect(result).to include(test[:field])
97
+ expect(result).to include('name')
98
+ expect(result).to include('state')
102
99
  end
103
100
  end
104
101
  end
105
102
 
106
103
  describe "#{test[:source]} for a geography" do
107
- use_vcr_cassette "#{test[:source]}_find_county_in_state"
108
104
 
109
105
  let(:params) do
110
106
  {
@@ -122,12 +118,12 @@ describe CensusApi::Request do
122
118
  end
123
119
 
124
120
  it 'should have one result' do
125
- @collection.count.should == 1
121
+ expect(@collection.count).to eq(1)
126
122
  end
127
123
 
128
124
  it 'should include fields for each result' do
129
125
  @collection.each do |result|
130
- result.should == test[:results][1]
126
+ expect(result).to eq(test[:results][1])
131
127
  end
132
128
  end
133
129
  end
@@ -137,7 +133,6 @@ describe CensusApi::Request do
137
133
  context 'DATASETS' do
138
134
  CensusApi::Client::DATASETS.each do |source|
139
135
  describe "#{source}" do
140
- use_vcr_cassette "dataset_#{source}_find_states"
141
136
  let(:params) do
142
137
  {
143
138
  key: api_key,
@@ -151,7 +146,7 @@ describe CensusApi::Request do
151
146
 
152
147
  it "#{source} should be valid" do
153
148
  @collection = CensusApi::Request.find(source, params)
154
- @collection.count.should == 52
149
+ expect(@collection.count).to eq(52)
155
150
  end
156
151
  end
157
152
  end
@@ -159,7 +154,6 @@ describe CensusApi::Request do
159
154
 
160
155
  context '.vintage' do
161
156
  describe 'vintage' do
162
- use_vcr_cassette 'sf1_find_states_vintage'
163
157
 
164
158
  let(:params) do
165
159
  {
@@ -179,34 +173,30 @@ describe CensusApi::Request do
179
173
  end
180
174
 
181
175
  it 'should be valid' do
182
- @collection_2010.count.should eq(52)
176
+ expect(@collection_2010.count).to eq(52)
183
177
  end
184
178
 
185
179
  it 'should not be same result set' do
186
- @collection_2010.should_not == @collection_2012
180
+ expect(@collection_2010).not_to eq(@collection_2012)
187
181
  end
188
182
  end
189
183
  end
190
184
 
191
185
  context '#format' do
192
186
  it 'should add wildcard after reformatting geography type without id' do
193
- CensusApi::Request.format('COUSUB', false).should
194
- be('county+subdivision:*')
187
+ expect(CensusApi::Request.format('COUSUB', false)).to eq('county+subdivision:*')
195
188
  end
196
189
 
197
190
  it 'should maintain geography id after reformatting geography type' do
198
- CensusApi::Request.format('COUSUB:86690', false).should
199
- be('county+subdivision:86690')
191
+ expect(CensusApi::Request.format('COUSUB:86690', false)).to eq('county+subdivision:86690')
200
192
  end
201
193
 
202
194
  it 'should truncate geography type AIANNH' do
203
- CensusApi::Request.format('AIANNH', true).should
204
- be('american+indian+area:*')
195
+ expect(CensusApi::Request.format('AIANNH', true)).to eq('american+indian+area:*')
205
196
  end
206
197
 
207
198
  it 'should not truncate geography type CBSA' do
208
- CensusApi::Request.format('CBSA', true).should
209
- be('metropolitan+statistical+area/micropolitan+statistical+area:*')
199
+ expect(CensusApi::Request.format('CBSA', true)).to eq('metropolitan+statistical+area/micropolitan+statistical+area:*')
210
200
  end
211
201
  end
212
202
  end
data/spec/spec_helper.rb CHANGED
@@ -8,7 +8,3 @@ require 'support/census_examples'
8
8
  def api_key
9
9
  RSPEC_API_KEY
10
10
  end
11
-
12
- RSpec.configure do |config|
13
- config.extend VCR::RSpec::Macros
14
- end
data/spec/vcr_setup.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'vcr'
2
2
 
3
- VCR.config do |c|
3
+ VCR.configure do |c|
4
4
  c.cassette_library_dir = 'spec/vcr_cassettes'
5
- c.hook_into :fakeweb
6
- c.ignore_localhost = true
5
+ c.configure_rspec_metadata!
6
+ c.hook_into :webmock
7
7
  c.default_cassette_options = { record: :new_episodes }
8
8
  end
metadata CHANGED
@@ -1,31 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: census_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.1.0
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-07-05 00:00:00.000000000 Z
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rest-client
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: hpricot
14
+ name: http
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - ">="
@@ -53,19 +39,19 @@ dependencies:
53
39
  - !ruby/object:Gem::Version
54
40
  version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
- name: fakeweb
42
+ name: webmock
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
- - - ">="
45
+ - - "<"
60
46
  - !ruby/object:Gem::Version
61
- version: '0'
47
+ version: '2.0'
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
- - - ">="
52
+ - - "<"
67
53
  - !ruby/object:Gem::Version
68
- version: '0'
54
+ version: '2.0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: vcr
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -139,11 +125,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
125
  version: '0'
140
126
  requirements: []
141
127
  rubyforge_project:
142
- rubygems_version: 2.2.2
128
+ rubygems_version: 2.5.1
143
129
  signing_key:
144
130
  specification_version: 4
145
131
  summary: A Ruby Wrapper for the US Census Bureau API, providing the ability to query
146
- both the 2010 Census and 2006-2010 ACS5 datasets.
132
+ both the SF1 and ACS5 datasets.
147
133
  test_files:
148
134
  - spec/api_key.sample.rb
149
135
  - spec/census_api/client_spec.rb