Sha256: 952b23abd7a37f82ed3265d7bd18a92909c2d3d1fa7dc9f7edd3a7b899f7bb09

Contents?: true

Size: 1.7 KB

Versions: 5

Compression:

Stored size: 1.7 KB

Contents

require 'restclient'

module Medivo
  class Appointment

    LABCORP_FORMAT = "%m/%d/%Y|%H:%M %P" unless defined? LABCORP_FORMAT
    MDY_FORMAT = "%m/%d/%Y" unless defined? MDY_FORMAT

    class << self

      def find(lab_code, date, am_pm='')
        if real_data?
          data = resource.get :params=>{:labcorp_id=>lab_code, :appointment_date=>date}
          data = JSON.parse(data)
        else
          data = build_fake_data(date)
        end
        filter_data(data, am_pm)
      end

      def resource
        @resource ||= begin
          @config = ResourceConfig.find 'appointment_resource.yml'
          RestClient::Resource.new @config.href, :timeout => (@config.timeout || 12)
        rescue => e
          Rails.logger.error e.inspect
          # blow up later, so the server can start
        end
      end

      def real_data?
        resource # to init the resource and config file
        true unless ((Rails.env.development? or Rails.env.staging?) and @config.real_data == false)
      end

      def filter_data(hash, am_pm)
        now = Time.now
        hash['times'].reject! do |time|
          (!time.match(/#{am_pm}/) or Time.strptime(time, LABCORP_FORMAT) < now)
        end
        hash
      end

      def build_fake_data(date)
        date = date.is_a?(String) ? Date.strptime(date, MDY_FORMAT) : date
        {'times'=> [
                build_date(date, "08:30 AM"), build_date(date, "10:30 AM"),
                build_date((date + 1), "03:30 PM"), build_date((date+1), "01:30 PM"),
                build_date((date + 2), "10:30 AM"), build_date((date+1), "03:00 PM"),
        ]}
      end

      def build_date(date, time)
        "#{date.strftime(MDY_FORMAT)}|#{time}"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
medivo-0.2.7 app/models/medivo/appointment.rb
medivo-0.2.6 app/models/medivo/appointment.rb
medivo-0.2.5 app/models/medivo/appointment.rb
medivo-0.2.3 app/models/medivo/appointment.rb
medivo-0.2.2 app/models/medivo/appointment.rb