class SuperEHR::AthenaAPI

Public Class Methods

new(version, key, secret, practice_id) click to toggle source

API SPECIFIC HOUSEKEEPING ###

# File lib/super_ehr.rb, line 273
def initialize(version, key, secret, practice_id)
  @uri = URI.parse('https://api.athenahealth.com/')
  @version = version
  @key = key
  @secret = secret
  @practiceid = practice_id
end

Public Instance Methods

get_changed_patients(ts='') click to toggle source
# File lib/super_ehr.rb, line 317
def get_changed_patients(ts='')
  patient_ids = get_changed_patients_ids(ts)
  patients = []
  for id in patient_ids
    patients << get_patient(id)
  end
  return patients
end
get_changed_patients_ids(start_date, end_date=Time.new.strftime("%m/%d/%Y %H:%M:%S")) click to toggle source

start_date needs to be in mm/dd/yyyy returns a list of patient ids that have been changed since start_date

# File lib/super_ehr.rb, line 328
def get_changed_patients_ids(start_date, end_date=Time.new.strftime("%m/%d/%Y %H:%M:%S"))
  subscribe = make_request("GET", "patients/changed/subscription", {})
  if subscribe.has_key?("status") and subscribe["status"] == "ACTIVE"
    response = make_request("GET", "patients/changed",
                            { :ignorerestrictions => false,
                              :leaveunprocessed => false,
                              :showprocessedstartdatetime => "#{start_date} 00:00:00",
                              :showprocessedenddatetime => end_date })
    patient_ids = []
    if response.key?("patients")
      patient_ids = response["patients"].map {|x| x["patientid"] }
    end
    return patient_ids
  else
    return nil
  end
end
get_patient(patient_id) click to toggle source

API CALLS ###

# File lib/super_ehr.rb, line 308
def get_patient(patient_id)
  response = make_request("GET", "patients/#{patient_id}", {})
  patient_info = {}
  if not response[0].empty?
    patient_info = response[0]
  end
  return patient_info
end
get_request_headers() click to toggle source
# File lib/super_ehr.rb, line 281
def get_request_headers
  return { 'Authorization' => "Bearer #{refresh_token}" }
end
get_request_url(endpoint) click to toggle source
# File lib/super_ehr.rb, line 285
def get_request_url(endpoint)
  return "#{@uri}/#{@version}/#{@practiceid}/#{endpoint}"
end
get_scheduled_patients(date, department_id=1) click to toggle source
# File lib/super_ehr.rb, line 346
def get_scheduled_patients(date, department_id=1)
  response = make_request("GET", "appointments/booked",
                          {:departmentid => department_id, :startdate => date, :enddate => date})
  patients = []
  if not response["appointments"].empty?
    for scheduled_patient in response["appointments"]
      patients << scheduled_patient
    end
  end
  return patients
end
refresh_token() click to toggle source
# File lib/super_ehr.rb, line 289
def refresh_token
  auth_paths = {
    'vi' => 'oauth',
    'preview1' => 'oauthpreview',
    'openpreview1' => 'oauthopenpreview',
  }

  auth = {:username => @key, :password => @secret}

  url = "#{@uri}/#{auth_paths[@version]}/token"
  params = {:grant_type => "client_credentials"}
  response = HTTParty.post(url, :body => params, :basic_auth => auth)

  return response["access_token"]
end
upload_document(patient_id, filepath, description, department_id=-1) click to toggle source

might have issues if patient is in multiple departments

# File lib/super_ehr.rb, line 359
def upload_document(patient_id, filepath, description, department_id=-1)
  endpoint = "patients/#{patient_id}/documents"
  headers = { 'Authorization' => "Bearer #{refresh_token}" }
  url = "#{@uri}/#{@version}/#{@practiceid}/#{endpoint}"
  params = {
    :departmentid => department_id != -1 ? department_id : get_patient(patient_id)["departmentid"],
    :attachmentcontents  => File.new(filepath),
    :documentsubclass    => "CLINICALDOCUMENT",
    :autoclose           => false,
    :internalnote        => description,
    :actionnote          => description }

    response = HTTMultiParty.post(url, :body => params, :headers => headers)
    return response
end