Last active
August 29, 2015 14:23
-
-
Save michaelachrisco/dff796c04701c96c1154 to your computer and use it in GitHub Desktop.
Example SOPA gem models and rake task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rake tasks | |
namespace :newborn_screened_disorders do | |
desc 'Import Newborn Diseases records from cdph.data.ca.gov' | |
task :race_ethnicity => :environment do | |
# https://cdph.data.ca.gov/Diseases-and-Conditions/Newborn-Screened-Disorders-by-Race-Ethnicity-2009-/ktfd-n9nb | |
client = SODA::Client.new( | |
{:domain => "cdph.data.ca.gov", | |
:app_token => Figaro.env.app_id }) | |
response = client.get("ktfd-n9nb", {:disorder_type => 'Primary Congenital Hypothyroidism'}) | |
end | |
desc 'Import Newborn Diseases by California Regions from cdph.data.ca.gov' | |
task :california_regions => :environment do | |
# https://cdph.data.ca.gov/Diseases-and-Conditions/Newborn-Screened-Disorders-by-California-Regions-2/t6pv-avc4 | |
client = SODA::Client.new( | |
{:domain => "cdph.data.ca.gov", | |
:app_token => Figaro.env.app_id }) | |
response = client.get("t6pv-avc4", {:disease_type => 'Primary Congenital Hypothyroidism (CH)'}) | |
# TODO: ADD IN MORE NAMES!! | |
disease = Disease.find_or_create_by(:name => 'Primary Congenital Hypothyroidism') | |
puts response.inspect | |
response.each do |region| | |
Prevalance.create do |prevalance| | |
prevalance.disease = disease | |
prevalance.disorder_percent = region.disorder_type_by_region | |
prevalance.case_count = region.case_count | |
prevalance.screened = region.screened_newborns | |
prevalance.california_region = region.california_region | |
end | |
end | |
puts Prevalance.all.inspect | |
end | |
end | |
# app/model/* | |
class Prevalance < ActiveRecord::Base | |
belongs_to :disease | |
def to_s | |
"#{id} - #{name}" | |
end | |
def disease | |
super || NullableObject.new | |
end | |
end | |
class Disease < ActiveRecord::Base | |
has_many :screening_locations | |
has_many :prevalances | |
def to_s | |
"#{id} - #{name}" | |
end | |
def prevalances | |
super || [NullableObject.new] | |
end | |
def screening_locations | |
super || [NullableObject.new] | |
end | |
end | |
class ScreeningLocation < ActiveRecord::Base | |
belongs_to :disease | |
# validates_formatting_of :phone, using: :us_phone | |
# validates_formatting_of :zipcode, using: :us_zip | |
geocoded_by :full_address | |
after_validation :geocode, :if => \ | |
->(obj) { obj.full_address.present? && obj.changed? } | |
def full_address | |
[street, city, state, 'US'].compact.join(', ') | |
end | |
def disease | |
super || NullableObject.new | |
end | |
end | |
# schema.rb | |
ActiveRecord::Schema.define(version: 20150606173947) do | |
create_table "diseases", force: true do |t| | |
t.text "description" | |
t.string "name" | |
t.string "link" | |
t.text "early_signs" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
end | |
create_table "prevalances", force: true do |t| | |
t.string "case_count" | |
t.integer "disease_id" | |
t.float "screened" | |
t.string "california_region" | |
t.decimal "disorder_percent" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
end | |
add_index "prevalances", ["disease_id"], name: "index_prevalances_on_disease_id" | |
create_table "screening_locations", force: true do |t| | |
t.integer "disease_id" | |
t.string "street" | |
t.string "name" | |
t.string "phone" | |
t.string "zipcode" | |
t.decimal "longitude" | |
t.decimal "latitude" | |
t.string "state" | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
t.string "city" | |
t.string "homepage" | |
end | |
add_index "screening_locations", ["disease_id"], name: "index_screening_locations_on_disease_id" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment