Class: CensusApi::Client
- Inherits:
-
Object
- Object
- CensusApi::Client
- Defined in:
- lib/census_api/client.rb
Constant Summary collapse
- DATASETS =
can add more datasets as support becomes available
%w( sf1 acs1 acs3 acs5 )
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_vintage ⇒ Object
readonly
Returns the value of attribute api_vintage.
-
#dataset ⇒ Object
Returns the value of attribute dataset.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #find(fields, level, *within) ⇒ Object
-
#initialize(api_key, options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(api_key, options = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/census_api/client.rb', line 10 def initialize(api_key, = {}) raise ArgumentError, "You must set an api_key." unless api_key # Use RestClient directly to determine the validity of the API Key path = "http://api.census.gov/data/2010/sf1?key=#{api_key}&get=P0010001&for=state:01" response = RestClient.get(path) if response.body.include? "Invalid Key" raise "'#{api_key}' is not a valid API key. Check your key for errors, or request a new one at census.gov." end @api_key = api_key @api_vintage = [:vintage] || 2010 if [:dataset] @dataset = [:dataset].downcase if DATASETS.include? [:dataset].downcase end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
5 6 7 |
# File 'lib/census_api/client.rb', line 5 def api_key @api_key end |
#api_vintage ⇒ Object (readonly)
Returns the value of attribute api_vintage.
5 6 7 |
# File 'lib/census_api/client.rb', line 5 def api_vintage @api_vintage end |
#dataset ⇒ Object
Returns the value of attribute dataset.
6 7 8 |
# File 'lib/census_api/client.rb', line 6 def dataset @dataset end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/census_api/client.rb', line 5 def end |
Instance Method Details
#find(fields, level, *within) ⇒ Object
28 29 30 31 |
# File 'lib/census_api/client.rb', line 28 def find(fields, level, *within) raise "Client has not been assigned a dataset to query. Try @client.dataset = 'SF1' or anything from #{DATASETS}" if self.dataset.nil? Request.find(dataset, {key: @api_key, vintage: @api_vintage, fields: fields, level: level, within: within}) end |