Class: Usatoday::Census::Population

Inherits:
Base
  • Object
show all
Defined in:
lib/usatoday-census/population.rb

Constant Summary collapse

TEXT_FIELDS =
%w(place_name state_postal)
NUMERIC_FIELDS =
%w(fips gnis population)
DECIMAL_FIELDS =
%w(pct_change population_density)
ALL_FIELDS =
TEXT_FIELDS + DECIMAL_FIELDS + NUMERIC_FIELDS

Constants inherited from Base

Base::API_BASE, Base::API_NAME, Base::API_SERVER

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key, api_key=, build_request_url, float_field, integer_field, invoke, prepare_params, text_field

Constructor Details

#initialize(params = {}) ⇒ Population

Returns a new instance of Population.



13
14
15
16
17
# File 'lib/usatoday-census/population.rb', line 13

def initialize(params={})
	params.each_pair do |k,v|
		instance_variable_set("@#{k}", v)
	end
end

Class Method Details

.init_from_api(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/usatoday-census/population.rb', line 19

def self.init_from_api(params)
  population = Population.new(
  :place_name => text_field(params['Placename']),
  :state_postal => text_field(params['StatePostal']),
  :fips => integer_field(params['FIPS']),
  :gnis => integer_field(params['GNIS']),
  :pct_change => float_field(params['PctChange']),
  :population => integer_field(params['Pop']),
  :population_density => float_field(params['PopSqMi'])
  )
  population
end

.search(keypat, keyname = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/usatoday-census/population.rb', line 32

def self.search(keypat, keyname=nil)
  result = []
  params = prepare_params(keypat, keyname)
  			response = invoke('population', params)
  			response.each do |r|
	result << init_from_api(r)
end
result
end