usatoday-census 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,11 +26,16 @@ or via an environment variable:
26
26
  API_KEY = ENV['USAT_CENSUS_API_KEY']
27
27
  Base.api_key = API_KEY
28
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:
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. Requests return an array of results and the default geographic level is statewide. Users can search for a state by its postal abbreviation or name. To request results for other geographic levels (National, County and City or Town), you can pass the corresponding "sumlevid" to the search method. The following all return an array consisting of a single object containing statewide results:
30
30
 
31
31
  Location.search('va') # find demographic information about Virginia
32
32
  Ethnicity.search("Virginia", "Placename") # can also search by full state name using the Placename attribute.
33
33
  Race.search("51", 'FIPS') # Or search by FIPS code, too.
34
+
35
+ These requests return an array of result objects at the county level:
36
+
37
+ Location.search('va', nil, 3) # find demographic information about Virgina counties using the state abbreviation.
38
+ Ethnicity.search("Virginia", "Placename", 3) # find ethnicity information for Virginia counties.
34
39
 
35
40
  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
41
 
@@ -43,9 +43,10 @@ module Usatoday
43
43
  value.to_f
44
44
  end
45
45
 
46
- def self.prepare_params(method, keyname=nil)
46
+ def self.prepare_params(method, keyname=nil, sumlevid=2)
47
47
  params = {"keypat" => method }
48
48
  params["keyname"] = keyname if keyname
49
+ params.merge({'sumlevid'=>sumlevid})
49
50
  params
50
51
  end
51
52
 
@@ -62,7 +63,7 @@ module Usatoday
62
63
  parsed_reply = JSON.parse reply
63
64
 
64
65
  raise BadResponseError, "Empty reply returned from API" if parsed_reply.nil?
65
- parsed_reply['response'].first
66
+ response = parsed_reply['response']
66
67
  rescue OpenURI::HTTPError => e
67
68
  case e.message
68
69
  when /^400/
@@ -26,15 +26,17 @@ module Usatoday
26
26
  :pct_non_hispanic_white => float_field(params['PctNonHispWhite']),
27
27
  :usat_diversity_index => float_field(params['USATDiversityIndex'])
28
28
  )
29
- ethnicity
30
29
  end
31
30
 
32
- def self.search(keypat, keyname=nil)
33
- params = prepare_params(keypat, keyname)
31
+ def self.search(keypat, keyname=nil, sumlevid=2)
32
+ result = []
33
+ params = prepare_params(keypat, keyname, sumlevid)
34
34
  response = invoke('ethnicity', params)
35
- init_from_api(response)
36
- end
37
-
35
+ response.each do |r|
36
+ result << init_from_api(r)
37
+ end
38
+ result
39
+ end
38
40
  end
39
41
  end
40
42
  end
@@ -28,10 +28,14 @@ module Usatoday
28
28
  housing
29
29
  end
30
30
 
31
- def self.search(keypat, keyname=nil)
32
- params = prepare_params(keypat, keyname)
31
+ def self.search(keypat, keyname=nil, sumlevid=2)
32
+ result = []
33
+ params = prepare_params(keypat, keyname, sumlevid)
33
34
  response = invoke('housing', params)
34
- init_from_api(response)
35
+ response.each do |r|
36
+ result << init_from_api(r)
37
+ end
38
+ result
35
39
  end
36
40
 
37
41
  end
@@ -46,13 +46,16 @@ module Usatoday
46
46
  :longitude => float_field(params['Long']),
47
47
  :pct_vacant => float_field(params['PctVacant'])
48
48
  )
49
- location
50
49
  end
51
50
 
52
- def self.search(keypat, keyname=nil)
53
- params = prepare_params(keypat, keyname)
51
+ def self.search(keypat, keyname=nil, sumlevid=2)
52
+ result = []
53
+ params = prepare_params(keypat, keyname, sumlevid)
54
54
  response = invoke('locations', params)
55
- init_from_api(response)
55
+ response.each do |r|
56
+ result << init_from_api(r)
57
+ end
58
+ result
56
59
  end
57
60
  end
58
61
  end
@@ -30,9 +30,13 @@ module Usatoday
30
30
  end
31
31
 
32
32
  def self.search(keypat, keyname=nil)
33
+ result = []
33
34
  params = prepare_params(keypat, keyname)
34
35
  response = invoke('population', params)
35
- init_from_api(response)
36
+ response.each do |r|
37
+ result << init_from_api(r)
38
+ end
39
+ result
36
40
  end
37
41
 
38
42
  end
@@ -33,9 +33,13 @@ module Usatoday
33
33
  end
34
34
 
35
35
  def self.search(keypat, keyname=nil)
36
+ result = []
36
37
  params = prepare_params(keypat, keyname)
37
38
  response = invoke('race', params)
38
- init_from_api(response)
39
+ response.each do |r|
40
+ result << init_from_api(r)
41
+ end
42
+ result
39
43
  end
