Class: Census::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/rboc/census.rb

Overview

A class representing a query to the Census API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



13
14
15
16
# File 'lib/rboc/census.rb', line 13

def initialize
  @variables = []
  @geo = Geography.new
end

Instance Attribute Details

#geoObject

Returns the value of attribute geo.



11
12
13
# File 'lib/rboc/census.rb', line 11

def geo
  @geo
end

#variablesObject

Returns the value of attribute variables.



11
12
13
# File 'lib/rboc/census.rb', line 11

def variables
  @variables
end

Instance Method Details

#[](rng) ⇒ Object

Constructs a new Query object with a subset of variables. Creates a shallow copy of this Query’s geography and api key.



54
55
56
57
58
59
60
61
# File 'lib/rboc/census.rb', line 54

def [](rng)
  variables = @variables[rng]
  q = Query.new
  q.variables = variables
  q.geo = @geo
  q.api_key = @api_key
  q
end

#api_keyObject

Returns the API key to be used for this query. If the key hasn’t been set explicitly, this method attempts to load a key previously installed by Census#install_key!.



25
26
27
# File 'lib/rboc/census.rb', line 25

def api_key
  @api_key ||= Census.installed_key
end

#api_key=(key) ⇒ Object



18
19
20
# File 'lib/rboc/census.rb', line 18

def api_key=(key)
  @api_key = key
end

#for(level) ⇒ Object



36
37
38
39
# File 'lib/rboc/census.rb', line 36

def for(level)
  @geo.summary_level = level
  self
end

#get(*vars) ⇒ Object

these chainable methods mirror the field names in the HTTP get string



31
32
33
34
# File 'lib/rboc/census.rb', line 31

def get(*vars)
  @variables = vars
  self
end

#in(container) ⇒ Object



41
42
43
44
# File 'lib/rboc/census.rb', line 41

def in(container)
  @geo.contained_in = container
  self
end

#key(key) ⇒ Object



46
47
48
49
# File 'lib/rboc/census.rb', line 46

def key(key)
  @api_key = key
  self
end

#to_hashObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rboc/census.rb', line 63

def to_hash
  h = {}
  h['key'] = self.api_key
  h.merge! geo.to_hash

  v = @variables
  v = v.join(',') if v.is_a? Array
  h['get'] = v

  h
end

#to_sObject

Returns the query portion of the API GET string.



77
78
79
# File 'lib/rboc/census.rb', line 77

def to_s
  URI.encode_www_form self.to_hash
end