Search code examples
c#ruby-on-railspostrest-clienthl7

Rails POST request to Practice Fusion API


I'm working with the Practice Fusion API to receive pending orders. I'm trying to write a POST request to acknowledge the downloaded orders that were received during the GET request. Once Practice Fusion receives the POST request, the pending orders are removed.

Gems I'm using:

rest-client

simple_hl7 --Used for hl7 parsing

Here's my working GET request:

def download_pf_orders
  download_count = 0
  uri = "#{PF_TEST_PENDING_API_URL}"
  rest_resource = RestClient::Resource.new(uri, PF_TEST_USERNAME, PF_TEST_PASSWORD)

  begin
    response = rest_resource.get(accept: 'application/json')
    json = JSON.parse(response)

    json.each do |data|

      sequence = data['SequenceNumber']
      puts "### Last Sequence Number: #{sequence}"

      PfOrder.create(
        sequence_number: data['SequenceNumber'],
        message_guid: data['MessageGuid'],
        hl7_document: data['Hl7Document']
        )
      download_count += 1
    end
  rescue => e
    puts "### Status Code: #{e.response.code} ###"
  end
  puts "### Downloaded Orders: #{download_count} ###"
end

Here's the POST request criteria outlined by Practice Fusion:

Sample HL7 ACK Message

Given a retrieved order message with the following MSH segment:

MSH|^~\&|PracticeFusion|ClientID|||20130930225002+0000||OML^O21^OML_O21|a783a 5d7-c9b2-42e9-abb1-a1b473079512|P|2.5.1|||AL|NE|||||ELINCS_MT-OML-1_1.0

The expected HL7 ACK message would contain the following MSH and MSA segments:

MSH|^~\&|PracticeFusion|VendorCode||ClientID|20130930225002+0000||ACK^ELINCS^ ACK_ELINCS|MessageControlID|P|2.5.1||||||||| ELINCS_MT-ACK-1_1.0

MSA|CA|a783a5d7-c9b2-42e9-abb1-a1b473079512

***As indicated by the color coordination in the sample order and acknowledgement message above, your ACK logic should populate MSH-6 Client ID of the acknowledgement using the MSH-4 Client ID value of the associated order, and the MSA-2 value using the control ID in MSH-10 of the order message.

***The ‘Vendor Code’ value populated in MSH-4 of your acknowledgement message is a unique value assigned by Practice Fusion. Please contact your implementation resource for this value if it has not previously been provided.

Here's some provided C# sample code to accomplish this:

/// <summary>
/// Post an HL7 Order Acknowledgement message back to PracticeFusion to signal
acceptance...
/// </summary>
static void AcknowledgeOrder(string hl7AckMessage)
{
  var request = (HttpWebRequest)
  WebRequest.Create(ConfigurationManager.AppSettings["OrderAcknowledgementUri"]);
  request.PreAuthenticate = true;
  request.Credentials = new
  NetworkCredential(ConfigurationManager.AppSettings["PF.ApiUserName"],
   ConfigurationManager.AppSettings["PF.ApiPassword"]);
  request.Method = WebRequestMethods.Http.Post;
  request.ContentType = "application/x-www-form-urlencoded";
  using (var writer = new StreamWriter(request.GetRequestStream()))
  {
   writer.Write(hl7AckMessage);
 }
}

Solution

  • I figured this out. This method will successfully POST back but still needs to account if orders have been posted successfully.

    def acknowledge_pf_order
      hl7_message = PfOrder.all
      hl7_message.each do |msg|
        clean_msg = msg.hl7_document.gsub(/\r/, '')
    
        msh_segment = clean_msg.gsub(MSH)
        msh_segment.each do |seg|
          parsed_msh = SimpleHL7::Message.parse(seg)
    
          parsed_msh.msh[4] = 'yourVendorCode'
          parsed_msh.msh[6] = 'yourClientID'
          parsed_msh.msh[9][1] = 'ACK'
          parsed_msh.msh[9][2] = 'ELINCS'
          parsed_msh.msh[9][3] = 'ACK_ELINCS'
          parsed_msh.msh[21] = 'ELINCS_MT-ACK-1_1.0'
    
          message_id = parsed_msh.msh[10]
          ack_message = parsed_msh.to_hl7
    
          msa_segment = "\nMSA|CA|#{message_id.to_s}"
    
          payload = ack_message << msa_segment
    
          uri = "#{PF_TEST_ACK_API_URL}"
          rest_resource = RestClient::Resource.new(uri, PF_TEST_USERNAME, PF_TEST_PASSWORD)
    
          begin
            response = rest_resource.post payload, content_type: 'text/plain'
            puts "### POST Status Code: #{response.code} ###"
          rescue Exception => e
            puts "### POST Status Code: #{e.response.code} ###"
          end
        end
      end
    end