u_s_census 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +40 -0
- data/Rakefile +7 -0
- data/lib/u_s_census/version.rb +3 -0
- data/lib/u_s_census.rb +49 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/u_s_census/u_s_census_spec.rb +7 -0
- data/u_s_census.gemspec +19 -0
- metadata +75 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Yong Cha
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# USCensus
|
2
|
+
|
3
|
+
Access US Census data using the provided API beginning with 2010 Census Summary
|
4
|
+
File 1 and the 2010 American Community Survey.
|
5
|
+
|
6
|
+
See http://www.census.gov/developers/
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'u_s_census'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install u_s_census
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Requirement
|
25
|
+
|
26
|
+
Get key (http://www.census.gov/developers/)
|
27
|
+
|
28
|
+
### Function call
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
key = "xxxxxxxx"
|
32
|
+
USCensus::census_summary_2010(key)
|
33
|
+
```
|
34
|
+
## Development
|
35
|
+
|
36
|
+
Questions or problems? Please post them on the [issue tracker]
|
37
|
+
(https://github.com/yocha/u_s_census/issues). You can contribute changes by
|
38
|
+
forking the project and submitting a pull request.
|
39
|
+
|
40
|
+
This gem is created by Yong Cha and is under the MIT License.
|
data/Rakefile
ADDED
data/lib/u_s_census.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
require "u_s_census/version"
|
5
|
+
|
6
|
+
module USCensus
|
7
|
+
HOST = "http://thedataweb.rm.census.gov/data/2010/"
|
8
|
+
|
9
|
+
def self.census_summary_2010(key, state = "*")
|
10
|
+
url = HOST + "sf1?key=#{key}&get=P0010001,NAME&for=state:#{state}"
|
11
|
+
output = open(url)
|
12
|
+
return parse(output, key)
|
13
|
+
end
|
14
|
+
private
|
15
|
+
def self.parse(output, key)
|
16
|
+
status = output.status[1]
|
17
|
+
if 400 == status
|
18
|
+
raise "Request was not valid. Queried for unknown variables or unknown geographies: #{ouput.read}"
|
19
|
+
elsif 500 == status
|
20
|
+
raise "Server side error while processing the request. Try again."
|
21
|
+
elsif 204 == status
|
22
|
+
return []
|
23
|
+
elsif 200 == status
|
24
|
+
puts output.base_uri.to_s
|
25
|
+
if output.base_uri.to_s.match /invalid_key/
|
26
|
+
raise "The key with this request it not valid: #{key}"
|
27
|
+
else
|
28
|
+
return array_to_hash YAML.load output.read
|
29
|
+
end
|
30
|
+
else
|
31
|
+
raise "Unknow status code: #{status}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
def self.array_to_hash(output)
|
35
|
+
length = output.size
|
36
|
+
return [] if length <= 1
|
37
|
+
keys = output.first
|
38
|
+
raise "No keys found." if keys.size == 0
|
39
|
+
arr = []
|
40
|
+
output[1..-1].map do |o|
|
41
|
+
h = Hash.new
|
42
|
+
keys.each_with_index do |v, i|
|
43
|
+
h[v] = o[i]
|
44
|
+
end
|
45
|
+
arr << h
|
46
|
+
end
|
47
|
+
return arr
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'u_s_census'
|
data/u_s_census.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/u_s_census/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Yong Cha"]
|
6
|
+
gem.email = ["yocha@yahoo.com"]
|
7
|
+
gem.description = %q{Access US Census data}
|
8
|
+
gem.summary = %q{Access US Census data}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "u_s_census"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = USCensus::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: u_s_census
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Yong Cha
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Access US Census data
|
31
|
+
email:
|
32
|
+
- yocha@yahoo.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- CHANGELOG.md
|
40
|
+
- Gemfile
|
41
|
+
- LICENSE
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- lib/u_s_census.rb
|
45
|
+
- lib/u_s_census/version.rb
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
- spec/u_s_census/u_s_census_spec.rb
|
48
|
+
- u_s_census.gemspec
|
49
|
+
homepage: ''
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.21
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Access US Census data
|
73
|
+
test_files:
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/u_s_census/u_s_census_spec.rb
|