tyrauber / census_api

A Ruby Gem for querying the US Census Bureau API
MIT License
30 stars 9 forks source link

[Enhancement] Friendly field names #5

Open beechnut opened 11 years ago

beechnut commented 11 years ago

At present, you pass in fields that are just the codes listed in the Census reference handbook. This provides reliability, but it's not very Rubyish.

I think it's possible to let people specify field components by text, which are looked up in a YAML file / Hash to construct the field code.

Example:

field = Field.new(:total_population, :total, :white, :total)
field.code
# => 'P0010001'

@client.find( field, county: 'Suffolk', state: 'MA' )
# => [{"P0010001"=>"722023", "name"=>"Suffolk County", "state"=>"25", "county"=>"025"}] 

# or specify the table name directly, which returns the first field for that table
@client.find( :total_population, county: 'Suffolk', state: 'MA' )
# => [{"P0010001"=>"722023", "name"=>"Suffolk County", "state"=>"25", "county"=>"025"}] 

field_hh = Field.new(:household_type, :other_family)
field_hh.code
#=> 'P0180004'

Field Construction

Field codes appear to have a highly structured construction, but the principles behind it are not clearly laid out anywhere in the census docs. From cursory research, they look like:

P001A0001

Or, as an object (looks like YAML a little...):

P: Population
  001: Total (Population)
    A: White
      0001: Total