Class: Census::Query
- Inherits:
-
Object
- Object
- Census::Query
- Defined in:
- lib/rboc/census.rb
Overview
A class representing a query to the Census API.
Instance Attribute Summary collapse
-
#geo ⇒ Object
Returns the value of attribute geo.
-
#variables ⇒ Object
Returns the value of attribute variables.
Instance Method Summary collapse
-
#[](rng) ⇒ Object
Constructs a new Query object with a subset of variables.
-
#api_key ⇒ Object
Returns the API key to be used for this query.
- #api_key=(key) ⇒ Object
- #for(level) ⇒ Object
-
#get(*vars) ⇒ Object
these chainable methods mirror the field names in the HTTP get string.
- #in(container) ⇒ Object
-
#initialize ⇒ Query
constructor
A new instance of Query.
- #key(key) ⇒ Object
- #to_hash ⇒ Object
-
#to_s ⇒ Object
Returns the query portion of the API GET string.
Constructor Details
Instance Attribute Details
#geo ⇒ Object
Returns the value of attribute geo.
11 12 13 |
# File 'lib/rboc/census.rb', line 11 def geo @geo end |
#variables ⇒ Object
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_key ⇒ Object
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_hash ⇒ Object
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_s ⇒ Object
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 |