Sha256: 988024e91313fa8cb34de674cf765818ba2326c61c1e0df3408a685c723f1976

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 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

    def self.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 self.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 self.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 self.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 self.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 self.build_date(date, time)
      "#{date.strftime(MDY_FORMAT)}|#{time}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
medivo-0.2.1 app/models/medivo/appointment.rb
medivo-0.2.0 app/models/medivo/appointment.rb