usatoday-census 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README.md +70 -0
- data/Rakefile +21 -0
- data/lib/usatoday-census.rb +5 -0
- data/lib/usatoday-census/base.rb +86 -0
- data/lib/usatoday-census/ethnicity.rb +40 -0
- data/lib/usatoday-census/exceptions.rb +38 -0
- data/lib/usatoday-census/housing.rb +39 -0
- data/lib/usatoday-census/location.rb +59 -0
- data/lib/usatoday-census/population.rb +40 -0
- data/lib/usatoday-census/query.rb +28 -0
- data/lib/usatoday-census/race.rb +43 -0
- data/lib/usatoday-census/version.rb +5 -0
- data/test/test_helper.rb +17 -0
- data/test/usatoday-census/test_ethnicity.rb +36 -0
- data/test/usatoday-census/test_housing.rb +36 -0
- data/test/usatoday-census/test_location.rb +36 -0
- data/test/usatoday-census/test_population.rb +36 -0
- data/test/usatoday-census/test_race.rb +36 -0
- data/usatoday-census.gemspec +24 -0
- metadata +120 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# usatoday-census
|
2
|
+
|
3
|
+
|
4
|
+
* [Source](http://github.com/dwillis/usatoday-census)
|
5
|
+
|
6
|
+
## DESCRIPTION:
|
7
|
+
|
8
|
+
Simple ruby wrapper for the [USA TODAY Census API](http://developer.usatoday.com/docs/read/Census). You'll need an API key.
|
9
|
+
|
10
|
+
## INSTALL:
|
11
|
+
|
12
|
+
* <tt>gem install usatoday-census</tt>
|
13
|
+
|
14
|
+
## USAGE:
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
require 'usatoday-census'
|
18
|
+
include Usatoday::Census
|
19
|
+
|
20
|
+
You'll want to set your API key as an environment variable in order to run the tests. Otherwise, you'll need to set it like so:
|
21
|
+
|
22
|
+
Base.api_key = YOUR_API_KEY
|
23
|
+
|
24
|
+
or via an environment variable:
|
25
|
+
|
26
|
+
API_KEY = ENV['USAT_CENSUS_API_KEY']
|
27
|
+
Base.api_key = API_KEY
|
28
|
+
|
29
|
+
The gem queries the USA TODAY Census API to return information about a state's population, ethnicity, housing, racial composition and other geographic details, including the USA TODAY Diversity Index. The initial version of the gem supports state-level requests, so the results will be for states only. Future support for other geographic levels (National, County and City or Town) will be added soon. Users can search for a state by its postal abbreviation or name:
|
30
|
+
|
31
|
+
Location.search('va') # find demographic information about Virginia
|
32
|
+
Ethnicity.search("Virginia", "Placename") # can also search by full state name using the Placename attribute.
|
33
|
+
Race.search("51", 'FIPS') # Or search by FIPS code, too.
|
34
|
+
|
35
|
+
Check out the tests for further examples. Run the tests via rake test. Note: you'll need to set your API key as an environment variable before running the tests. The API has a limit of two queries per second, so the test requests are delayed by one second to ensure passage.
|
36
|
+
|
37
|
+
## Note on Patches/Pull Requests
|
38
|
+
|
39
|
+
* Fork the project.
|
40
|
+
* Make your feature addition or bug fix.
|
41
|
+
* Add tests for it. This is important so I don't break it in a
|
42
|
+
future version unintentionally.
|
43
|
+
* Commit, do not mess with rakefile, version, or history.
|
44
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
45
|
+
* Send me a pull request. Bonus points for topic branches.
|
46
|
+
|
47
|
+
## LICENSE:
|
48
|
+
|
49
|
+
(The MIT License)
|
50
|
+
|
51
|
+
Copyright (c) 2011 Derek Willis
|
52
|
+
|
53
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
54
|
+
a copy of this software and associated documentation files (the
|
55
|
+
'Software'), to deal in the Software without restriction, including
|
56
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
57
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
58
|
+
permit persons to whom the Software is furnished to do so, subject to
|
59
|
+
the following conditions:
|
60
|
+
|
61
|
+
The above copyright notice and this permission notice shall be
|
62
|
+
included in all copies or substantial portions of the Software.
|
63
|
+
|
64
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
65
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
66
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
67
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
68
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
69
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
70
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
Rake::TestTask.new(:test) do |test|
|
6
|
+
test.libs << 'lib' << 'test'
|
7
|
+
test.pattern = 'test/**/test_*.rb'
|
8
|
+
test.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :test
|
12
|
+
|
13
|
+
require 'rake/rdoctask'
|
14
|
+
Rake::RDocTask.new do |rdoc|
|
15
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
16
|
+
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = "campaign_cash #{version}"
|
19
|
+
rdoc.rdoc_files.include('README*')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'json'
|
3
|
+
require 'htmlentities'
|
4
|
+
|
5
|
+
module Usatoday
|
6
|
+
module Census
|
7
|
+
class Base
|
8
|
+
|
9
|
+
API_SERVER = "api.usatoday.com"
|
10
|
+
API_NAME = "census"
|
11
|
+
API_BASE = "/open/#{API_NAME}"
|
12
|
+
|
13
|
+
@@api_key = nil
|
14
|
+
@@debug = false
|
15
|
+
@@decode_html_entities = true
|
16
|
+
|
17
|
+
def self.api_key=(key)
|
18
|
+
@@api_key = key
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.api_key
|
22
|
+
@@api_key
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.build_request_url(method, params)
|
26
|
+
URI::HTTP.build :host => API_SERVER,
|
27
|
+
:path => API_BASE + '/' + method,
|
28
|
+
:query => params.map {|k,v| "#{URI.escape(k)}=#{URI.escape(v.to_s)}"}.join('&')
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.text_field(value)
|
32
|
+
return nil if value.nil?
|
33
|
+
@@decode_html_entities ? HTMLEntities.new.decode(value) : value
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.integer_field(value)
|
37
|
+
return nil if value.nil?
|
38
|
+
value.to_i
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.float_field(value)
|
42
|
+
return nil if value.nil?
|
43
|
+
value.to_f
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.prepare_params(method, keyname=nil)
|
47
|
+
params = {"keypath" => method }
|
48
|
+
params["keyname"] = keyname if keyname
|
49
|
+
params
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def self.invoke(method, params={})
|
54
|
+
begin
|
55
|
+
raise AuthenticationError, "You must initialize the API key before you run any API queries" if @@api_key.nil?
|
56
|
+
|
57
|
+
full_params = params.merge 'api_key' => @@api_key
|
58
|
+
uri = build_request_url(method, full_params)
|
59
|
+
puts "REQUEST: #{uri}" if @@debug
|
60
|
+
|
61
|
+
reply = uri.read
|
62
|
+
parsed_reply = JSON.parse reply
|
63
|
+
|
64
|
+
raise BadResponseError, "Empty reply returned from API" if parsed_reply.nil?
|
65
|
+
parsed_reply['response'].first
|
66
|
+
rescue OpenURI::HTTPError => e
|
67
|
+
case e.message
|
68
|
+
when /^400/
|
69
|
+
raise BadRequestError
|
70
|
+
when /^403/
|
71
|
+
raise AuthenticationError
|
72
|
+
when /^404/
|
73
|
+
return nil
|
74
|
+
when /^500/
|
75
|
+
raise ServerError
|
76
|
+
else
|
77
|
+
raise ConnectionError
|
78
|
+
end
|
79
|
+
raise "Error connecting to URL #{uri} #{e}"
|
80
|
+
rescue JSON::ParserError => e
|
81
|
+
raise BadResponseError, "Invalid JSON returned from API:\n#{reply}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Usatoday
|
2
|
+
module Census
|
3
|
+
class Ethnicity < Base
|
4
|
+
|
5
|
+
TEXT_FIELDS = %w(place_name state_postal)
|
6
|
+
NUMERIC_FIELDS = %w(fips gnis)
|
7
|
+
DECIMAL_FIELDS = %w(pct_hispanic pct_non_hispanic_white usat_diversity_index)
|
8
|
+
|
9
|
+
ALL_FIELDS = TEXT_FIELDS + DECIMAL_FIELDS + NUMERIC_FIELDS
|
10
|
+
|
11
|
+
attr_reader *ALL_FIELDS
|
12
|
+
|
13
|
+
def initialize(params={})
|
14
|
+
params.each_pair do |k,v|
|
15
|
+
instance_variable_set("@#{k}", v)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.init_from_api(params)
|
20
|
+
ethnicity = Ethnicity.new(
|
21
|
+
:place_name => text_field(params['Placename']),
|
22
|
+
:state_postal => text_field(params['StatePostal']),
|
23
|
+
:fips => integer_field(params['FIPS']),
|
24
|
+
:gnis => integer_field(params['GNIS']),
|
25
|
+
:pct_hispanic => float_field(params['PctHisp']),
|
26
|
+
:pct_non_hispanic_white => float_field(params['PctNonHispWhite']),
|
27
|
+
:usat_diversity_index => float_field(params['USATDiversityIndex'])
|
28
|
+
)
|
29
|
+
ethnicity
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.search(keypat, keyname=nil)
|
33
|
+
params = prepare_params(keypat, keyname)
|
34
|
+
response = invoke('ethnicity', params)
|
35
|
+
init_from_api(response)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Usatoday
|
2
|
+
module Census
|
3
|
+
##
|
4
|
+
# The generic Error class from which all other Errors are derived.
|
5
|
+
class Error < ::RuntimeError
|
6
|
+
end
|
7
|
+
|
8
|
+
##
|
9
|
+
# This error is thrown if there are problems authenticating your API key.
|
10
|
+
class AuthenticationError < Error
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# This error is thrown if the request was not parsable by the API server.
|
15
|
+
class BadRequestError < Error
|
16
|
+
end
|
17
|
+
|
18
|
+
##
|
19
|
+
# This error is thrown if the response from the API server is not parsable.
|
20
|
+
class BadResponseError < Error
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# This error is thrown if there is an error connecting to the API server.
|
25
|
+
class ServerError < Error
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# This error is thrown if there is a timeout connecting to the server (to be implemented).
|
30
|
+
class TimeoutError < Error
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# This error is thrown for general connection errors to the API server.
|
35
|
+
class ConnectionError < Error
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Usatoday
|
2
|
+
module Census
|
3
|
+
class Housing < Base
|
4
|
+
|
5
|
+
TEXT_FIELDS = %w(place_name state_postal)
|
6
|
+
NUMERIC_FIELDS = %w(fips gnis housing_units)
|
7
|
+
DECIMAL_FIELDS = %w(pct_vacant)
|
8
|
+
|
9
|
+
ALL_FIELDS = TEXT_FIELDS + DECIMAL_FIELDS + NUMERIC_FIELDS
|
10
|
+
|
11
|
+
attr_reader *ALL_FIELDS
|
12
|
+
|
13
|
+
def initialize(params={})
|
14
|
+
params.each_pair do |k,v|
|
15
|
+
instance_variable_set("@#{k}", v)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.init_from_api(params)
|
20
|
+
housing = Housing.new(
|
21
|
+
:place_name => text_field(params['Placename']),
|
22
|
+
:state_postal => text_field(params['StatePostal']),
|
23
|
+
:fips => integer_field(params['FIPS']),
|
24
|
+
:gnis => integer_field(params['GNIS']),
|
25
|
+
:pct_vacant => float_field(params['PctHisp']),
|
26
|
+
:housing_units => integer_field(params['HousingUnits'])
|
27
|
+
)
|
28
|
+
housing
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.search(keypat, keyname=nil)
|
32
|
+
params = prepare_params(keypat, keyname)
|
33
|
+
response = invoke('housing', params)
|
34
|
+
init_from_api(response)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Usatoday
|
2
|
+
module Census
|
3
|
+
class Location < Base
|
4
|
+
|
5
|
+
TEXT_FIELDS = %w(place_name place_name_full state_ap state_postal)
|
6
|
+
NUMERIC_FIELDS = %w(fips gnis population housing_units)
|
7
|
+
DECIMAL_FIELDS = %w(pct_change pct_hispanic pct_non_hispanic pct_white pct_non_hispanic_white pct_black pct_american_indian pct_asian pct_native_hawaiian pct_two_or_more pct_other usat_diversity_index population_density land_square_miles water_square_miles total_square_miles latitude longitude pct_vacant)
|
8
|
+
|
9
|
+
ALL_FIELDS = TEXT_FIELDS + DECIMAL_FIELDS + NUMERIC_FIELDS
|
10
|
+
|
11
|
+
attr_reader *ALL_FIELDS
|
12
|
+
|
13
|
+
def initialize(params={})
|
14
|
+
params.each_pair do |k,v|
|
15
|
+
instance_variable_set("@#{k}", v)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.init_from_api(params)
|
20
|
+
location = Location.new(
|
21
|
+
:place_name => text_field(params['Placename']),
|
22
|
+
:place_name_full => text_field(params['PlacenameFull']),
|
23
|
+
:state_ap => text_field(params['StateAP']),
|
24
|
+
:state_postal => text_field(params['StatePostal']),
|
25
|
+
:fips => integer_field(params['FIPS']),
|
26
|
+
:gnis => integer_field(params['GNIS']),
|
27
|
+
:population => integer_field(params['Pop']),
|
28
|
+
:housing_units => integer_field(params['HousingUnits']),
|
29
|
+
:pct_change => float_field(params['PctChange']),
|
30
|
+
:pct_hispanic => float_field(params['PctHisp']),
|
31
|
+
:pct_non_hispanic => float_field(params['PctNonHisp']),
|
32
|
+
:pct_white => float_field(params['PctWhite']),
|
33
|
+
:pct_non_hispanic_white => float_field(params['PctNonHispWhite']),
|
34
|
+
:pct_black => float_field(params['PctBlack']),
|
35
|
+
:pct_american_indian => float_field(params['PctAmInd']),
|
36
|
+
:pct_asian => float_field(params['PctAsian']),
|
37
|
+
:pct_native_hawaiian => float_field(params['PctNatHawOth']),
|
38
|
+
:pct_two_or_more => float_field(params['PctTwoOrMore']),
|
39
|
+
:pct_other => float_field(params['PctOther']),
|
40
|
+
:usat_diversity_index => float_field(params['USATDiversityIndex']),
|
41
|
+
:population_density => float_field(params['PopSqMi']),
|
42
|
+
:land_square_miles => float_field(params['LandSqMi']),
|
43
|
+
:water_square_miles => float_field(params['WaterSqMi']),
|
44
|
+
:total_square_miles => float_field(params['TotSqMi']),
|
45
|
+
:latitude => float_field(params['Lat']),
|
46
|
+
:longitude => float_field(params['Long']),
|
47
|
+
:pct_vacant => float_field(params['PctVacant'])
|
48
|
+
)
|
49
|
+
location
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.search(keypat, keyname=nil)
|
53
|
+
params = prepare_params(keypat, keyname)
|
54
|
+
response = invoke('locations', params)
|
55
|
+
init_from_api(response)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Usatoday
|
2
|
+
module Census
|
3
|
+
class Population < Base
|
4
|
+
|
5
|
+
TEXT_FIELDS = %w(place_name state_postal)
|
6
|
+
NUMERIC_FIELDS = %w(fips gnis population)
|
7
|
+
DECIMAL_FIELDS = %w(pct_change population_density)
|
8
|
+
|
9
|
+
ALL_FIELDS = TEXT_FIELDS + DECIMAL_FIELDS + NUMERIC_FIELDS
|
10
|
+
|
11
|
+
attr_reader *ALL_FIELDS
|
12
|
+
|
13
|
+
def initialize(params={})
|
14
|
+
params.each_pair do |k,v|
|
15
|
+
instance_variable_set("@#{k}", v)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.init_from_api(params)
|
20
|
+
population = Population.new(
|
21
|
+
:place_name => text_field(params['Placename']),
|
22
|
+
:state_postal => text_field(params['StatePostal']),
|
23
|
+
:fips => integer_field(params['FIPS']),
|
24
|
+
:gnis => integer_field(params['GNIS']),
|
25
|
+
:pct_change => float_field(params['PctChange']),
|
26
|
+
:population => integer_field(params['Pop']),
|
27
|
+
:population_density => float_field(params['PopSqMi'])
|
28
|
+
)
|
29
|
+
population
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.search(keypat, keyname=nil)
|
33
|
+
params = prepare_params(keypat, keyname)
|
34
|
+
response = invoke('population', params)
|
35
|
+
init_from_api(response)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
3
|
+
module Usatoday
|
4
|
+
module Census
|
5
|
+
##
|
6
|
+
# The Query class represents a single query to the Census API. Supports
|
7
|
+
# all of the named parameters to C.search as accessor methods.
|
8
|
+
#
|
9
|
+
class Query
|
10
|
+
FIELDS = [:only_facets, :except_facets, :begin_date, :end_date, :since,
|
11
|
+
:before, :fee, :has_thumbnail, :facets, :fields, :query, :offset] + Article::TEXT_FIELDS.map{|f| f.to_sym}
|
12
|
+
FIELDS.each {|f| attr_accessor f}
|
13
|
+
|
14
|
+
# Produce a hash which uniquely identifies this query
|
15
|
+
def hash
|
16
|
+
strs = FIELDS.collect {|f| "#{f}:#{send(f).inspect}"}
|
17
|
+
Digest::SHA256.hexdigest(strs.join(' '))
|
18
|
+
end
|
19
|
+
|
20
|
+
# Perform this query. Returns result of Article.search
|
21
|
+
def perform
|
22
|
+
params = {}
|
23
|
+
FIELDS.each {|f| params[f] = send(f) unless send(f).nil?}
|
24
|
+
Article.search(params)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Usatoday
|
2
|
+
module Census
|
3
|
+
class Race < Base
|
4
|
+
|
5
|
+
TEXT_FIELDS = %w(place_name state_postal)
|
6
|
+
NUMERIC_FIELDS = %w(fips gnis)
|
7
|
+
DECIMAL_FIELDS = %w(pct_white pct_black pct_american_indian pct_asian pct_native_hawaiian pct_two_or_more)
|
8
|
+
|
9
|
+
ALL_FIELDS = TEXT_FIELDS + DECIMAL_FIELDS + NUMERIC_FIELDS
|
10
|
+
|
11
|
+
attr_reader *ALL_FIELDS
|
12
|
+
|
13
|
+
def initialize(params={})
|
14
|
+
params.each_pair do |k,v|
|
15
|
+
instance_variable_set("@#{k}", v)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.init_from_api(params)
|
20
|
+
race = Race.new(
|
21
|
+
:place_name => text_field(params['Placename']),
|
22
|
+
:state_postal => text_field(params['StatePostal']),
|
23
|
+
:fips => integer_field(params['FIPS']),
|
24
|
+
:gnis => integer_field(params['GNIS']),
|
25
|
+
:pct_white => float_field(params['PctWhite']),
|
26
|
+
:pct_black => float_field(params['PctBlack']),
|
27
|
+
:pct_american_indian => float_field(params['PctAmInd']),
|
28
|
+
:pct_asian => float_field(params['PctAsian']),
|
29
|
+
:pct_native_hawaiian => float_field(params['PctNatHawOth']),
|
30
|
+
:pct_two_or_more => float_field(params['PctTwoOrMore'])
|
31
|
+
)
|
32
|
+
race
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.search(keypat, keyname=nil)
|
36
|
+
params = prepare_params(keypat, keyname)
|
37
|
+
response = invoke('race', params)
|
38
|
+
init_from_api(response)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
%w(base exceptions location ethnicity housing population race).each do |f|
|
7
|
+
require File.join(File.dirname(__FILE__), '../lib/usatoday-census', f)
|
8
|
+
end
|
9
|
+
|
10
|
+
include Usatoday::Census
|
11
|
+
|
12
|
+
# set your USAT Census API key as an environment variable to run the tests
|
13
|
+
API_KEY = ENV['USAT_CENSUS_API_KEY']
|
14
|
+
Base.api_key = API_KEY
|
15
|
+
|
16
|
+
module TestUsatoday
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class TestUsatoday::TestEthnicity < Test::Unit::TestCase
|
2
|
+
include Usatoday::Census
|
3
|
+
|
4
|
+
context "Ethnicity.new" do
|
5
|
+
setup do
|
6
|
+
sleep(1)
|
7
|
+
response = Base.invoke('ethnicity', {'keypat' => 'pa'})
|
8
|
+
@ethnicity = Ethnicity.init_from_api(response)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "return an object of the Ethnicity type" do
|
12
|
+
assert_kind_of(Ethnicity, @ethnicity)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "Ethnicity.new with placename" do
|
17
|
+
setup do
|
18
|
+
sleep(1)
|
19
|
+
@ethnicity = Ethnicity.search('Virginia', 'Placename')
|
20
|
+
end
|
21
|
+
should "return an object of the Ethnicity type" do
|
22
|
+
assert_kind_of(Ethnicity, @ethnicity)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "Ethnicity.new with FIPS" do
|
27
|
+
setup do
|
28
|
+
sleep(1)
|
29
|
+
@ethnicity = Ethnicity.search(51, 'FIPS')
|
30
|
+
end
|
31
|
+
should "return an object of the Ethnicity type" do
|
32
|
+
assert_kind_of(Ethnicity, @ethnicity)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class TestUsatoday::TestHousing < Test::Unit::TestCase
|
2
|
+
include Usatoday::Census
|
3
|
+
context "Housing.new" do
|
4
|
+
setup do
|
5
|
+
sleep(1)
|
6
|
+
response = Base.invoke('housing', {'keypat' => 'va'})
|
7
|
+
@housing = Housing.init_from_api(response)
|
8
|
+
end
|
9
|
+
|
10
|
+
should "return an object of the Housing type" do
|
11
|
+
assert_kind_of(Housing, @housing)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Housing.new with placename" do
|
16
|
+
setup do
|
17
|
+
sleep(1)
|
18
|
+
@housing = Housing.search('Virginia', 'Placename')
|
19
|
+
end
|
20
|
+
should "return an object of the Housing type" do
|
21
|
+
assert_kind_of(Housing, @housing)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "Housing.new with FIPS" do
|
26
|
+
setup do
|
27
|
+
sleep(1)
|
28
|
+
@housing = Housing.search(51, 'FIPS')
|
29
|
+
end
|
30
|
+
should "return an object of the Housing type" do
|
31
|
+
assert_kind_of(Housing, @housing)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class TestUsatoday::TestLocation < Test::Unit::TestCase
|
2
|
+
include Usatoday::Census
|
3
|
+
context "Location.new" do
|
4
|
+
setup do
|
5
|
+
sleep(1)
|
6
|
+
response = Base.invoke('locations', {'keypat' => 'va'})
|
7
|
+
@location = Location.init_from_api(response)
|
8
|
+
end
|
9
|
+
|
10
|
+
should "return an object of the Location type" do
|
11
|
+
assert_kind_of(Location, @location)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Location.new with placename" do
|
16
|
+
setup do
|
17
|
+
sleep(1)
|
18
|
+
@location = Location.search('Virginia', 'Placename')
|
19
|
+
end
|
20
|
+
should "return an object of the Location type" do
|
21
|
+
assert_kind_of(Location, @location)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "Location.new with FIPS" do
|
26
|
+
setup do
|
27
|
+
sleep(1)
|
28
|
+
@location = Location.search(51, 'FIPS')
|
29
|
+
end
|
30
|
+
should "return an object of the Location type" do
|
31
|
+
assert_kind_of(Location, @location)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class TestUsatoday::TestPopulation < Test::Unit::TestCase
|
2
|
+
include Usatoday::Census
|
3
|
+
context "Population.new" do
|
4
|
+
setup do
|
5
|
+
sleep(1)
|
6
|
+
response = Base.invoke('population', {'keypat' => 'va'})
|
7
|
+
@population = Population.init_from_api(response)
|
8
|
+
end
|
9
|
+
|
10
|
+
should "return an object of the Population type" do
|
11
|
+
assert_kind_of(Population, @population)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Population.new with placename" do
|
16
|
+
setup do
|
17
|
+
sleep(1)
|
18
|
+
@population = Population.search('Virginia', 'Placename')
|
19
|
+
end
|
20
|
+
should "return an object of the Population type" do
|
21
|
+
assert_kind_of(Population, @population)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "Population.new with FIPS" do
|
26
|
+
setup do
|
27
|
+
sleep(1)
|
28
|
+
@population = Population.search(51, 'FIPS')
|
29
|
+
end
|
30
|
+
should "return an object of the Population type" do
|
31
|
+
assert_kind_of(Population, @population)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class TestUsatoday::TestRace < Test::Unit::TestCase
|
2
|
+
include Usatoday::Census
|
3
|
+
context "Race.new" do
|
4
|
+
setup do
|
5
|
+
sleep(1)
|
6
|
+
response = Base.invoke('race', {'keypat' => 'va'})
|
7
|
+
@race = Race.init_from_api(response)
|
8
|
+
end
|
9
|
+
|
10
|
+
should "return an object of the Race type" do
|
11
|
+
assert_kind_of(Race, @race)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Race.new with placename" do
|
16
|
+
setup do
|
17
|
+
sleep(1)
|
18
|
+
@race = Race.search('Virginia', 'Placename')
|
19
|
+
end
|
20
|
+
should "return an object of the Race type" do
|
21
|
+
assert_kind_of(Race, @race)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "Race.new with FIPS" do
|
26
|
+
setup do
|
27
|
+
sleep(1)
|
28
|
+
@race = Race.search(51, 'FIPS')
|
29
|
+
end
|
30
|
+
should "return an object of the Race type" do
|
31
|
+
assert_kind_of(Race, @race)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "usatoday-census/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "usatoday-census"
|
7
|
+
s.version = Usatoday::Census::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Derek Willis"]
|
10
|
+
s.email = ["dwillis@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Wrapper for USA Today Census API}
|
13
|
+
s.description = %q{Retrieves state population and demographic information.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "usatoday-census"
|
16
|
+
|
17
|
+
s.add_dependency "shoulda"
|
18
|
+
s.add_dependency "json"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: usatoday-census
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Derek Willis
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-31 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: shoulda
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: json
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Retrieves state population and demographic information.
|
50
|
+
email:
|
51
|
+
- dwillis@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- lib/usatoday-census.rb
|
64
|
+
- lib/usatoday-census/base.rb
|
65
|
+
- lib/usatoday-census/ethnicity.rb
|
66
|
+
- lib/usatoday-census/exceptions.rb
|
67
|
+
- lib/usatoday-census/housing.rb
|
68
|
+
- lib/usatoday-census/location.rb
|
69
|
+
- lib/usatoday-census/population.rb
|
70
|
+
- lib/usatoday-census/query.rb
|
71
|
+
- lib/usatoday-census/race.rb
|
72
|
+
- lib/usatoday-census/version.rb
|
73
|
+
- test/test_helper.rb
|
74
|
+
- test/usatoday-census/test_ethnicity.rb
|
75
|
+
- test/usatoday-census/test_housing.rb
|
76
|
+
- test/usatoday-census/test_location.rb
|
77
|
+
- test/usatoday-census/test_population.rb
|
78
|
+
- test/usatoday-census/test_race.rb
|
79
|
+
- usatoday-census.gemspec
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: ""
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
requirements: []
|
108
|
+
|
109
|
+
rubyforge_project: usatoday-census
|
110
|
+
rubygems_version: 1.4.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Wrapper for USA Today Census API
|
114
|
+
test_files:
|
115
|
+
- test/test_helper.rb
|
116
|
+
- test/usatoday-census/test_ethnicity.rb
|
117
|
+
- test/usatoday-census/test_housing.rb
|
118
|
+
- test/usatoday-census/test_location.rb
|
119
|
+
- test/usatoday-census/test_population.rb
|
120
|
+
- test/usatoday-census/test_race.rb
|