census_api 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/census_api/client.rb +12 -0
- data/lib/census_api/request.rb +1 -12
- data/lib/census_api/version.rb +1 -1
- metadata +1 -1
data/lib/census_api/client.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
module CensusApi
|
2
2
|
class Client
|
3
|
+
|
4
|
+
require "yaml"
|
5
|
+
|
3
6
|
attr_reader :api_key, :options
|
4
7
|
attr_accessor :dataset
|
5
8
|
|
9
|
+
SUMMARY_LEVELS = {}
|
6
10
|
DATASETS = %w( sf1 acs5 ) # can add more datasets as support becomes available
|
7
11
|
|
8
12
|
def initialize(api_key, options = {})
|
@@ -20,11 +24,19 @@ module CensusApi
|
|
20
24
|
if options[:dataset]
|
21
25
|
@dataset = options[:dataset].downcase if DATASETS.include? options[:dataset].downcase
|
22
26
|
end
|
27
|
+
|
28
|
+
set_summary_levels
|
23
29
|
end
|
24
30
|
|
25
31
|
def find(fields, level, *within)
|
26
32
|
raise "Client has not been assigned a dataset to query. Try @client.dataset = 'SF1' or anything from #{DATASETS}" if self.dataset.nil?
|
27
33
|
Request.find(dataset, {key: @api_key, fields: fields, level: level, within: within})
|
28
34
|
end
|
35
|
+
|
36
|
+
def set_summary_levels
|
37
|
+
if SUMMARY_LEVELS.empty?
|
38
|
+
YAML.load_file(File.dirname(__FILE__).to_s + '/../yml/census_shapes.yml').each{ |k,v| SUMMARY_LEVELS[k] = v }
|
39
|
+
end
|
40
|
+
end
|
29
41
|
end
|
30
42
|
end
|
data/lib/census_api/request.rb
CHANGED
@@ -4,12 +4,9 @@ module CensusApi
|
|
4
4
|
require 'restclient'
|
5
5
|
require 'hpricot'
|
6
6
|
require 'json'
|
7
|
-
require "yaml"
|
8
7
|
|
9
8
|
attr_accessor :response
|
10
9
|
|
11
|
-
@@census_shapes
|
12
|
-
|
13
10
|
CENSUS_URL = "http://api.census.gov/data/2010"
|
14
11
|
|
15
12
|
def initialize(url, source, options)
|
@@ -52,21 +49,13 @@ module CensusApi
|
|
52
49
|
else
|
53
50
|
s = [s,"*"]
|
54
51
|
end
|
55
|
-
shp =
|
52
|
+
shp = CensusApi::Client::SUMMARY_LEVELS[s[0].upcase]
|
56
53
|
s.shift && s.unshift(shp['name'].downcase.gsub(" ", "+")) if !shp.nil?
|
57
54
|
s.unshift(s.shift.split("/")[0]) if !s[0].scan("home+land").empty? && truncate
|
58
55
|
s.join(":")
|
59
56
|
}
|
60
57
|
return result.join("+")
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.shapes
|
64
|
-
return @@census_shapes if defined?( @@census_shapes)
|
65
|
-
@@census_shapes = {}
|
66
|
-
YAML.load_file(File.dirname(__FILE__).to_s + '/../yml/census_shapes.yml').each{|k,v| @@census_shapes[k] = v}
|
67
|
-
return @@census_shapes
|
68
58
|
end
|
69
|
-
|
70
59
|
end
|
71
60
|
end
|
72
61
|
|
data/lib/census_api/version.rb
CHANGED