u_s_census 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in u_s_census.gemspec
4
+ gemspec
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
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ module USCensus
2
+ VERSION = "0.0.1"
3
+ end
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
@@ -0,0 +1 @@
1
+ require 'u_s_census'
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe USCensus do
4
+ it "raises an exception" do
5
+ lambda {USCensus::census_summary_2010('xxxx')}.should raise_error
6
+ end
7
+ end
@@ -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