class Census::DataSetVintage
Create one DataSetVintage
object per data set description in the discovery API.
Constants
- MEMBER_VARS
a map from API json variables to member variables
Public Class Methods
new(json)
click to toggle source
# File lib/rboc/data.rb, line 33 def initialize(json) MEMBER_VARS.each do |var, key| v = ('@' + var.to_s).to_sym self.instance_variable_set v, json[key] end # 'vintage' should be an int @vintage = @vintage.to_i end
Public Instance Methods
query(q=Query.new) { |q| ... }
click to toggle source
Accesses the the data api and parses the result into a Census::Data object.
# File lib/rboc/data.rb, line 73 def query(q=Query.new) yield q if block_given? # download the first 50 or fewer variables json = self.query_raw q[0...50] rs = ResultSet.new json # download remaining variables 50 at a time offset = 50 while offset <= q.variables.length json = self.api_raw year, file, q[offset...(offset+50)] json = JSON.parse json # sometimes the API returns a descriptive hash (in a single element array) if the # requested columns are invalid raise InvalidQueryError if json.first.is_a? Hash rs.merge! json offset += 50 end rs end
query_raw(q=Query.new) { |q| ... }
click to toggle source
Accesses the data api and returns the unmodified body of the HTTP response. Raises errors if the HTTP response code indicates a problem.
# File lib/rboc/data.rb, line 46 def query_raw(q=Query.new) yield q if block_given? url = self.web_service + '?' + q.to_s puts "GET #{url}" c = Curl::Easy.new url c.perform r = c.response_code if r == 200 return c.body_str elsif r == 400 raise InvalidQueryError elsif r == 204 raise NoMatchingRecordsError elsif r == 500 raise ServerSideError elsif r == 302 && (c.head.include?("missing_key") || c.head.include?("invalid_key")) raise InvalidKeyError else raise CensusApiError, "Unexpected HTTP response code: #{r}" end end