Class: CensusApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/census_api/client.rb

Overview

> 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.

Constant Summary collapse

DATASETS =
%w( sf1 acs1 acs3 acs5 )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

can add more datasets as support becomes available



16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/census_api/client.rb', line 10

def api_key
  @api_key
end

#api_vintageObject (readonly)

Returns the value of attribute api_vintage.



10
11
12
# File 'lib/census_api/client.rb', line 10

def api_vintage
  @api_vintage
end

#datasetObject

Returns the value of attribute dataset.



11
12
13
# File 'lib/census_api/client.rb', line 11

def dataset
  @dataset
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/census_api/client.rb', line 10

def options
  @options
end

Instance Method Details

#where(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# 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