Sha256: 5aaab0aa616c153acc049eedbdb44ffcbfb0956e0051d4cf9ac0d80525bdf019

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

require_relative 'resource'

module Medivo
  class Order < Resource
    set_element_name 'customer'
    set_collection_name 'customers'

    validates_presence_of :email, :address, :zip, :dob, :home_phone, :psc,
                          :test_types, :gender, :account_number
    validates_inclusion_of :gender, :in => %w(Male Female)
    validates_with Medivo::DateValidator, :fields=>[:dob]
    validates_with Medivo::ArrayValidator, :fields=>[:test_types]

    def read_attribute_for_validation(key)
       @attributes[key]
    end

    def initialize(attributes = {}, persisted = false)
      super(attributes, persisted)
      self.draw_location = 'PSC'
      self.take_tests_same_day = true
    end

    # overriding save to return false if it does not save without errors
    def save
      new? ? create : update
      valid?
    end

    # overriding create to capture 404 status and put that in error
    # messages instead of throwing exception
    def create
        super
        @remote_errors = nil
    rescue ActiveResource::ResourceNotFound => e
      @remote_errors = e
      load_remote_errors(e,true)
    end

    def to_param
      requisition_id
    end

    def requisition_id
      requisition_number
    end

    def self.get_order(requsition_id)
      find(requsition_id)
    end

    def results
      @results ||= Array.wrap(try(:reconciled_results).try(:result))
    end

    def simulate_result(lab_prefix, type='normal')
      raise "Incorrect Result Type" unless %w(normal false_positive positive).include?(type) and %w(qd lc).include?(lab_prefix)
      body = case type
        when 'normal' then
          "<mock><simulate>result</simulate><result kind='#{type}' complete='complete'></result></mock>"
        when 'false_positive', 'positive' then
          "<mock><simulate>result</simulate><result><hl7>#{Base64.encode64(File.read("lib/pdfs/#{lab_prefix}_#{type}.hl7"))}</hl7></result></mock>"
      end

      post(:mock_order, {}, body)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
medivo-0.0.8 app/models/medivo/order.rb
medivo-0.0.7 app/models/medivo/order.rb
medivo-0.0.4 app/models/medivo/order.rb
medivo-0.0.3 app/models/medivo/order.rb