u_s_census 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module USCensus
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/u_s_census.rb CHANGED
@@ -5,30 +5,46 @@ require "u_s_census/version"
5
5
 
6
6
  module USCensus
7
7
  HOST = "http://thedataweb.rm.census.gov/data/2010/"
8
+ # FIPS geography code
9
+ STATES = {
10
+ "Alabama"=>"01","Alaska"=>"02","Arizona"=>"04","Arkansas"=>"05","California"=>"06","Colorado"=>"08","Connecticut"=>"09","Delaware"=>"10","District of Columbia"=>"11","Florida"=>"12","Georgia"=>"13","Hawaii"=>"15","Idaho"=>"16","Illinois"=>"17","Indiana"=>"18","Iowa"=>"19","Kansas"=>"20","Kentucky"=>"21","Louisiana"=>"22","Maine"=>"23","Maryland"=>"24","Massachusetts"=>"25","Michigan"=>"26","Minnesota"=>"27","Mississippi"=>"28","Missouri"=>"29","Montana"=>"30","Nebraska"=>"31" ,"Nevada"=>"32","New Hampshire"=>"33","New Jersey"=>"34","New Mexico"=>"35","New York"=>"36","North Carolina"=>"37","North Dakota"=>"38","Ohio"=>"39","Oklahoma" =>"40","Oregon"=>"41","Pennsylvania"=>"42","Rhode Island"=>"44","South Carolina"=>"45","South Dakota"=>"46","Tennessee"=>"47","Texas"=>"48","Utah"=>"49","Vermont"=>"50","Virginia"=>"51","Washington"=>"53","West Virginia"=>"54","Wisconsin"=>"55","Wyoming"=>"56","Puerto Rico"=>"72"
11
+ }
8
12
 
9
- def self.census_summary_2010(key, state = "*")
10
- url = HOST + "sf1?key=#{key}&get=P0010001,NAME&for=state:#{state}"
13
+ # Get 2010 Census SF1 data for the states by using
14
+ # <tt>census_summary_2010</tt> and pass it key and list of states (FIPS codes).
15
+ # This will return array of hashes.
16
+ def self.census_summary_2010(key, states = "*")
17
+ states = fips_codes(states)
18
+ url = HOST + "sf1?key=#{key}&get=P0010001,NAME&for=state:#{states}"
11
19
  output = open(url)
12
20
  return parse(output, key)
13
21
  end
14
22
  private
23
+ def self.fips_codes(states)
24
+ if states.is_a? Array
25
+ states.map do |s|
26
+ STATES[s] || s
27
+ end.join(',')
28
+ else
29
+ states
30
+ end
31
+ end
15
32
  def self.parse(output, key)
16
- status = output.status[1]
17
- if 400 == status
33
+ status = output.status[0]
34
+ if "400" == status
18
35
  raise "Request was not valid. Queried for unknown variables or unknown geographies: #{ouput.read}"
19
- elsif 500 == status
36
+ elsif "500" == status
20
37
  raise "Server side error while processing the request. Try again."
21
- elsif 204 == status
38
+ elsif "204" == status
22
39
  return []
23
- elsif 200 == status
24
- puts output.base_uri.to_s
40
+ elsif "200" == status
25
41
  if output.base_uri.to_s.match /invalid_key/
26
42
  raise "The key with this request it not valid: #{key}"
27
43
  else
28
44
  return array_to_hash YAML.load output.read
29
45
  end
30
46
  else
31
- raise "Unknow status code: #{status}"
47
+ raise "Unknown status code: #{status}"
32
48
  end
33
49
  end
34
50
  def self.array_to_hash(output)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u_s_census
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: