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. client#find takes positional arguments and is now deprecated.

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

#find(fields, level, *within) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/census_api/client.rb', line 26

def find(fields, level, *within)
  warn '[DEPRECATION] `find` is deprecated. Please use `where` instead.'
  fail "Client requires a dataset (#{DATASETS})." if @dataset.nil?
  options = {
    key: @api_key,
    vintage: @api_vintage,
    fields: fields,
    level: level,
    within: within
  }
  Request.find(dataset, options)
end

#where(options = { key: @api_key, vintage: @api_vintage }) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/census_api/client.rb', line 39

def where(options = { 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
  Request.find(dataset, options)
end