40
44
 
41
45
  end
@@ -1,5 +1,5 @@
1
1
  module Usatoday
2
2
  module Census
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -5,7 +5,7 @@ class TestUsatoday::TestEthnicity < Test::Unit::TestCase
5
5
  setup do
6
6
  sleep(1)
7
7
  response = Base.invoke('ethnicity', {'keypat' => 'pa'})
8
- @ethnicity = Ethnicity.init_from_api(response)
8
+ @ethnicity = Ethnicity.init_from_api(response.first)
9
9
  end
10
10
 
11
11
  should "return an object of the Ethnicity type" do
@@ -13,24 +13,45 @@ class TestUsatoday::TestEthnicity < Test::Unit::TestCase
13
13
  end
14
14
  end
15
15
 
16
- context "Ethnicity.new with placename" do
16
+ context "Ethnicity.search with placename" do
17
17
  setup do
18
18
  sleep(1)
19
- @ethnicity = Ethnicity.search('Virginia', 'Placename')
19
+ @ethnicity = Ethnicity.search('Virginia', 'Placename').first
20
20
  end
21
21
  should "return an object of the Ethnicity type" do
22
22
  assert_kind_of(Ethnicity, @ethnicity)
23
23
  end
24
+ should "return the correct results" do
25
+ @ethnicity.pct_hispanic == 0.078968
26
+ end
27
+
24
28
  end
25
29
 
26
- context "Ethnicity.new with FIPS" do
30
+ context "Ethnicity.search with FIPS" do
27
31
  setup do
28
32
  sleep(1)
29
- @ethnicity = Ethnicity.search(51, 'FIPS')
33
+ @ethnicity = Ethnicity.search(51, 'FIPS').first
30
34
  end
31
35
  should "return an object of the Ethnicity type" do
32
36
  assert_kind_of(Ethnicity, @ethnicity)
33
37
  end
38
+ should "return the correct results" do
39
+ @ethnicity.pct_hispanic == 0.078968
40
+ end
34
41
  end
35
42
 
43
+ context "Ethnicity.search with abbrev and county level" do
44
+ setup do
45
+ sleep(1)
46
+ @ethnicities = Ethnicity.search('va', nil, 3)
47
+ end
48
+ should "return multiple ethnicity objects" do
49
+ @ethnicities.size > 0
50
+ end
51
+
52
+ should "return the correct place name for the first result" do
53
+ @ethnicities.first.place_name == 'Accomack'
54
+ end
55
+
56
+ end
36
57
  end
@@ -4,7 +4,7 @@ class TestUsatoday::TestHousing < Test::Unit::TestCase
4
4
  setup do
5
5
  sleep(1)
6
6
  response = Base.invoke('housing', {'keypat' => 'va'})
7
- @housing = Housing.init_from_api(response)
7
+ @housing = Housing.init_from_api(response.first)
8
8
  end
9
9
 
10
10
  should "return an object of the Housing type" do
@@ -15,21 +15,27 @@ class TestUsatoday::TestHousing < Test::Unit::TestCase
15
15
  context "Housing.new with placename" do
16
16
  setup do
17
17
  sleep(1)
18
- @housing = Housing.search('Virginia', 'Placename')
18
+ @housing = Housing.search('Virginia', 'Placename').first
19
19
  end
20
20
  should "return an object of the Housing type" do
21
21
  assert_kind_of(Housing, @housing)
22
22
  end
23
+ should "return the correct results" do
24
+ @housing.housing_units == 3364939
25
+ end
23
26
  end
24
27
 
25
28
  context "Housing.new with FIPS" do
26
29
  setup do
27
30
  sleep(1)
28
- @housing = Housing.search(51, 'FIPS')
31
+ @housing = Housing.search(51, 'FIPS').first
29
32
  end
30
33
  should "return an object of the Housing type" do
31
34
  assert_kind_of(Housing, @housing)
32
35
  end
36
+ should "return the correct results" do
37
+ @housing.housing_units == 3364939
38
+ end
33
39
  end
34
40
 
35
41
 
@@ -4,7 +4,7 @@ class TestUsatoday::TestLocation < Test::Unit::TestCase
4
4
  setup do
5
5
  sleep(1)
6
6
  response = Base.invoke('locations', {'keypat' => 'va'})
7
- @location = Location.init_from_api(response)
7
+ @location = Location.init_from_api(response.first)
8
8
  end
9
9
 
10
10
  should "return an object of the Location type" do
@@ -15,21 +15,43 @@ class TestUsatoday::TestLocation < Test::Unit::TestCase
15
15
  context "Location.new with placename" do
16
16
  setup do
17
17
  sleep(1)
18
- @location = Location.search('Virginia', 'Placename')
18
+ @location = Location.search('Virginia', 'Placename').first
19
19
  end
20
20
  should "return an object of the Location type" do
