Permalink
Browse files

Merge branch 'api-refactor'

  • Loading branch information...
2 parents b33223e + 1960e8c commit df243445f5d0189d8b005b318e7907e809140040 @nilbus committed Jul 10, 2015
View
@@ -1,199 +1,24 @@
class API
BASE_URL_CONST = "https://taurus.i3l.gatech.edu:8443/HealthPort/fhir/"
- RPATH_PAT_CONST = "Patient/"
- RPATH_OBS_CONST = "Observation/"
- RPATH_CON_CONST = "Condition/"
- RPATH_MED_CONST = "MedicationPrescription/"
RPATH_PARAMS_CONST = "?_format=json"
RPATH_FOR_PATIENT_PREFIX_CONST = "&subject=Patient/"
RPATH_COUNT_100_CONST = "&_count=100"
- ### Patients
-
- def self.get_patient_object_from_data(patient_data)
- this_pid = dig_with_specified_array_location(patient_data, 0, "identifier", "array", "value")
- this_name_first = dig_with_specified_array_location(patient_data, 0, "name", "array", "given", "array")
- this_name_last = dig_with_specified_array_location(patient_data, 0, "name", "array", "family", "array")
- Patient.new(this_pid, this_name_first, this_name_last)
- end
-
- def self.all_patients
- results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_PAT_CONST}#{RPATH_PARAMS_CONST}#{RPATH_COUNT_100_CONST}")
- results_hash = JSON.parse(results.body)
- patients = results_hash["entry"].map do |patient_data|
- get_patient_object_from_data(patient_data["content"])
- end
- end
-
- def self.update_patient(patient)
- results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_PAT_CONST}#{patient.pid}#{RPATH_PARAMS_CONST}")
- results_hash = JSON.parse(results.body)
- patient.fname = results_hash["name"][0]["given"].first
- patient.lname = results_hash["name"][0]["family"].first
- patient
- end
-
- def self.get_patient_from_pid(pid)
- results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_PAT_CONST}#{pid}#{RPATH_PARAMS_CONST}")
- results_hash = JSON.parse(results.body)
- get_patient_object_from_data(results_hash)
- end
-
- ### Observations
-
- def self.get_observation_object_from_data(observation_data)
- # id, value, units, comment, pub_date, status, reliability, code_system, code, display_div
- this_id = dig_with_specified_array_location(observation_data, 0, "id")
- this_value = dig_with_specified_array_location(observation_data, 0, "content", "valueQuantity", "value")
- this_units = dig_with_specified_array_location(observation_data, 0, "content", "valueQuantity", "units")
- this_comment = dig_with_specified_array_location(observation_data, 0, "content", "comments")
- this_date = dig_with_specified_array_location(observation_data, 0, "con tent", "appliesDateTime")
- this_status = dig_with_specified_array_location(observation_data, 0, "content", "status")
- this_reliability = dig_with_specified_array_location(observation_data, 0, "content", "reliability")
- this_code_system = dig_with_specified_array_location(observation_data, 0, "content", "name", "coding", "array", "system")
- this_code = dig_with_specified_array_location(observation_data, 0, "content", "name", "coding", "array", "code")
- this_code_text = dig_with_specified_array_location(observation_data, 0, "content", "name", "coding", "array", "display")
- this_display = dig_with_specified_array_location(observation_data, 0, "content", "text", "div")
- Observation.new(this_display, this_id, this_value, this_units, this_comment, this_date, this_status, this_reliability, this_code_system, this_code, this_code_text)
- end
-
- def self.update_patient_observations(patient)
- results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_OBS_CONST}#{RPATH_PARAMS_CONST}#{RPATH_FOR_PATIENT_PREFIX_CONST}#{patient.pid}#{RPATH_COUNT_100_CONST}")
- results_hash = JSON.parse(results.body)
- observation_array = results_hash["entry"].map do |observation_data|
- get_observation_object_from_data(observation_data)
- end
-
- data_collection_done = false
- while data_collection_done == false do
- results_next_tag = dig_with_specified_array_location(results_hash, 1, "link", "array", "rel")
- results_next_url = dig_with_specified_array_location(results_hash, 1, "link", "array", "href")
- unless results_next_tag == "next"
- results_next_tag = dig_with_specified_array_location(results_hash, 2, "link", "array", "rel")
- results_next_url = dig_with_specified_array_location(results_hash, 2, "link", "array", "href")
- end
- if results_next_tag == "next"
- results = nil
- more_observations = nil
- results = HTTParty.get(results_next_url)
- results_hash = JSON.parse(results.body)
- more_observations = results_hash["entry"].map do |observation_data|
- get_observation_object_from_data(observation_data)
- end
- observation_array += more_observations
- else
- data_collection_done = true
- end
- end
- patient.observations = observation_array
+ def all_patients
+ Patient.new.all
end
- ### Conditions
-
- def self.get_condition_object_from_data(condition_data)
- #id, value, onset_date, status, code_system, code
- this_id = dig_with_specified_array_location(condition_data, 0, "id")
- this_value = dig_with_specified_array_location(condition_data, 0, "content", "code", "coding", "array", "display")
- this_onset_date = dig_with_specified_array_location(condition_data, 0, "content", "onsetDate")
- this_status = dig_with_specified_array_location(condition_data, 0, "content", "status")
- this_code_system = dig_with_specified_array_location(condition_data, 0, "content", "code", "coding", "array", "system")
- this_code = dig_with_specified_array_location(condition_data, 0, "content", "code", "coding", "array", "code")
- Condition.new(this_id, this_value, this_onset_date, this_status, this_code_system, this_code)
+ def update_patient(patient)
+ Patient.new.update(patient)
end
- def self.update_patient_conditions(patient)
- results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_CON_CONST}#{RPATH_PARAMS_CONST}#{RPATH_FOR_PATIENT_PREFIX_CONST}#{patient.pid}#{RPATH_COUNT_100_CONST}")
- results_hash = JSON.parse(results.body)
- condition_array = results_hash["entry"].map do |condition_data|
- get_condition_object_from_data(condition_data)
- end
-
- data_collection_done = false
- while data_collection_done == false do
- results_next_tag = dig_with_specified_array_location(results_hash, 1, "link", "array", "rel")
- results_next_url = dig_with_specified_array_location(results_hash, 1, "link", "array", "href")
- unless results_next_tag == "next"
- results_next_tag = dig_with_specified_array_location(results_hash, 2, "link", "array", "rel")
- results_next_url = dig_with_specified_array_location(results_hash, 2, "link", "array", "href")
- end
- if results_next_tag == "next"
- results = nil
- more_conditions = nil
- results = HTTParty.get(results_next_url)
- results_hash = JSON.parse(results.body)
- more_conditions = results_hash["entry"].map do |condition_data|
- get_condition_object_from_data(condition_data)
- end
- condition_array += more_conditions
- else
- data_collection_done = true
- end
- end
- patient.conditions = condition_array
+ def update_patient_assessments(type, patient)
+ patient.send "#{type}=", type_class.new(patient).fetch_assessments
end
- ### Medications
+ private
- def self.get_medication_object_from_data(medication_data)
- #id, value, status, prescriber, written_date, dosage_value, dosage_units, dosage_text, dispense_quantity, dispense_repeats, coding_system, code
- this_id = dig_with_specified_array_location(medication_data, 0, "id")
- this_value = dig_with_specified_array_location(medication_data, 0, "content", "medication", "display")
- this_status = dig_with_specified_array_location(medication_data, 0, "content", "status")
- this_prescriber = dig_with_specified_array_location(medication_data, 0, "content", "prescriber", "display")
- this_written_date = dig_with_specified_array_location(medication_data, 0, "content", "dateWritten")
- this_dosage_value = dig_with_specified_array_location(medication_data, 0, "content", "dosageInstruction", "array", "doseQuantity", "value")
- this_dosage_units = dig_with_specified_array_location(medication_data, 0, "content", "dosageInstruction", "array", "doseQuantity", "units")
- this_dosage_text = dig_with_specified_array_location(medication_data, 0, "content", "dosageInstruction", "array", "text")
- this_dispense_quantity = dig_with_specified_array_location(medication_data, 0, "content", "dispense", "quantity", "value")
- this_dispense_repeats = dig_with_specified_array_location(medication_data, 0, "content", "dispense", "numberOfRepeatsAllowed")
- this_coding_system = dig_with_specified_array_location(medication_data, 0, "content", "contained", "array", "code", "coding", "array", "system")
- this_code = dig_with_specified_array_location(medication_data, 0, "content", "contained", "array", "code", "coding", "array", "code")
- Medication.new(this_id, this_value, this_status, this_prescriber, this_written_date, this_dosage_value, this_dosage_units, this_dosage_text, this_dispense_quantity, this_dispense_repeats, this_coding_system, this_code)
+ def type_class(type)
+ self.class.const_get(type.to_s.classify)
end
-
- def self.update_patient_medications(patient)
- results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_MED_CONST}#{RPATH_PARAMS_CONST}#{RPATH_FOR_PATIENT_PREFIX_CONST}#{patient.pid}#{RPATH_COUNT_100_CONST}")
- results_hash = JSON.parse(results.body)
- medication_array = results_hash["entry"].map do |medication_data|
- get_medication_object_from_data(medication_data)
- end
-
- data_collection_done = false
- while data_collection_done == false do
- results_next_tag = dig_with_specified_array_location(results_hash, 1, "link", "array", "rel")
- results_next_url = dig_with_specified_array_location(results_hash, 1, "link", "array", "href")
- unless results_next_tag == "next"
- results_next_tag = dig_with_specified_array_location(results_hash, 2, "link", "array", "rel")
- results_next_url = dig_with_specified_array_location(results_hash, 2, "link", "array", "href")
- end
- if results_next_tag == "next"
- results = nil
- more_medications = nil
- results = HTTParty.get(results_next_url)
- results_hash = JSON.parse(results.body)
- more_medications = results_hash["entry"].map do |medication_data|
- get_medication_object_from_data(medication_data)
- end
- medication_array += more_medications
- else
- data_collection_done = true
- end
- end
- patient.medications = medication_array
- end
-
- ### Helper Methods
-
- def self.dig_with_specified_array_location(hash, array_location, *path)
- path.inject(hash) do |location, key|
- if location && location.kind_of?(Hash)
- location[key]
- elsif location && location.kind_of?(Array) && location.length > 0
- location[array_location]
- else
- nil
- end
- end
- end
-
end
@@ -0,0 +1,29 @@
+class API
+ class Assessment
+ attr_accessor :patient
+
+ def initialize(patient)
+ @patient = patient
+ end
+
+ def fetch_assessments
+ assessments = []
+ next_page_url = url
+ while next_page_url.present?
+ additional_assessments, next_page_url = fetch_assessments_page(next_page_url)
+ assessments += additional_assessments
+ end
+ assessments
+ end
+
+ def fetch_assessments_page(page_url)
+ response = HTTParty.get(page_url)
+ results = JSON.parse(response.body)
+ assessments = results["entry"].map do |assessment|
+ extract_object_from_data(assessment)
+ end
+ next_page = NextPage.new(results).url
+ assessment, next_page
+ end
+ end
+end
@@ -0,0 +1,21 @@
+class API
+ class Condition < Assessment
+ RPATH_CONST = "Condition/"
+
+ def extract_object_from_data(condition_data)
+ #id, value, onset_date, status, code_system, code
+ data = ResponseData.new(condition_data)
+ id = data.get(0, "id")
+ value = data.get(0, "content", "code", "coding", "array", "display")
+ onset_date = data.get(0, "content", "onsetDate")
+ status = data.get(0, "content", "status")
+ code_system = data.get(0, "content", "code", "coding", "array", "system")
+ code = data.get(0, "content", "code", "coding", "array", "code")
+ ::Condition.new(id, value, onset_date, status, code_system, code)
+ end
+
+ def url(patient_pidd)
+ "#{BASE_URL_CONST}#{RPATH_CON_CONST}#{RPATH_PARAMS_CONST}#{RPATH_FOR_PATIENT_PREFIX_CONST}#{patient_pid}#{RPATH_COUNT_100_CONST}"
+ end
+ end
+end
@@ -0,0 +1,27 @@
+class API
+ class Medication < Assessment
+ RPATH_CONST = "MedicationPrescription/"
+
+ def extract_object_from_data(medication_data)
+ #id, value, status, prescriber, written_date, dosage_value, dosage_units, dosage_text, dispense_quantity, dispense_repeats, coding_system, code
+ data = ResponseData.new(medication_data)
+ id = data.get(0, "id")
+ value = data.get(0, "content", "medication", "display")
+ status = data.get(0, "content", "status")
+ prescriber = data.get(0, "content", "prescriber", "display")
+ written_date = data.get(0, "content", "dateWritten")
+ dosage_value = data.get(0, "content", "dosageInstruction", "array", "doseQuantity", "value")
+ dosage_units = data.get(0, "content", "dosageInstruction", "array", "doseQuantity", "units")
+ dosage_text = data.get(0, "content", "dosageInstruction", "array", "text")
+ dispense_quantity = data.get(0, "content", "dispense", "quantity", "value")
+ dispense_repeats = data.get(0, "content", "dispense", "numberOfRepeatsAllowed")
+ coding_system = data.get(0, "content", "contained", "array", "code", "coding", "array", "system")
+ code = data.get(0, "content", "contained", "array", "code", "coding", "array", "code")
+ Medication.new(id, value, status, prescriber, written_date, dosage_value, dosage_units, dosage_text, dispense_quantity, dispense_repeats, coding_system, code)
+ end
+
+ def url(patient_pidd)
+ "#{BASE_URL_CONST}#{RPATH_MED_CONST}#{RPATH_PARAMS_CONST}#{RPATH_FOR_PATIENT_PREFIX_CONST}#{patient_pid}#{RPATH_COUNT_100_CONST}"
+ end
+ end
+end
@@ -0,0 +1,24 @@
+class API
+ class NextPage
+ attr_accessor :results
+
+ def initialize(results)
+ @results = results
+ end
+
+ def url
+ next_link_label, next_link_url = extract_url_from_line(1)
+ next_link_label, next_link_url = extract_url_from_line(2) unless next_link_label == 'next'
+ next_link_url if next_link_label == 'next'
+ end
+
+ private
+
+ def extract_url_from_line(line_index)
+ data = ResponseData.new(results)
+ next_link_label = data.get(line_index, "link", "array", "rel")
+ next_link_url = data.get(line_index, "link", "array", "href")
+ next_link_label, next_link_url
+ end
+ end
+end
@@ -0,0 +1,26 @@
+class API
+ class Observation < Assessment
+ RPATH_CONST = "Observation/"
+
+ def extract_object_from_data(observation_data)
+ # id, value, units, comment, pub_date, status, reliability, code_system, code, display_div
+ data = ResponseData.new(observation_data)
+ id = data.get(0, "id")
+ value = data.get(0, "content", "valueQuantity", "value")
+ units = data.get(0, "content", "valueQuantity", "units")
+ comment = data.get(0, "content", "comments")
+ date = data.get(0, "con tent", "appliesDateTime")
+ status = data.get(0, "content", "status")
+ reliability = data.get(0, "content", "reliability")
+ code_system = data.get(0, "content", "name", "coding", "array", "system")
+ code = data.get(0, "content", "name", "coding", "array", "code")
+ code_text = data.get(0, "content", "name", "coding", "array", "display")
+ display = data.get(0, "content", "text", "div")
+ ::Observation.new(display, id, value, units, comment, date, status, reliability, code_system, code, code_text)
+ end
+
+ def url
+ "#{BASE_URL_CONST}#{RPATH_OBS_CONST}#{RPATH_PARAMS_CONST}#{RPATH_FOR_PATIENT_PREFIX_CONST}#{patient.pid}#{RPATH_COUNT_100_CONST}"
+ end
+ end
+end
@@ -0,0 +1,37 @@
+class API
+ class Patient
+ RPATH_CONST = "Patient/"
+
+ def all
+ results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_PAT_CONST}#{RPATH_PARAMS_CONST}#{RPATH_COUNT_100_CONST}")
+ results_hash = JSON.parse(results.body)
+ patients = results_hash["entry"].map do |patient_data|
+ extract_object_from_data(patient_data["content"])
+ end
+ end
+
+ def update(patient)
+ results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_PAT_CONST}#{patient.pid}#{RPATH_PARAMS_CONST}")
+ results_hash = JSON.parse(results.body)
+ patient.fname = results_hash["name"][0]["given"].first
+ patient.lname = results_hash["name"][0]["family"].first
+ patient
+ end
+
+ def find(pid)
+ results = HTTParty.get("#{BASE_URL_CONST}#{RPATH_PAT_CONST}#{pid}#{RPATH_PARAMS_CONST}")
+ results_hash = JSON.parse(results.body)
+ extract_object_from_data(results_hash)
+ end
+
+ private
+
+ def extract_object_from_data(patient_data)
+ data = ResponseData.new(patient_data)
+ pid = data.get(0, "identifier", "array", "value")
+ name_first = data.get(0, "name", "array", "given", "array")
+ name_last = data.get(0, "name", "array", "family", "array")
+ Patient.new(pid, name_first, name_last)
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit df24344

Please sign in to comment.