Skip to content

Commit

Permalink
Merge branch 'develop' of barrel.mitre.org:hdata-patient-server/hdata…
Browse files Browse the repository at this point in the history
…-patient-server into develop
  • Loading branch information
rdingwell committed Feb 7, 2012
2 parents bba8f19 + b373bc6 commit 952ec4a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 34 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: http://github.com/projectcypress/health-data-standards.git
revision: b9c81c7707a17819bc92ce4f92c05bddbdba32a5
revision: e431176e7d3bf12aaa77953b14a4b8b1de054b4f
branch: develop
specs:
health-data-standards (0.7.1)
Expand Down Expand Up @@ -75,7 +75,7 @@ GEM
configatron
hashie
curb (0.7.18)
daemons (1.1.7)
daemons (1.1.8)
devise (2.0.0)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
Expand Down Expand Up @@ -195,7 +195,7 @@ GEM
tilt (~> 1.3)
sax-machine (0.0.20)
nokogiri (> 0.0.0)
slop (2.4.3)
slop (2.4.4)
sprockets (2.1.2)
hike (~> 1.2)
rack (~> 1.0)
Expand Down
31 changes: 23 additions & 8 deletions app/controllers/entries_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class EntriesController < ApplicationController
before_filter :get_section_name
before_filter :find_entry, except: "index"

respond_to :atom, :xml
before_filter :set_up_section
before_filter :find_entry, only: "show"

def index
@entries = @record.send(@section_name)
Expand All @@ -12,18 +10,35 @@ def index
end

def show
render :json => @entry.attributes
respond_to do |wants|
wants.json {render :json => @entry.attributes}
wants.xml do
exporter = @extension.exporters['application/xml']
render :xml => exporter.export(@entry)
end
end
end


private

def get_section_name
def set_up_section
@section_name = params[:section]
sr = SectionRegistry.instance
@extension = sr.extension_from_path(params[:section])
unless @extension
render text: 'Section Not Found', status: 404
end
end

def find_entry
@entry = @record.send(@section_name).find(params[:id])
if BSON::ObjectId.legal?(params[:id])
@entry = @record.send(@section_name).find(:first, conditions: {id: params[:id]})
unless @entry
render text: 'Entry Not Found', status: 404
end
else
render text: 'Not a valid identifier for a section document', status: 400
end
end

end
21 changes: 21 additions & 0 deletions config/initializers/hdata_sections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'section_registry'

##############################################################
# Register all the sections we will use in the web application
##############################################################
sr = SectionRegistry.instance

sr.add_section('allergies', 'http://projecthdata.org/hdata/schemas/2009/06/allergy', 'Allergies')
sr.add_section('care_goals', 'http://projecthdata.org/hdata/schemas/2009/06/care_goal', 'Care Goals')
sr.add_section('conditions', 'http://projecthdata.org/hdata/schemas/2009/06/condition', 'Conditions')
sr.add_section('encounters', 'http://projecthdata.org/hdata/schemas/2009/06/encounter', 'Encounters')
sr.add_section('immunizations', 'http://projecthdata.org/hdata/schemas/2009/06/immunization', 'Immunizations')
sr.add_section('medical_equipment', 'http://projecthdata.org/hdata/schemas/2009/06/medical_equipment', 'Medical Equipment')
sr.add_section('medications', 'http://projecthdata.org/hdata/schemas/2009/06/medication', 'Medications')
sr.add_section('procedures', 'http://projecthdata.org/hdata/schemas/2009/06/procedure', 'Procedures')
sr.add_section('results', 'http://projecthdata.org/hdata/schemas/2009/06/result', 'Lab Results') do |importers, exporters|
importers['application/xml'] = HealthDataStandards::Import::GreenCda::ResultImporter.instance
exporters['application/xml'] = HealthDataStandards::Export::GreenCda::ExportGenerator.create_exporter_for(:result)
end
sr.add_section('social_history', 'http://projecthdata.org/hdata/schemas/2009/06/social_history', 'Social History')
sr.add_section('vital_signs', 'http://projecthdata.org/hdata/schemas/2009/06/result', 'Vital Signs')
26 changes: 4 additions & 22 deletions lib/section_registry.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'ostruct'

# A registry where hData extensionIds can be mapped to url paths and vice versa
#
# Note - This class gets configured in config/initializers/hdata_sections.rb
#
class SectionRegistry
include Singleton

