class Census::Query

A class representing a query to the Census API.

Attributes

geo[RW]
variables[RW]

Public Class Methods

new() click to toggle source
# File lib/rboc/census.rb, line 13
def initialize
  @variables = []
  @geo = Geography.new
end

Public Instance Methods

[](rng) click to toggle source

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

# 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_key() click to toggle source

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

# File lib/rboc/census.rb, line 25
def api_key
  @api_key ||= Census.installed_key
end
api_key=(key) click to toggle source
# File lib/rboc/census.rb, line 18
def api_key=(key)
  @api_key = key
end
for(level) click to toggle source
# File lib/rboc/census.rb, line 36
def for(level)
  @geo.summary_level = level
  self
end
get(*vars) click to toggle source

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

# File lib/rboc/census.rb, line 31
def get(*vars)
  @variables = vars
  self
end
in(container) click to toggle source
# File lib/rboc/census.rb, line 41
def in(container)
  @geo.contained_in = container
  self
end
key(key) click to toggle source
# File lib/rboc/census.rb, line 46
def key(key)
  @api_key = key
  self
end
to_hash() click to toggle source
# 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_s() click to toggle source

Returns the query portion of the API GET string.

# File lib/rboc/census.rb, line 77
def to_s
  URI.encode_www_form self.to_hash
end