class SuperEHR::DrChronoAPI

Attributes

access_token[R]
refresh_token[R]

Public Class Methods

new(args={}) click to toggle source

API SPECIFIC HOUSEKEEPING ###

# File lib/super_ehr.rb, line 382
def initialize(args={})
  params = {:access_code => '',
            :access_token => '',
            :refresh_token => '',
            :client_id => '',
            :client_secret => '',
            :redirect_uri => ''
          }
  params = params.merge(args)
  if (params[:access_code] == '' && (params[:access_token] == '' || params[:refresh_token] == ''))
    raise ArgumentError, "Access Code='#{params[:access_code]}' is blank or one or more of Access Token='#{params[:access_token]}', Refresh Token='#{params[:refresh_token]}' is blank"
  end
  @access_code = params[:access_code]
  @client_id = params[:client_id]
  @client_secret = params[:client_secret]
  @redirect_uri = params[:redirect_uri]
  @access_token = params[:access_token]
  @refresh_token = params[:refresh_token]
  @uri = URI.parse("https://drchrono.com")
  exchange_token
end

Public Instance Methods

get_access_token() click to toggle source
# File lib/super_ehr.rb, line 412
def get_access_token
  return @access_token
end
get_changed_patients(date) click to toggle source
# File lib/super_ehr.rb, line 440
def get_changed_patients(date)
  if self.class != SuperEHR::DrChronoAPI
    date = date.gsub(/\//, '-')
    date = Date.strptime(date, '%m-%d-%Y').ios8601
  end
  return get_patients({:since => date})
end
get_changed_patients_ids(ts) click to toggle source
# File lib/super_ehr.rb, line 448
def get_changed_patients_ids(ts)
  patients = get_changed_patients(ts)
  ids = []
  for patient in patients
    ids << patient["id"]
  end
  return ids
end
get_patient(patient_id) click to toggle source

Not efficient Get the patient using patient id from our database

# File lib/super_ehr.rb, line 425
def get_patient(patient_id)
  patients = get_patients()
  for patient in patients
    if patient["id"] == patient_id
      return patient
    end
  end
  return nil
end
get_patients(params={}) click to toggle source
# File lib/super_ehr.rb, line 435
def get_patients(params={})
  patient_url = 'api/patients'
  return chrono_request(patient_url, params)
end
get_refresh_token() click to toggle source
# File lib/super_ehr.rb, line 416
def get_refresh_token
  return @refresh_token
end
get_request_headers() click to toggle source
# File lib/super_ehr.rb, line 404
def get_request_headers
  return { 'Authorization' => "Bearer #{@access_token}"}
end
get_request_url(endpoint) click to toggle source
# File lib/super_ehr.rb, line 408
def get_request_url(endpoint)
  return "#{@uri}/#{endpoint}"
end
get_scheduled_patients(day='') click to toggle source
# File lib/super_ehr.rb, line 457
def get_scheduled_patients(day='')
  url = 'api/appointments'
  return chrono_request(url, {:date => day.gsub(/\//, '-')})
end
upload_document(patient_id, filepath, description) click to toggle source
# File lib/super_ehr.rb, line 462
def upload_document(patient_id, filepath, description)
  url = get_request_url("api/documents")
  headers = get_request_headers
  params = {
    :doctor => /\/api\/doctors\/.*/.match(get_patient(patient_id)["doctor"]),
    :patient => "/api/patients/#{patient_id}",
    :description => description,
    :date => Time.now.strftime("%Y-%m-%d") << " 00:00:00",
    :document => File.new(filepath)
  }
  response = HTTMultiParty.post(url, :body => params, :headers => headers)
  return response
end

Private Instance Methods

chrono_request(endpoint, params={}) click to toggle source
# File lib/super_ehr.rb, line 478
def chrono_request(endpoint, params={})
  result = []
  while endpoint
    data = make_request("GET", endpoint, params)
    if data["results"]
      result = result | data["results"]
    end
    endpoint = data["next"]
  end
  return result
end
exchange_token() click to toggle source
# File lib/super_ehr.rb, line 490
def exchange_token
  if @refresh_token == '' || @refresh_token == nil
    post_args = {
      "code" => @access_code,
      "grant_type" => "authorization_code",
      "redirect_uri" => @redirect_uri,
      "client_id" => @client_id,
      "client_secret" => @client_secret
    }
    response = HTTParty.post(get_request_url("o/token/"),
                             :body => post_args)
    @refresh_token = response["refresh_token"]
    @access_token = response["access_token"]
    return response["access_token"]
  else
    post_args = {
      "refresh_token" => @refresh_token,
      "grant_type" => "refresh_token",
      "redirect_uri" => @redirect_uri,
      "client_id" => @client_id,
      "client_secret" => @client_secret
    }
    response = HTTParty.post(get_request_url("o/token/"),
                             :body => post_args)
    @refresh_token = response["refresh_token"]
    @access_token = response["access_token"]
    return response["access_token"]
  end
end