Expand Down Expand Up @@ -30,25 +33,4 @@ def add_section(path, extension_id, name)
def extension_from_path(path)
@extensions.find {|e| e.path == path}
end
end


##############################################################
# Register all the sections we will use in the web application
##############################################################
sr = SectionRegistry.instance

sr.add_section('allergies', 'http://projecthdata.org/hdata/schemas/2009/06/allergy', 'Allergies')
sr.add_section('care_goals', 'http://projecthdata.org/hdata/schemas/2009/06/care_goal', 'Care Goals')
sr.add_section('conditions', 'http://projecthdata.org/hdata/schemas/2009/06/condition', 'Conditions')
sr.add_section('encounters', 'http://projecthdata.org/hdata/schemas/2009/06/encounter', 'Encounters')
sr.add_section('immunizations', 'http://projecthdata.org/hdata/schemas/2009/06/immunization', 'Immunizations')
sr.add_section('medical_equipment', 'http://projecthdata.org/hdata/schemas/2009/06/medical_equipment', 'Medical Equipment')
sr.add_section('medications', 'http://projecthdata.org/hdata/schemas/2009/06/medication', 'Medications')
sr.add_section('procedures', 'http://projecthdata.org/hdata/schemas/2009/06/procedure', 'Procedures')
sr.add_section('results', 'http://projecthdata.org/hdata/schemas/2009/06/result', 'Lab Results') do |importers, exporters|
importers['application/xml'] = HealthDataStandards::Import::GreenCda::ResultImporter.instance
exporters['application/xml'] = HealthDataStandards::Export::GreenCda::ExportGenerator.create_exporter_for(:result)
end
sr.add_section('social_history', 'http://projecthdata.org/hdata/schemas/2009/06/social_history', 'Social History')
sr.add_section('vital_signs', 'http://projecthdata.org/hdata/schemas/2009/06/result', 'Vital Signs')
end
12 changes: 12 additions & 0 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
birthdate -790874989
gender 'M'
sequence(:medical_record_number) {|n| n}

trait :with_lab_results do
results {|r| [r.association(:lab_result)]}
end
end

factory :lab_result do
description 'LDL Cholesterol'
time 1285387200
codes({'CPT' => ['83721'], 'LOINC' => ['2089-1']})
value({'scalar' => 127, 'units' => 'mg/dL'})
reference_range '70 mg/dL - 160 mg/dL'
end


Expand Down
48 changes: 48 additions & 0 deletions test/functional/entries_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'test_helper'

class EntriesControllerTest < ActionController::TestCase
setup do
@record = FactoryGirl.create(:record, :with_lab_results)
end

test "get a result as xml" do
request.env['HTTP_ACCEPT'] = Mime::XML
get :show, {record_id: @record.medical_record_number, section: 'results', id: @record.results.first.id}
doc = Nokogiri::XML::Document.parse(response.body)
assert_response :success
assert_equal "result", doc.children.first.name
end

test "get a result as JSON" do
request.env['HTTP_ACCEPT'] = Mime::JSON
get :show, {record_id: @record.medical_record_number, section: 'results', id: @record.results.first.id}
doc = JSON.parse(response.body)
assert_response :success
assert_equal "LDL Cholesterol", doc['description']
end

test "get a section that doesn't exist" do
request.env['HTTP_ACCEPT'] = Mime::XML
get :show, {record_id: @record.medical_record_number, section: 'bacon', id: @record.results.first.id}
assert_response :missing
end

test "get section Atom feed" do
request.env['HTTP_ACCEPT'] = 'application/atom+xml'
get :index, {record_id: @record.medical_record_number, section: 'results'}
assert_response :success
rss = Feedzirra::Feed.parse(@response.body)
assert_not_nil(rss.entries)
assert_equal(1, rss.entries.size)
assert rss.entries[0].links[0].include? "/records/#{@record.medical_record_number}/results/#{@record.results.first.id}"
end

test "get a document that doesn't exist" do
request.env['HTTP_ACCEPT'] = Mime::XML
get :show, {record_id: @record.medical_record_number, section: 'results', id: 'bacon'}
assert_response 400

get :show, {record_id: @record.medical_record_number, section: 'results', id: ('0' * 24)}
assert_response :missing
end
end
1 change: 0 additions & 1 deletion test/unit/section_registry_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'test_helper'
require 'section_registry'

class SectionRegistryTest < ActiveSupport::TestCase
test 'finding an extension by path' do
Expand Down

0 comments on commit 952ec4a

Please sign in to comment.