class CensusApi::Client
> CensusApi::Client
¶ ↑
client#initialize method takes an api_key
and options hash, which includes dataset and vintage. client#where method accepts an options hash, including fields, level and within. Within is optional.
Constants
- DATASETS
Attributes
api_key[R]
api_vintage[R]
dataset[RW]
options[R]
Public Class Methods
new(api_key, options = {})
click to toggle source
can add more datasets as support becomes available
# File lib/census_api/client.rb, line 16 def initialize(api_key, options = {}) fail ArgumentError, 'You must set an api_key.' unless api_key validate_api_key(api_key) @api_key = api_key @api_vintage = options[:vintage] || 2010 if options[:dataset] && DATASETS.include?(options[:dataset].downcase) @dataset = options[:dataset].downcase end end
Public Instance Methods
where(options={})
click to toggle source
# File lib/census_api/client.rb, line 26 def where(options={}) options.merge!(key: @api_key, vintage: @api_vintage) fail "Client requires a dataset (#{DATASETS})." if @dataset.nil? [:fields, :level].each do |f| fail ArgumentError, "#{f} is a requied parameter" if options[f].nil? end options[:within] = [options[:within]] unless options[:within].nil? Request.find(dataset, options) end
Protected Instance Methods
validate_api_key(api_key)
click to toggle source
# File lib/census_api/client.rb, line 38 def validate_api_key(api_key) path = "/data/2010/sf1?key=#{api_key}&get=P0010001&for=state:01" response = $census_connection.get path if response.to_s.include? 'Invalid Key' fail "'#{api_key}' is not a valid API key. Check your key for errors, or request a new one at census.gov." end end