21
21
  assert_kind_of(Location, @location)
22
22
  end
23
+
24
+ should "return the correct hash of names" do
25
+ @location.state_postal == "VA"
26
+ end
23
27
  end
24
28
 
25
29
  context "Location.new with FIPS" do
26
30
  setup do
27
31
  sleep(1)
28
- @location = Location.search(51, 'FIPS')
32
+ @location = Location.search(51, 'FIPS').first
29
33
  end
30
34
  should "return an object of the Location type" do
31
35
  assert_kind_of(Location, @location)
32
36
  end
37
+
38
+ should "return the correct result" do
39
+ @location.population == 8001024
40
+ end
41
+ end
42
+
43
+ context "Location.search with abbrev and county level" do
44
+ setup do
45
+ sleep(1)
46
+ @locations = Location.search('va', nil, 3)
47
+ end
48
+ should "return multiple Location objects" do
49
+ @locations.size > 0
50
+ end
51
+
52
+ should "return the correct place" do
53
+ @locations.first.place_name == 'Accomack'
54
+ end
33
55
  end
34
56
 
35
57
 
@@ -4,7 +4,7 @@ class TestUsatoday::TestPopulation < Test::Unit::TestCase
4
4
  setup do
5
5
  sleep(1)
6
6
  response = Base.invoke('population', {'keypat' => 'va'})
7
- @population = Population.init_from_api(response)
7
+ @population = Population.init_from_api(response.first)
8
8
  end
9
9
 
10
10
  should "return an object of the Population type" do
@@ -15,21 +15,27 @@ class TestUsatoday::TestPopulation < Test::Unit::TestCase
15
15
  context "Population.new with placename" do
16
16
  setup do
17
17
  sleep(1)
18
- @population = Population.search('Virginia', 'Placename')
18
+ @population = Population.search('Virginia', 'Placename').first
19
19
  end
20
20
  should "return an object of the Population type" do
21
21
  assert_kind_of(Population, @population)
22
22
  end
23
+ should "return the correct results" do
24
+ @population.population_density == 202.6
25
+ end
23
26
  end
24
27
 
25
28
  context "Population.new with FIPS" do
26
29
  setup do
27
30
  sleep(1)
28
- @population = Population.search(51, 'FIPS')
31
+ @population = Population.search(51, 'FIPS').first
29
32
  end
30
33
  should "return an object of the Population type" do
31
34
  assert_kind_of(Population, @population)
32
35
  end
36
+ should "return the correct results" do
37
+ @population.population_density == 202.6
38
+ end
33
39
  end
34
40
 
35
41
 
@@ -4,7 +4,7 @@ class TestUsatoday::TestRace < Test::Unit::TestCase
4
4
  setup do
5
5
  sleep(1)
6
6
  response = Base.invoke('race', {'keypat' => 'va'})
7
- @race = Race.init_from_api(response)
7
+ @race = Race.init_from_api(response.first)
8
8
  end
9
9
 
10
10
  should "return an object of the Race type" do
@@ -15,21 +15,27 @@ class TestUsatoday::TestRace < Test::Unit::TestCase
15
15
  context "Race.new with placename" do
16
16
  setup do
17
17
  sleep(1)
18
- @race = Race.search('Virginia', 'Placename')
18
+ @race = Race.search('Virginia', 'Placename').first
19
19
  end
20
20
  should "return an object of the Race type" do
21
21
  assert_kind_of(Race, @race)
22
22
  end
23
+ should "return the correct results" do
24
+ @race.pct_white == 0.685769
25
+ end
23
26
  end
24
27
 
25
28
  context "Race.new with FIPS" do
26
29
  setup do
27
30
  sleep(1)
28
- @race = Race.search(51, 'FIPS')
31
+ @race = Race.search(51, 'FIPS').first
29
32
  end
30
33
  should "return an object of the Race type" do
31
34
  assert_kind_of(Race, @race)
32
35
  end
36
+ should "return the correct results" do
37
+ @race.pct_white == 0.685769
38
+ end
33
39
  end
34
40
 
35
41
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usatoday-census
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Derek Willis
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-09 00:00:00 -04:00
18
+ date: 2011-06-26 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -81,7 +81,6 @@ files:
81
81
  - lib/usatoday-census/housing.rb
82
82
  - lib/usatoday-census/location.rb
83
83
  - lib/usatoday-census/population.rb
84
- - lib/usatoday-census/query.rb
85
84
  - lib/usatoday-census/race.rb
86
85
  - lib/usatoday-census/version.rb
87
86
  - test/test_helper.rb
@@ -121,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
120
  requirements: []
122
121
 
123
122
  rubyforge_project: usatoday-census
124
- rubygems_version: 1.6.2
123
+ rubygems_version: 1.4.2
125
124
  signing_key:
126
125
  specification_version: 3
127
126
  summary: Wrapper for USA Today Census API
@@ -1,28 +0,0 @@
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